2024-05-03 17:42:53 +03:00
|
|
|
import asyncio
|
|
|
|
from typing import Iterable
|
|
|
|
|
|
|
|
from yt_dlp import YoutubeDL
|
|
|
|
|
|
|
|
|
|
|
|
async def get_playlist_items(ydl: YoutubeDL, url: str) -> list[str]:
|
|
|
|
|
2024-05-03 19:10:42 +03:00
|
|
|
return await asyncio.get_event_loop().run_in_executor(
|
|
|
|
None,
|
|
|
|
_target_get_playlist_items,
|
|
|
|
ydl,
|
|
|
|
url,
|
|
|
|
)
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
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']]
|
|
|
|
|
|
|
|
|
2024-05-03 19:10:42 +03:00
|
|
|
async def download(
|
|
|
|
ydl: YoutubeDL,
|
|
|
|
url: str,
|
|
|
|
playlist_items: Iterable[int] | None = None) -> int:
|
2024-05-03 17:42:53 +03:00
|
|
|
|
2024-05-03 19:10:42 +03:00
|
|
|
return await asyncio.get_event_loop().run_in_executor(
|
|
|
|
None,
|
|
|
|
_target_download,
|
|
|
|
ydl,
|
|
|
|
url,
|
|
|
|
playlist_items,
|
|
|
|
)
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
|
2024-05-03 19:10:42 +03:00
|
|
|
def _target_download(
|
|
|
|
ydl: YoutubeDL,
|
|
|
|
url: str,
|
|
|
|
playlist_items: Iterable[int] | None = None) -> int:
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
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
|