2018-10-05 17:58:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-03-01 03:10:42 +01:00
|
|
|
# Copyright 2018-2021 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
|
|
|
|
from .. import 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"
|
2021-03-01 03:10:42 +01:00
|
|
|
module = None
|
2018-10-05 17:58:15 +02:00
|
|
|
|
2020-05-18 01:35:53 +02:00
|
|
|
def __init__(self, job):
|
2021-03-01 03:10:42 +01:00
|
|
|
module = self.module
|
|
|
|
if not module:
|
|
|
|
module_name = self.config("module") or "youtube_dl"
|
|
|
|
module = YoutubeDLDownloader.module = __import__(module_name)
|
|
|
|
|
2020-05-18 01:35:53 +02:00
|
|
|
DownloaderBase.__init__(self, job)
|
|
|
|
extractor = job.extractor
|
2018-10-19 22:10:59 +02:00
|
|
|
|
2019-06-30 22:55:31 +02:00
|
|
|
retries = self.config("retries", extractor._retries)
|
2018-10-19 22:10:59 +02:00
|
|
|
options = {
|
|
|
|
"format": self.config("format") or None,
|
|
|
|
"ratelimit": text.parse_bytes(self.config("rate"), None),
|
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),
|
|
|
|
"nopart": not self.part,
|
2019-06-20 17:19:44 +02:00
|
|
|
"updatetime": self.config("mtime", True),
|
2019-11-05 16:16:26 +01:00
|
|
|
"proxy": extractor.session.proxies.get("http"),
|
2020-09-01 22:05:17 +02:00
|
|
|
"min_filesize": text.parse_bytes(
|
|
|
|
self.config("filesize-min"), None),
|
|
|
|
"max_filesize": text.parse_bytes(
|
|
|
|
self.config("filesize-max"), None),
|
2018-10-19 22:10:59 +02:00
|
|
|
}
|
|
|
|
options.update(self.config("raw-options") or {})
|
|
|
|
|
|
|
|
if self.config("logging", True):
|
|
|
|
options["logger"] = self.log
|
2020-05-12 20:13:04 +02:00
|
|
|
self.forward_cookies = self.config("forward-cookies", False)
|
2018-10-19 22:10:59 +02:00
|
|
|
|
2021-03-01 03:10:42 +01:00
|
|
|
self.outtmpl = self.config("outtmpl")
|
|
|
|
if self.outtmpl == "default":
|
|
|
|
self.outtmpl = module.DEFAULT_OUTTMPL
|
2019-08-24 22:39:37 +02:00
|
|
|
|
2021-03-01 03:10:42 +01:00
|
|
|
self.ytdl = module.YoutubeDL(options)
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
def download(self, url, pathfmt):
|
2019-07-24 21:19:11 +02:00
|
|
|
if self.forward_cookies:
|
|
|
|
set_cookie = self.ytdl.cookiejar.set_cookie
|
|
|
|
for cookie in self.session.cookies:
|
|
|
|
set_cookie(cookie)
|
2019-06-26 19:32:07 +02:00
|
|
|
|
2018-10-05 17:58:15 +02:00
|
|
|
try:
|
|
|
|
info_dict = self.ytdl.extract_info(url[5:], download=False)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if "entries" in info_dict:
|
2019-08-12 21:40:37 +02:00
|
|
|
index = pathfmt.kwdict.get("_ytdl_index")
|
2019-03-24 11:27:20 +01:00
|
|
|
if index is None:
|
|
|
|
return self._download_playlist(pathfmt, info_dict)
|
|
|
|
else:
|
|
|
|
info_dict = info_dict["entries"][index]
|
2019-10-25 13:17:13 +02:00
|
|
|
|
|
|
|
extra = pathfmt.kwdict.get("_ytdl_extra")
|
|
|
|
if extra:
|
|
|
|
info_dict.update(extra)
|
|
|
|
|
2018-10-05 17:58:15 +02:00
|
|
|
return self._download_video(pathfmt, info_dict)
|
|
|
|
|
|
|
|
def _download_video(self, 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:
|
|
|
|
self.ytdl.params["outtmpl"] = self.outtmpl
|
|
|
|
pathfmt.filename = filename = self.ytdl.prepare_filename(info_dict)
|
|
|
|
pathfmt.extension = info_dict["ext"]
|
|
|
|
pathfmt.path = pathfmt.directory + filename
|
|
|
|
pathfmt.realpath = pathfmt.temppath = (
|
|
|
|
pathfmt.realdirectory + filename)
|
|
|
|
else:
|
|
|
|
pathfmt.set_extension(info_dict["ext"])
|
|
|
|
|
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)
|
2018-10-19 22:10:59 +02:00
|
|
|
self.ytdl.params["outtmpl"] = pathfmt.temppath.replace("%", "%%")
|
2018-10-05 17:58:15 +02:00
|
|
|
|
|
|
|
self.out.start(pathfmt.path)
|
|
|
|
try:
|
|
|
|
self.ytdl.process_info(info_dict)
|
|
|
|
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
|
|
|
|
|
|
|
|
def _download_playlist(self, pathfmt, info_dict):
|
|
|
|
pathfmt.set_extension("%(playlist_index)s.%(ext)s")
|
|
|
|
self.ytdl.params["outtmpl"] = pathfmt.realpath
|
|
|
|
|
|
|
|
for entry in info_dict["entries"]:
|
|
|
|
self.ytdl.process_info(entry)
|
|
|
|
return True
|
2018-11-16 14:40:05 +01:00
|
|
|
|
|
|
|
|
2020-05-13 22:35:33 +02:00
|
|
|
def compatible_formats(formats):
|
|
|
|
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
|