| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for sites supported by youtube-dl""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import ytdl, config |
| 13 |
|
| 14 |
|
| 15 |
class YoutubeDLExtractor(Extractor): |
| 16 |
"""Generic extractor for youtube-dl supported URLs""" |
| 17 |
category = "ytdl" |
| 18 |
directory_fmt = ("{category}", "{subcategory}") |
| 19 |
filename_fmt = "{title}-{id}.{extension}" |
| 20 |
archive_fmt = "{extractor_key} {id}" |
| 21 |
pattern = r"ytdl:(.*)" |
| 22 |
example = "ytdl:https://www.youtube.com/watch?v=abcdefghijk" |
| 23 |
|
| 24 |
def __init__(self, match): |
| 25 |
|
| 26 |
ytdl_module = ytdl.import_module(config.get( |
| 27 |
("extractor", "ytdl"), "module")) |
| 28 |
self.ytdl_module_name = ytdl_module.__name__ |
| 29 |
|
| 30 |
|
| 31 |
self.ytdl_url = url = match[1] |
| 32 |
generic = config.interpolate(("extractor", "ytdl"), "generic", True) |
| 33 |
if generic == "force": |
| 34 |
self.ytdl_ie_key = "Generic" |
| 35 |
self.force_generic_extractor = True |
| 36 |
else: |
| 37 |
for ie in ytdl_module.extractor.gen_extractor_classes(): |
| 38 |
if ie.suitable(url): |
| 39 |
self.ytdl_ie_key = ie.ie_key() |
| 40 |
break |
| 41 |
if not generic and self.ytdl_ie_key == "Generic": |
| 42 |
raise self.exc.NoExtractorError() |
| 43 |
self.force_generic_extractor = False |
| 44 |
|
| 45 |
if self.ytdl_ie_key == "Generic" and config.interpolate( |
| 46 |
("extractor", "ytdl"), "generic-category", True): |
| 47 |
|
| 48 |
self.category = "ytdl-generic" |
| 49 |
self.subcategory = url[url.rfind("/", None, 8)+1:url.find("/", 8)] |
| 50 |
else: |
| 51 |
|
| 52 |
self.subcategory = self.ytdl_ie_key |
| 53 |
Extractor.__init__(self, match) |
| 54 |
|
| 55 |
def items(self): |
| 56 |
|
| 57 |
ytdl_module = ytdl.import_module( |
| 58 |
config.get(("extractor", "ytdl", self.subcategory), "module") or |
| 59 |
self.ytdl_module_name) |
| 60 |
self.log.debug("Using %s", ytdl_module) |
| 61 |
|
| 62 |
|
| 63 |
extr_opts = { |
| 64 |
"extract_flat" : "in_playlist", |
| 65 |
"force_generic_extractor": self.force_generic_extractor, |
| 66 |
} |
| 67 |
user_opts = { |
| 68 |
"retries" : self._retries, |
| 69 |
"socket_timeout" : self._timeout, |
| 70 |
"nocheckcertificate" : not self._verify, |
| 71 |
} |
| 72 |
|
| 73 |
if self._proxies: |
| 74 |
user_opts["proxy"] = self._proxies.get("http") |
| 75 |
|
| 76 |
username, password = self._get_auth_info() |
| 77 |
if username: |
| 78 |
user_opts["username"], user_opts["password"] = username, password |
| 79 |
del username, password |
| 80 |
|
| 81 |
ytdl_instance = ytdl.construct_YoutubeDL( |
| 82 |
ytdl_module, self, user_opts, extr_opts) |
| 83 |
|
| 84 |
|
| 85 |
if cookies := self.cookies: |
| 86 |
set_cookie = ytdl_instance.cookiejar.set_cookie |
| 87 |
for cookie in cookies: |
| 88 |
set_cookie(cookie) |
| 89 |
|
| 90 |
|
| 91 |
try: |
| 92 |
info_dict = ytdl_instance._YoutubeDL__extract_info( |
| 93 |
self.ytdl_url, |
| 94 |
ytdl_instance.get_info_extractor(self.ytdl_ie_key), |
| 95 |
False, {}, True) |
| 96 |
|
| 97 |
|
| 98 |
except self.exc.ControlException: |
| 99 |
raise |
| 100 |
except Exception as exc: |
| 101 |
raise self.exc.AbortExtraction( |
| 102 |
f"Failed to extract video data " |
| 103 |
f"({exc.__class__.__name__}: {exc})") |
| 104 |
|
| 105 |
if not info_dict: |
| 106 |
return |
| 107 |
elif "entries" in info_dict: |
| 108 |
results = self._process_entries( |
| 109 |
ytdl_module, ytdl_instance, info_dict["entries"]) |
| 110 |
else: |
| 111 |
results = (info_dict,) |
| 112 |
|
| 113 |
|
| 114 |
for info_dict in results: |
| 115 |
info_dict["extension"] = None |
| 116 |
info_dict["_ytdl_info_dict"] = info_dict |
| 117 |
info_dict["_ytdl_instance"] = ytdl_instance |
| 118 |
|
| 119 |
url = "ytdl:" + (info_dict.get("url") or |
| 120 |
info_dict.get("webpage_url") or |
| 121 |
self.ytdl_url) |
| 122 |
|
| 123 |
yield Message.Directory, "", info_dict |
| 124 |
yield Message.Url, url, info_dict |
| 125 |
|
| 126 |
def _process_entries(self, ytdl_module, ytdl_instance, entries): |
| 127 |
for entry in entries: |
| 128 |
if not entry: |
| 129 |
continue |
| 130 |
|
| 131 |
if entry.get("_type") in {"url", "url_transparent"}: |
| 132 |
try: |
| 133 |
entry = ytdl_instance.extract_info( |
| 134 |
entry["url"], False, |
| 135 |
ie_key=entry.get("ie_key")) |
| 136 |
except self.exc.ControlException: |
| 137 |
raise |
| 138 |
except Exception as exc: |
| 139 |
if ytdl_instance.params.get("ignoreerrors"): |
| 140 |
continue |
| 141 |
raise self.exc.AbortExtraction( |
| 142 |
f"Failed to extract playlist entry " |
| 143 |
f"({exc.__class__.__name__}: {exc})") |
| 144 |
if not entry: |
| 145 |
continue |
| 146 |
|
| 147 |
if "entries" in entry: |
| 148 |
yield from self._process_entries( |
| 149 |
ytdl_module, ytdl_instance, entry["entries"]) |
| 150 |
else: |
| 151 |
yield entry |
| 152 |
|
| 153 |
|
| 154 |
if config.get(("extractor", "ytdl"), "enabled"): |
| 155 |
|
| 156 |
YoutubeDLExtractor.pattern = r"(?:ytdl:)?(.*)" |