Code cleanup

(there was an attempt to redirect yt-dlp stdout)
This commit is contained in:
DarkCat09 2024-05-03 20:10:42 +04:00
parent 2b47b002e6
commit 93c5ea3b18
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
4 changed files with 35 additions and 25 deletions

View file

@ -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)

View file

@ -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,
})

View file

@ -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')

View file

@ -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)