JSON schema v0.0.2

This commit is contained in:
DarkCat09 2024-05-05 19:04:18 +04:00
parent 96be306b2b
commit e9c55a67d3
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
2 changed files with 14 additions and 10 deletions

View file

@ -25,7 +25,7 @@ async def handler(socket: SocketT) -> None:
match data['action']: match data['action']:
case 'list': # list tracks in album case 'list': # list tracks in album
await socket.send(response.ok_playlist( await socket.send(response.playlist(
await ydl_wrap.get_playlist_items( await ydl_wrap.get_playlist_items(
ydls.get_ydl(data['site']), ydls.get_ydl(data['site']),
data['url'], data['url'],
@ -38,7 +38,7 @@ async def handler(socket: SocketT) -> None:
data['url'], data['url'],
data.get('items'), data.get('items'),
) )
await socket.send(response.ok_downloaded(ret)) await socket.send(response.dl_end(ret))
# TODO: cancellation # TODO: cancellation

View file

@ -1,25 +1,29 @@
import json import json
import traceback import traceback
OK = '{"ok":true}' def playlist(items: list[str]) -> str:
def ok_playlist(items: list[str]) -> str:
return json.dumps({ return json.dumps({
"ok": True, "type": "items",
"data": items, "data": items,
}) })
def ok_downloaded(ret: int) -> str: def dl_progress(msg: str) -> str:
return json.dumps({ return json.dumps({
"type": "downloaded", "type": "dl_progress",
"data": msg,
})
def dl_end(ret: int) -> str:
return json.dumps({
"type": "dl_end",
"data": ret, "data": ret,
}) })
def error(ex: Exception) -> str: def error(ex: Exception) -> str:
traceback.print_tb(ex.__traceback__) traceback.print_tb(ex.__traceback__)
return json.dumps({ return json.dumps({
"ok": False, "type": "error",
"error": { "data": {
"type": ex.__class__.__qualname__, "type": ex.__class__.__qualname__,
"message": str(ex), "message": str(ex),
} }