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]:
|
|
|
|
|
2024-05-03 19:57:20 +03:00
|
|
|
info = ydl.extract_info(url, download=False, process=True)
|
2024-05-03 17:42:53 +03:00
|
|
|
if info is None:
|
|
|
|
raise RuntimeError('ydl.extract_info returned None')
|
2024-05-03 19:57:20 +03:00
|
|
|
return [
|
|
|
|
entry['track'] if 'track' in entry else entry['title']
|
|
|
|
for entry in info['entries']
|
|
|
|
]
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
|
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
|