musicdlp/backend/ydl_wrap.py
DarkCat09 93c5ea3b18
Code cleanup
(there was an attempt to redirect yt-dlp stdout)
2024-05-03 20:10:42 +04:00

51 lines
1.1 KiB
Python

import asyncio
from typing import Iterable
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,
)
def _target_get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
info = ydl.extract_info(url, download=False, process=False)
if info is None:
raise RuntimeError('ydl.extract_info returned None')
return [entry['title'] for entry in info['entries']]
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,
)
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)
ret = ydl.download(url)
del ydl.params['playlist_items']
return ret