Code cleanup
(there was an attempt to redirect yt-dlp stdout)
This commit is contained in:
parent
2b47b002e6
commit
93c5ea3b18
4 changed files with 35 additions and 25 deletions
|
@ -1,9 +1,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
|
||||||
import secrets
|
|
||||||
import time
|
|
||||||
|
|
||||||
import websockets
|
import websockets
|
||||||
|
|
||||||
import response
|
import response
|
||||||
|
@ -11,17 +8,15 @@ import ydl_pool
|
||||||
import ydl_wrap
|
import ydl_wrap
|
||||||
|
|
||||||
|
|
||||||
|
type SocketT = websockets.WebSocketServerProtocol
|
||||||
|
|
||||||
|
|
||||||
# TODO: config
|
# TODO: config
|
||||||
HOST = '127.0.0.1'
|
HOST = '127.0.0.1'
|
||||||
PORT = 5678
|
PORT = 5678
|
||||||
|
|
||||||
|
|
||||||
def generate_key() -> str:
|
async def handler(socket: SocketT) -> None:
|
||||||
|
|
||||||
return hex(time.time_ns()) + secrets.token_hex(2)
|
|
||||||
|
|
||||||
|
|
||||||
async def handler(socket: websockets.WebSocketServerProtocol) -> None:
|
|
||||||
|
|
||||||
ydls = ydl_pool.Downloaders()
|
ydls = ydl_pool.Downloaders()
|
||||||
|
|
||||||
|
@ -41,14 +36,12 @@ async def handler(socket: websockets.WebSocketServerProtocol) -> None:
|
||||||
))
|
))
|
||||||
|
|
||||||
case 'download': # download by URL
|
case 'download': # download by URL
|
||||||
# TODO: send all yt-dlp's output to client
|
ret = await ydl_wrap.download(
|
||||||
await socket.send(response.ok_download(
|
ydls.get_ydl(data['site']),
|
||||||
await ydl_wrap.download(
|
data['url'],
|
||||||
ydls.get_ydl(data['site']),
|
data.get('items'),
|
||||||
data['url'],
|
)
|
||||||
data.get('items'),
|
await socket.send(response.ok_downloaded(ret))
|
||||||
)
|
|
||||||
))
|
|
||||||
|
|
||||||
# TODO: cancellation
|
# TODO: cancellation
|
||||||
|
|
||||||
|
@ -71,4 +64,4 @@ async def main() -> None:
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
asyncio.run(main(), debug=True)
|
||||||
|
|
|
@ -9,9 +9,9 @@ def ok_playlist(items: list[str]) -> str:
|
||||||
"data": items,
|
"data": items,
|
||||||
})
|
})
|
||||||
|
|
||||||
def ok_download(ret: int) -> str:
|
def ok_downloaded(ret: int) -> str:
|
||||||
return json.dumps({
|
return json.dumps({
|
||||||
"ok": True,
|
"type": "downloaded",
|
||||||
"data": ret,
|
"data": ret,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ class Downloaders:
|
||||||
ydl = self.ydls[site]
|
ydl = self.ydls[site]
|
||||||
if ydl is None:
|
if ydl is None:
|
||||||
ydl = create_ydl_fn[site]()
|
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. ..."
|
# 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.params['outtmpl']['default'] = 'music/%(artists.0)s/%(album)s/%(track)s.%(ext)s'
|
||||||
ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process')
|
ydl.add_post_processor(id3pp.ID3TagsPP(), when='post_process')
|
||||||
|
|
|
@ -6,7 +6,12 @@ from yt_dlp import YoutubeDL
|
||||||
|
|
||||||
async def get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
|
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]:
|
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']]
|
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:
|
if playlist_items:
|
||||||
ydl.params['playlist_items'] = ','.join(str(i) for i in playlist_items)
|
ydl.params['playlist_items'] = ','.join(str(i) for i in playlist_items)
|
||||||
|
|
Loading…
Add table
Reference in a new issue