musicdlp/backend/response.py

38 lines
762 B
Python
Raw Normal View History

2024-05-02 16:18:27 +03:00
import json
2024-05-08 12:52:36 +03:00
import logging
from typing import Literal
2024-05-02 16:18:27 +03:00
2024-05-05 18:04:18 +03:00
def playlist(items: list[str]) -> str:
2024-05-02 16:18:27 +03:00
return json.dumps({
2024-05-05 18:04:18 +03:00
"type": "items",
"data": items,
2024-05-02 16:18:27 +03:00
})
2024-05-08 12:52:36 +03:00
type YdlLogLevel = Literal['debug'] | Literal['warning'] | Literal['error']
def ydl_log(level: YdlLogLevel, msg: str) -> str:
2024-05-05 18:04:18 +03:00
return json.dumps({
2024-05-08 12:52:36 +03:00
"type": "ydl_log",
"level": level,
2024-05-05 18:04:18 +03:00
"data": msg,
})
2024-05-08 12:52:36 +03:00
def ydl_end(ret: int) -> str:
2024-05-02 16:18:27 +03:00
return json.dumps({
2024-05-08 12:52:36 +03:00
"type": "ydl_end",
"data": ret,
2024-05-02 16:18:27 +03:00
})
2024-05-08 12:52:36 +03:00
def error(ex: Exception) -> str:
2024-05-08 12:52:36 +03:00
logging.getLogger('musicdlp').exception(ex)
2024-05-02 16:18:27 +03:00
return json.dumps({
2024-05-05 18:04:18 +03:00
"type": "error",
"data": {
"type": ex.__class__.__qualname__,
"message": str(ex),
}
2024-05-02 16:18:27 +03:00
})