2018-10-05 17:58:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-02-08 19:30:29 +01:00
|
|
|
# Copyright 2018-2022 Mike Fährmann
|
2018-10-05 17:58:15 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Downloader module for URLs requiring youtube-dl support"""
|
|
|
|
|
2018-10-19 22:10:59 +02:00
|
|
|
from .common import DownloaderBase
|
2021-11-07 02:44:11 +01:00
|
|
|
from .. import ytdl, text
|
2018-10-05 17:58:15 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
2018-11-16 14:40:05 +01:00
|
|
|
class YoutubeDLDownloader(DownloaderBase):
|
2018-10-05 17:58:15 +02:00
|
|
|
scheme = "ytdl"
|
|
|
|
|
2020-05-18 01:35:53 +02:00
|
|
|
def __init__(self, job):
|
|
|
|
DownloaderBase.__init__(self, job)
|
2018-10-19 22:10:59 +02:00
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
extractor = job.extractor
|
2019-06-30 22:55:31 +02:00
|
|
|
retries = self.config("retries", extractor._retries)
|
2021-11-07 02:44:11 +01:00
|
|
|
self.ytdl_opts = {
|
2019-06-30 22:55:31 +02:00
|
|
|
"retries": retries+1 if retries >= 0 else float("inf"),
|
2018-10-19 22:10:59 +02:00
|
|
|
"socket_timeout": self.config("timeout", extractor._timeout),
|
|
|
|
"nocheckcertificate": not self.config("verify", extractor._verify),
|
2022-03-10 23:32:16 +01:00
|
|
|
"proxy": self.proxies.get("http") if self.proxies else None,
|
2018-10-19 22:10:59 +02:00
|
|
|
}
|
2021-07-10 20:47:33 +02:00
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
self.ytdl_instance = None
|
2020-05-12 20:13:04 +02:00
|
|
|
self.forward_cookies = self.config("forward-cookies", False)
|
2021-11-07 02:44:11 +01:00
|
|
|
self.progress = self.config("progress", 3.0)
|
2021-03-01 03:10:42 +01:00
|
|
|
self.outtmpl = self.config("outtmpl")
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
def download(self, url, pathfmt):
|
2021-07-10 20:47:33 +02:00
|
|
|
kwdict = pathfmt.kwdict
|
2021-07-16 03:42:13 +02:00
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
ytdl_instance = kwdict.pop("_ytdl_instance", None)
|
|
|
|
if not ytdl_instance:
|
|
|
|
ytdl_instance = self.ytdl_instance
|
|
|
|
if not ytdl_instance:
|
2022-02-08 19:30:29 +01:00
|
|
|
try:
|
|
|
|
module = ytdl.import_module(self.config("module"))
|
|
|
|
except ImportError as exc:
|
|
|
|
self.log.error("Cannot import module '%s'", exc.name)
|
|
|
|
self.log.debug("", exc_info=True)
|
|
|
|
self.download = lambda u, p: False
|
|
|
|
return False
|
2021-11-07 02:44:11 +01:00
|
|
|
self.ytdl_instance = ytdl_instance = ytdl.construct_YoutubeDL(
|
|
|
|
module, self, self.ytdl_opts)
|
|
|
|
if self.outtmpl == "default":
|
|
|
|
self.outtmpl = module.DEFAULT_OUTTMPL
|
2021-07-16 03:42:13 +02:00
|
|
|
if self.forward_cookies:
|
2021-11-07 02:44:11 +01:00
|
|
|
set_cookie = ytdl_instance.cookiejar.set_cookie
|
2021-07-16 03:42:13 +02:00
|
|
|
for cookie in self.session.cookies:
|
|
|
|
set_cookie(cookie)
|
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
if self.progress is not None and not ytdl_instance._progress_hooks:
|
|
|
|
ytdl_instance.add_progress_hook(self._progress_hook)
|
|
|
|
|
2021-07-10 20:47:33 +02:00
|
|
|
info_dict = kwdict.pop("_ytdl_info_dict", None)
|
|
|
|
if not info_dict:
|
|
|
|
try:
|
2021-11-07 02:44:11 +01:00
|
|
|
info_dict = ytdl_instance.extract_info(url[5:], download=False)
|
2021-07-10 20:47:33 +02:00
|
|
|
except Exception:
|
2023-03-06 12:25:12 +01:00
|
|
|
pass
|
|
|
|
if not info_dict:
|
2021-07-10 20:47:33 +02:00
|
|
|
return False
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
if "entries" in info_dict:
|
2021-07-10 20:47:33 +02:00
|
|
|
index = kwdict.get("_ytdl_index")
|
2019-03-24 11:27:20 +01:00
|
|
|
if index is None:
|
2021-11-07 02:44:11 +01:00
|
|
|
return self._download_playlist(
|
|
|
|
ytdl_instance, pathfmt, info_dict)
|
2019-03-24 11:27:20 +01:00
|
|
|
else:
|
|
|
|
info_dict = info_dict["entries"][index]
|
2019-10-25 13:17:13 +02:00
|
|
|
|
2021-07-10 20:47:33 +02:00
|
|
|
extra = kwdict.get("_ytdl_extra")
|
2019-10-25 13:17:13 +02:00
|
|
|
if extra:
|
|
|
|
info_dict.update(extra)
|
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
return self._download_video(ytdl_instance, pathfmt, info_dict)
|
2018-10-05 17:58:15 +02:00
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
def _download_video(self, ytdl_instance, pathfmt, info_dict):
|
2019-05-31 14:56:45 +02:00
|
|
|
if "url" in info_dict:
|
2019-08-12 21:40:37 +02:00
|
|
|
text.nameext_from_url(info_dict["url"], pathfmt.kwdict)
|
2019-08-24 22:39:37 +02:00
|
|
|
|
2020-05-13 22:35:33 +02:00
|
|
|
formats = info_dict.get("requested_formats")
|
|
|
|
if formats and not compatible_formats(formats):
|
|
|
|
info_dict["ext"] = "mkv"
|
|
|
|
|
2019-08-24 22:39:37 +02:00
|
|
|
if self.outtmpl:
|
2021-11-07 02:44:11 +01:00
|
|
|
self._set_outtmpl(ytdl_instance, self.outtmpl)
|
|
|
|
pathfmt.filename = filename = \
|
|
|
|
ytdl_instance.prepare_filename(info_dict)
|
2019-08-24 22:39:37 +02:00
|
|
|
pathfmt.extension = info_dict["ext"]
|
|
|
|
pathfmt.path = pathfmt.directory + filename
|
|
|
|
pathfmt.realpath = pathfmt.temppath = (
|
|
|
|
pathfmt.realdirectory + filename)
|
|
|
|
else:
|
|
|
|
pathfmt.set_extension(info_dict["ext"])
|
2022-11-08 17:01:10 +01:00
|
|
|
pathfmt.build_path()
|
2019-08-24 22:39:37 +02:00
|
|
|
|
2018-10-05 17:58:15 +02:00
|
|
|
if pathfmt.exists():
|
|
|
|
pathfmt.temppath = ""
|
|
|
|
return True
|
2018-10-19 22:10:59 +02:00
|
|
|
if self.part and self.partdir:
|
2018-10-05 17:58:15 +02:00
|
|
|
pathfmt.temppath = os.path.join(
|
|
|
|
self.partdir, pathfmt.filename)
|
2021-07-16 02:14:59 +02:00
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
self._set_outtmpl(ytdl_instance, pathfmt.temppath.replace("%", "%%"))
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
self.out.start(pathfmt.path)
|
|
|
|
try:
|
2021-11-07 02:44:11 +01:00
|
|
|
ytdl_instance.process_info(info_dict)
|
2018-10-05 17:58:15 +02:00
|
|
|
except Exception:
|
2018-10-19 22:10:59 +02:00
|
|
|
self.log.debug("Traceback", exc_info=True)
|
2018-10-05 17:58:15 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-11-07 02:44:11 +01:00
|
|
|
def _download_playlist(self, ytdl_instance, pathfmt, info_dict):
|
2018-10-05 17:58:15 +02:00
|
|
|
pathfmt.set_extension("%(playlist_index)s.%(ext)s")
|
2022-11-08 17:01:10 +01:00
|
|
|
pathfmt.build_path()
|
2021-11-07 02:44:11 +01:00
|
|
|
self._set_outtmpl(ytdl_instance, pathfmt.realpath)
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
for entry in info_dict["entries"]:
|
2021-11-07 02:44:11 +01:00
|
|
|
ytdl_instance.process_info(entry)
|
2018-10-05 17:58:15 +02:00
|
|
|
return True
|
2018-11-16 14:40:05 +01:00
|
|
|
|
2021-09-28 22:37:11 +02:00
|
|
|
def _progress_hook(self, info):
|
|
|
|
if info["status"] == "downloading" and \
|
|
|
|
info["elapsed"] >= self.progress:
|
2021-10-21 22:57:04 +02:00
|
|
|
total = info.get("total_bytes") or info.get("total_bytes_estimate")
|
2021-11-12 17:35:53 +01:00
|
|
|
speed = info.get("speed")
|
2021-09-28 22:37:11 +02:00
|
|
|
self.out.progress(
|
2021-10-21 22:57:04 +02:00
|
|
|
None if total is None else int(total),
|
2021-09-28 22:37:11 +02:00
|
|
|
info["downloaded_bytes"],
|
2021-11-12 17:35:53 +01:00
|
|
|
int(speed) if speed else 0,
|
2021-09-28 22:37:11 +02:00
|
|
|
)
|
|
|
|
|
2021-07-16 03:42:13 +02:00
|
|
|
@staticmethod
|
2021-11-07 02:44:11 +01:00
|
|
|
def _set_outtmpl(ytdl_instance, outtmpl):
|
2021-07-16 02:14:59 +02:00
|
|
|
try:
|
2022-06-19 12:11:48 +02:00
|
|
|
ytdl_instance._parse_outtmpl
|
2021-07-16 02:14:59 +02:00
|
|
|
except AttributeError:
|
2022-06-19 12:11:48 +02:00
|
|
|
try:
|
|
|
|
ytdl_instance.outtmpl_dict["default"] = outtmpl
|
|
|
|
except AttributeError:
|
|
|
|
ytdl_instance.params["outtmpl"] = outtmpl
|
|
|
|
else:
|
|
|
|
ytdl_instance.params["outtmpl"] = {"default": outtmpl}
|
2021-07-16 02:14:59 +02:00
|
|
|
|
2018-11-16 14:40:05 +01:00
|
|
|
|
2020-05-13 22:35:33 +02:00
|
|
|
def compatible_formats(formats):
|
2021-07-10 20:47:33 +02:00
|
|
|
"""Returns True if 'formats' are compatible for merge"""
|
2020-05-13 22:35:33 +02:00
|
|
|
video_ext = formats[0].get("ext")
|
|
|
|
audio_ext = formats[1].get("ext")
|
|
|
|
|
|
|
|
if video_ext == "webm" and audio_ext == "webm":
|
|
|
|
return True
|
|
|
|
|
|
|
|
exts = ("mp3", "mp4", "m4a", "m4p", "m4b", "m4r", "m4v", "ismv", "isma")
|
|
|
|
return video_ext in exts and audio_ext in exts
|
|
|
|
|
|
|
|
|
2018-11-16 14:40:05 +01:00
|
|
|
__downloader__ = YoutubeDLDownloader
|