From 93c5ea3b188bbfe7a620648fc13b9f5f8401bb18 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 3 May 2024 20:10:42 +0400 Subject: [PATCH] Code cleanup (there was an attempt to redirect yt-dlp stdout) --- backend/main.py | 29 +++++++++++------------------ backend/response.py | 4 ++-- backend/ydl_pool.py | 2 +- backend/ydl_wrap.py | 25 +++++++++++++++++++++---- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/backend/main.py b/backend/main.py index c954104..35b7086 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,9 +1,6 @@ import asyncio import json -import secrets -import time - import websockets import response @@ -11,17 +8,15 @@ import ydl_pool import ydl_wrap +type SocketT = websockets.WebSocketServerProtocol + + # TODO: config HOST = '127.0.0.1' PORT = 5678 -def generate_key() -> str: - - return hex(time.time_ns()) + secrets.token_hex(2) - - -async def handler(socket: websockets.WebSocketServerProtocol) -> None: +async def handler(socket: SocketT) -> None: ydls = ydl_pool.Downloaders() @@ -41,14 +36,12 @@ async def handler(socket: websockets.WebSocketServerProtocol) -> None: )) case 'download': # download by URL - # TODO: send all yt-dlp's output to client - await socket.send(response.ok_download( - await ydl_wrap.download( - ydls.get_ydl(data['site']), - data['url'], - data.get('items'), - ) - )) + ret = await ydl_wrap.download( + ydls.get_ydl(data['site']), + data['url'], + data.get('items'), + ) + await socket.send(response.ok_downloaded(ret)) # TODO: cancellation @@ -71,4 +64,4 @@ async def main() -> None: if __name__ == '__main__': - asyncio.run(main()) + asyncio.run(main(), debug=True) diff --git a/backend/response.py b/backend/response.py index 7ea223c..47e0c4b 100644 --- a/backend/response.py +++ b/backend/response.py @@ -9,9 +9,9 @@ def ok_playlist(items: list[str]) -> str: "data": items, }) -def ok_download(ret: int) -> str: +def ok_downloaded(ret: int) -> str: return json.dumps({ - "ok": True, + "type": "downloaded", "data": ret, }) diff --git a/backend/ydl_pool.py b/backend/ydl_pool.py index b083517..10bed62 100644 --- a/backend/ydl_pool.py +++ b/backend/ydl_pool.py @@ -48,7 +48,7 @@ class Downloaders: ydl = self.ydls[site] if ydl is None: ydl = create_ydl_fn[site]() - ydl.params['trim_file_name'] = 40 # TODO: config + ydl.params['trim_file_name'] = 255 # TODO: config # NOTE: includes path, not only filename # artists.0 instead of artist, because it can contain "feat. ..." ydl.params['outtmpl']['default'] = 'music/%(artists.0)s/%(album)s/%(track)s.%(ext)s' ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process') diff --git a/backend/ydl_wrap.py b/backend/ydl_wrap.py index 560c8cf..cd37f35 100644 --- a/backend/ydl_wrap.py +++ b/backend/ydl_wrap.py @@ -6,7 +6,12 @@ from yt_dlp import YoutubeDL async def get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]: - return await asyncio.get_event_loop().run_in_executor(None, _target_get_playlist_items, ydl, url) + return await asyncio.get_event_loop().run_in_executor( + None, + _target_get_playlist_items, + ydl, + url, + ) def _target_get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]: @@ -17,12 +22,24 @@ def _target_get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]: return [entry['title'] for entry in info['entries']] -async def download(ydl: YoutubeDL, url: str, playlist_items: Iterable[int] | None = None) -> int: +async def download( + ydl: YoutubeDL, + url: str, + playlist_items: Iterable[int] | None = None) -> int: - return await asyncio.get_event_loop().run_in_executor(None, _target_download, ydl, url, playlist_items) + return await asyncio.get_event_loop().run_in_executor( + None, + _target_download, + ydl, + url, + playlist_items, + ) -def _target_download(ydl: YoutubeDL, url: str, playlist_items: Iterable[int] | None = None) -> int: +def _target_download( + ydl: YoutubeDL, + url: str, + playlist_items: Iterable[int] | None = None) -> int: if playlist_items: ydl.params['playlist_items'] = ','.join(str(i) for i in playlist_items)