mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-26 12:42:29 +01:00
[ytdl] transfer YoutubeDL objects to downloader (#1680)
allows specifying downloader-specific options per subcategory but overwrites all downloader.ytdl settings
This commit is contained in:
parent
06e69ea79a
commit
f5b097165e
@ -53,23 +53,27 @@ class YoutubeDLDownloader(DownloaderBase):
|
|||||||
self.ytdl = module.YoutubeDL(options)
|
self.ytdl = module.YoutubeDL(options)
|
||||||
|
|
||||||
def download(self, url, pathfmt):
|
def download(self, url, pathfmt):
|
||||||
if self.forward_cookies:
|
|
||||||
set_cookie = self.ytdl.cookiejar.set_cookie
|
|
||||||
for cookie in self.session.cookies:
|
|
||||||
set_cookie(cookie)
|
|
||||||
|
|
||||||
kwdict = pathfmt.kwdict
|
kwdict = pathfmt.kwdict
|
||||||
|
|
||||||
|
ytdl = kwdict.pop("_ytdl_instance", None)
|
||||||
|
if not ytdl:
|
||||||
|
ytdl = self.ytdl
|
||||||
|
if self.forward_cookies:
|
||||||
|
set_cookie = ytdl.cookiejar.set_cookie
|
||||||
|
for cookie in self.session.cookies:
|
||||||
|
set_cookie(cookie)
|
||||||
|
|
||||||
info_dict = kwdict.pop("_ytdl_info_dict", None)
|
info_dict = kwdict.pop("_ytdl_info_dict", None)
|
||||||
if not info_dict:
|
if not info_dict:
|
||||||
try:
|
try:
|
||||||
info_dict = self.ytdl.extract_info(url[5:], download=False)
|
info_dict = ytdl.extract_info(url[5:], download=False)
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "entries" in info_dict:
|
if "entries" in info_dict:
|
||||||
index = kwdict.get("_ytdl_index")
|
index = kwdict.get("_ytdl_index")
|
||||||
if index is None:
|
if index is None:
|
||||||
return self._download_playlist(pathfmt, info_dict)
|
return self._download_playlist(ytdl, pathfmt, info_dict)
|
||||||
else:
|
else:
|
||||||
info_dict = info_dict["entries"][index]
|
info_dict = info_dict["entries"][index]
|
||||||
|
|
||||||
@ -77,9 +81,9 @@ class YoutubeDLDownloader(DownloaderBase):
|
|||||||
if extra:
|
if extra:
|
||||||
info_dict.update(extra)
|
info_dict.update(extra)
|
||||||
|
|
||||||
return self._download_video(pathfmt, info_dict)
|
return self._download_video(ytdl, pathfmt, info_dict)
|
||||||
|
|
||||||
def _download_video(self, pathfmt, info_dict):
|
def _download_video(self, ytdl, pathfmt, info_dict):
|
||||||
if "url" in info_dict:
|
if "url" in info_dict:
|
||||||
text.nameext_from_url(info_dict["url"], pathfmt.kwdict)
|
text.nameext_from_url(info_dict["url"], pathfmt.kwdict)
|
||||||
|
|
||||||
@ -88,8 +92,8 @@ class YoutubeDLDownloader(DownloaderBase):
|
|||||||
info_dict["ext"] = "mkv"
|
info_dict["ext"] = "mkv"
|
||||||
|
|
||||||
if self.outtmpl:
|
if self.outtmpl:
|
||||||
self._set_outtmpl(self.outtmpl)
|
self._set_outtmpl(ytdl, self.outtmpl)
|
||||||
pathfmt.filename = filename = self.ytdl.prepare_filename(info_dict)
|
pathfmt.filename = filename = ytdl.prepare_filename(info_dict)
|
||||||
pathfmt.extension = info_dict["ext"]
|
pathfmt.extension = info_dict["ext"]
|
||||||
pathfmt.path = pathfmt.directory + filename
|
pathfmt.path = pathfmt.directory + filename
|
||||||
pathfmt.realpath = pathfmt.temppath = (
|
pathfmt.realpath = pathfmt.temppath = (
|
||||||
@ -104,29 +108,30 @@ class YoutubeDLDownloader(DownloaderBase):
|
|||||||
pathfmt.temppath = os.path.join(
|
pathfmt.temppath = os.path.join(
|
||||||
self.partdir, pathfmt.filename)
|
self.partdir, pathfmt.filename)
|
||||||
|
|
||||||
self._set_outtmpl(pathfmt.temppath.replace("%", "%%"))
|
self._set_outtmpl(ytdl, pathfmt.temppath.replace("%", "%%"))
|
||||||
|
|
||||||
self.out.start(pathfmt.path)
|
self.out.start(pathfmt.path)
|
||||||
try:
|
try:
|
||||||
self.ytdl.process_info(info_dict)
|
ytdl.process_info(info_dict)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.log.debug("Traceback", exc_info=True)
|
self.log.debug("Traceback", exc_info=True)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _download_playlist(self, pathfmt, info_dict):
|
def _download_playlist(self, ytdl, pathfmt, info_dict):
|
||||||
pathfmt.set_extension("%(playlist_index)s.%(ext)s")
|
pathfmt.set_extension("%(playlist_index)s.%(ext)s")
|
||||||
self._set_outtmpl(pathfmt.realpath)
|
self._set_outtmpl(ytdl, pathfmt.realpath)
|
||||||
|
|
||||||
for entry in info_dict["entries"]:
|
for entry in info_dict["entries"]:
|
||||||
self.ytdl.process_info(entry)
|
ytdl.process_info(entry)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _set_outtmpl(self, outtmpl):
|
@staticmethod
|
||||||
|
def _set_outtmpl(ytdl, outtmpl):
|
||||||
try:
|
try:
|
||||||
self.ytdl.outtmpl_dict["default"] = outtmpl
|
ytdl.outtmpl_dict["default"] = outtmpl
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self.ytdl.params["outtmpl"] = outtmpl
|
ytdl.params["outtmpl"] = outtmpl
|
||||||
|
|
||||||
|
|
||||||
def compatible_formats(formats):
|
def compatible_formats(formats):
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
"""Extractors for sites supported by youtube-dl"""
|
"""Extractors for sites supported by youtube-dl"""
|
||||||
|
|
||||||
from .common import Extractor, Message
|
from .common import Extractor, Message
|
||||||
from .. import config, exception
|
from .. import text, config, exception
|
||||||
|
|
||||||
|
|
||||||
class YoutubeDLExtractor(Extractor):
|
class YoutubeDLExtractor(Extractor):
|
||||||
@ -55,11 +55,20 @@ class YoutubeDLExtractor(Extractor):
|
|||||||
|
|
||||||
# construct YoutubeDL object
|
# construct YoutubeDL object
|
||||||
options = {
|
options = {
|
||||||
"format": self.config("format"),
|
"format" : self.config("format"),
|
||||||
"socket_timeout": self._timeout,
|
"retries" : self._retries,
|
||||||
"nocheckcertificate": not self._verify,
|
"socket_timeout" : self._timeout,
|
||||||
"proxy": self.session.proxies.get("http"),
|
"nocheckcertificate" : not self._verify,
|
||||||
|
"proxy" : self.session.proxies.get("http"),
|
||||||
"force_generic_extractor": self.force_generic_extractor,
|
"force_generic_extractor": self.force_generic_extractor,
|
||||||
|
"nopart" : not self.config("part", True),
|
||||||
|
"updatetime" : self.config("mtime", True),
|
||||||
|
"ratelimit" : text.parse_bytes(
|
||||||
|
self.config("rate"), None),
|
||||||
|
"min_filesize" : text.parse_bytes(
|
||||||
|
self.config("filesize-min"), None),
|
||||||
|
"max_filesize" : text.parse_bytes(
|
||||||
|
self.config("filesize-max"), None),
|
||||||
}
|
}
|
||||||
|
|
||||||
raw_options = self.config("raw-options")
|
raw_options = self.config("raw-options")
|
||||||
@ -98,6 +107,7 @@ class YoutubeDLExtractor(Extractor):
|
|||||||
for info_dict in results:
|
for info_dict in results:
|
||||||
info_dict["extension"] = None
|
info_dict["extension"] = None
|
||||||
info_dict["_ytdl_info_dict"] = info_dict
|
info_dict["_ytdl_info_dict"] = info_dict
|
||||||
|
info_dict["_ytdl_instance"] = ytdl
|
||||||
|
|
||||||
url = "ytdl:" + (info_dict.get("url") or
|
url = "ytdl:" + (info_dict.get("url") or
|
||||||
info_dict.get("webpage_url") or
|
info_dict.get("webpage_url") or
|
||||||
|
Loading…
Reference in New Issue
Block a user