33 lines
1,021 B
Python
33 lines
1,021 B
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
|