Fix: keep connection

This commit is contained in:
DarkCat09 2024-05-03 14:39:29 +04:00
parent 26312a655b
commit 77127d3476
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3

View file

@ -1,6 +1,5 @@
import asyncio import asyncio
import json import json
from typing import Any
import secrets import secrets
import time import time
@ -25,31 +24,32 @@ def generate_key() -> str:
async def handler(socket: websockets.WebSocketServerProtocol) -> None: async def handler(socket: websockets.WebSocketServerProtocol) -> None:
data = json.loads(await socket.recv()) async for message in socket:
if 'action' not in data: data = json.loads(await socket.recv())
await socket.send(response.error_field('action')) if 'action' not in data:
return
match data['action']:
case 'init': # create session
if data.get('site') not in SITES:
await socket.send(response.error_field('site'))
return
key = generate_key()
sessions[key] = "" # TODO
await socket.send(response.ok_init(key))
case 'list': # list tracks in album
if 'url' not in data:
await socket.send(response.error_field('url'))
return
await socket.send(response.ok_playlist(['title 1', 'title 2'])) # TODO
case 'download': # download by URL
if 'url' not in data:
await socket.send(response.error_field('url'))
return
# TODO: pass command to the thread started in `init`
await socket.send(response.OK)
case _:
await socket.send(response.error_field('action')) await socket.send(response.error_field('action'))
return
match data['action']:
case 'init': # create session
if data.get('site') not in SITES:
await socket.send(response.error_field('site'))
return
key = generate_key()
sessions[key] = "" # TODO
await socket.send(response.ok_init(key))
case 'list': # list tracks in album
if 'url' not in data:
await socket.send(response.error_field('url'))
return
await socket.send(response.ok_playlist(['title 1', 'title 2'])) # TODO
case 'download': # download by URL
if 'url' not in data:
await socket.send(response.error_field('url'))
return
# TODO: start thread and send all yt-dlp's output to client
await socket.send(response.OK)
case _:
await socket.send(response.error_field('action'))
async def main() -> None: async def main() -> None: