2024-05-05 18:23:20 +03:00
|
|
|
import asyncio
|
2024-05-08 12:52:36 +03:00
|
|
|
import logging
|
2024-05-05 18:23:20 +03:00
|
|
|
from typing import Callable, Awaitable, Iterable
|
|
|
|
|
2024-05-03 17:42:53 +03:00
|
|
|
from yt_dlp import YoutubeDL
|
|
|
|
from yt_dlp.postprocessor import FFmpegExtractAudioPP
|
|
|
|
|
2024-05-03 19:57:20 +03:00
|
|
|
import config
|
2024-05-08 12:52:36 +03:00
|
|
|
import response
|
2024-05-03 17:42:53 +03:00
|
|
|
import id3pp
|
|
|
|
|
|
|
|
|
|
|
|
class _CreateYDL:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def youtube() -> YoutubeDL:
|
|
|
|
ydl = YoutubeDL({'format': 'ba'})
|
|
|
|
ydl.add_post_processor(id3pp.InfoYouTubePP(), when='before_dl')
|
|
|
|
ydl.add_post_processor(FFmpegExtractAudioPP(preferredcodec='mp3'), when='post_process')
|
|
|
|
return ydl
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def yt_proxied() -> YoutubeDL:
|
|
|
|
ydl = _CreateYDL.youtube()
|
2024-05-28 08:09:59 +03:00
|
|
|
proxy = config.get().yt_proxy
|
|
|
|
if proxy is not None:
|
|
|
|
ydl.params['proxy'] = proxy
|
2024-05-03 17:42:53 +03:00
|
|
|
return ydl
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def yandex() -> YoutubeDL:
|
2024-05-03 19:57:20 +03:00
|
|
|
return YoutubeDL()
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
create_ydl_fn = {
|
|
|
|
'youtube': _CreateYDL.youtube,
|
|
|
|
'yt_proxied': _CreateYDL.yt_proxied,
|
|
|
|
'yandex': _CreateYDL.yandex,
|
|
|
|
}
|
|
|
|
|
|
|
|
ydl_fn_keys = create_ydl_fn.keys()
|
|
|
|
|
2024-05-06 20:07:19 +03:00
|
|
|
# need process=True for track title in extract_info output
|
|
|
|
NP_YDLS = {'yandex'}
|
|
|
|
|
2024-05-03 17:42:53 +03:00
|
|
|
|
2024-05-08 12:52:36 +03:00
|
|
|
class YdlLogger:
|
2024-05-03 17:42:53 +03:00
|
|
|
|
2024-05-05 18:23:20 +03:00
|
|
|
def __init__(
|
|
|
|
self,
|
2024-05-08 12:52:36 +03:00
|
|
|
log_cb: Callable[[response.YdlLogLevel, str], Awaitable],
|
|
|
|
loop: asyncio.AbstractEventLoop) -> None:
|
|
|
|
self.log_cb = log_cb
|
|
|
|
self.loop = loop
|
|
|
|
self.mdlp_logger = logging.getLogger('musicdlp')
|
|
|
|
|
|
|
|
def debug(self, msg: str) -> None:
|
|
|
|
asyncio.run_coroutine_threadsafe(self.log_cb('debug', msg), self.loop)
|
|
|
|
|
|
|
|
def info(self, _: str) -> None: # afaik not used in yt-dlp
|
|
|
|
pass
|
|
|
|
|
|
|
|
def warning(self, msg: str) -> None:
|
|
|
|
asyncio.run_coroutine_threadsafe(self.log_cb('warning', msg), self.loop)
|
|
|
|
|
|
|
|
def error(self, msg: str) -> None:
|
|
|
|
self.mdlp_logger.error(msg)
|
|
|
|
asyncio.run_coroutine_threadsafe(self.log_cb('error', msg), self.loop)
|
|
|
|
|
|
|
|
|
|
|
|
class Downloader:
|
|
|
|
|
|
|
|
def __init__(self, logger: YdlLogger | None = None) -> None:
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
self.ydls: dict[str, YoutubeDL | None] = {
|
|
|
|
'youtube': None,
|
|
|
|
'yt_proxied': None,
|
|
|
|
'yandex': None,
|
|
|
|
}
|
|
|
|
|
2024-05-05 18:23:20 +03:00
|
|
|
self.cur_ydl: YoutubeDL | None = None
|
2024-05-06 20:07:19 +03:00
|
|
|
self.cur_site = ''
|
2024-05-05 18:23:20 +03:00
|
|
|
|
2024-05-08 12:52:36 +03:00
|
|
|
self.id3pp_obj = id3pp.ID3TagsPP()
|
|
|
|
|
|
|
|
self.logger = logger
|
2024-05-05 18:23:20 +03:00
|
|
|
|
|
|
|
def choose_ydl(self, site: str) -> None:
|
2024-05-03 17:42:53 +03:00
|
|
|
|
|
|
|
ydl = self.ydls[site]
|
2024-05-05 18:23:20 +03:00
|
|
|
cfg = config.get()
|
2024-05-03 19:57:20 +03:00
|
|
|
|
2024-05-03 17:42:53 +03:00
|
|
|
if ydl is None:
|
|
|
|
ydl = create_ydl_fn[site]()
|
2024-05-03 19:57:20 +03:00
|
|
|
|
2024-05-08 12:52:36 +03:00
|
|
|
if self.logger is not None:
|
|
|
|
ydl.params['logger'] = self.logger
|
|
|
|
|
2024-05-04 09:27:57 +03:00
|
|
|
ydl.params['outtmpl']['default'] = cfg.tmpl
|
2024-05-08 12:52:36 +03:00
|
|
|
ydl.add_post_processor(self.id3pp_obj, when='post_process')
|
2024-05-03 19:57:20 +03:00
|
|
|
|
|
|
|
cookies = cfg.cookies_dir / (site + '.txt')
|
|
|
|
if cookies.exists():
|
|
|
|
ydl.params['cookiefile'] = str(cookies)
|
|
|
|
|
2024-05-05 18:23:20 +03:00
|
|
|
self.cur_ydl = ydl
|
2024-05-06 20:07:19 +03:00
|
|
|
self.cur_site = site
|
2024-05-05 18:23:20 +03:00
|
|
|
|
|
|
|
def get_cur_ydl(self) -> YoutubeDL:
|
|
|
|
|
|
|
|
ydl = self.cur_ydl
|
|
|
|
if ydl is None:
|
|
|
|
raise RuntimeError('ydl object not initialized')
|
2024-05-03 17:42:53 +03:00
|
|
|
return ydl
|
|
|
|
|
2024-05-05 18:23:20 +03:00
|
|
|
async def get_playlist_items(self, url: str) -> list[str]:
|
|
|
|
|
|
|
|
return await asyncio.get_event_loop().run_in_executor(
|
|
|
|
None,
|
|
|
|
Downloader._target_get_playlist_items,
|
|
|
|
self.get_cur_ydl(),
|
|
|
|
url,
|
2024-05-06 20:07:19 +03:00
|
|
|
self.cur_site in NP_YDLS,
|
2024-05-05 18:23:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
2024-05-06 20:07:19 +03:00
|
|
|
def _target_get_playlist_items(ydl: YoutubeDL, url: str, process: bool) -> list[str]:
|
2024-05-05 18:23:20 +03:00
|
|
|
|
2024-05-06 20:07:19 +03:00
|
|
|
info = ydl.extract_info(url, download=False, process=process)
|
2024-05-05 18:23:20 +03:00
|
|
|
if info is None:
|
|
|
|
raise RuntimeError('ydl.extract_info returned None')
|
|
|
|
return [
|
|
|
|
entry['track'] if 'track' in entry else entry['title']
|
|
|
|
for entry in info['entries']
|
|
|
|
]
|
|
|
|
|
|
|
|
async def download(
|
|
|
|
self,
|
|
|
|
url: str,
|
|
|
|
playlist_items: Iterable[int] | None = None) -> int:
|
|
|
|
|
|
|
|
return await asyncio.get_event_loop().run_in_executor(
|
|
|
|
None,
|
|
|
|
Downloader._target_download,
|
|
|
|
self.get_cur_ydl(),
|
|
|
|
url,
|
|
|
|
playlist_items,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
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)
|
2024-05-08 12:54:56 +03:00
|
|
|
|
|
|
|
if playlist_items:
|
|
|
|
del ydl.params['playlist_items']
|
2024-05-05 18:23:20 +03:00
|
|
|
|
|
|
|
return ret
|
|
|
|
|
2024-05-03 17:42:53 +03:00
|
|
|
def cleanup(self) -> None:
|
|
|
|
|
|
|
|
for ydl in self.ydls.values():
|
|
|
|
if ydl is not None:
|
|
|
|
ydl.close()
|