]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/archiveorg.py
[ie/lbry] Add playlist support (#8213)
[yt-dlp.git] / yt_dlp / extractor / archiveorg.py
CommitLineData
a3e26449 1import json
ac668111 2import re
3import urllib.parse
4
a4a554a7 5from .common import InfoExtractor
ac668111 6from .youtube import YoutubeBaseInfoExtractor, YoutubeIE
3d2623a8 7from ..compat import compat_urllib_parse_unquote
8from ..networking import HEADRequest
9from ..networking.exceptions import HTTPError
d50aca41 10from ..utils import (
ac668111 11 KNOWN_EXTENSIONS,
12 ExtractorError,
aa4b0545 13 bug_reports_message,
d50aca41 14 clean_html,
a3e26449 15 dict_get,
879e7199 16 extract_attributes,
aa4b0545 17 get_element_by_id,
a3e26449 18 int_or_none,
1f13021e 19 join_nonempty,
217753f4 20 js_to_json,
a3e26449 21 merge_dicts,
879e7199 22 mimetype2ext,
aa4b0545 23 orderedSet,
879e7199 24 parse_duration,
4dfbf869 25 parse_qs,
879e7199 26 str_or_none,
ac668111 27 str_to_int,
aa4b0545 28 traverse_obj,
879e7199 29 try_get,
30 unified_strdate,
31 unified_timestamp,
ac668111 32 url_or_none,
aa4b0545 33 urlhandle_detect_ext,
d50aca41 34)
5fe3a3c3
PH
35
36
a4a554a7 37class ArchiveOrgIE(InfoExtractor):
5fe3a3c3 38 IE_NAME = 'archive.org'
a3e26449 39 IE_DESC = 'archive.org video and audio'
40 _VALID_URL = r'https?://(?:www\.)?archive\.org/(?:details|embed)/(?P<id>[^?#]+)(?:[?].*)?$'
e8e28989
S
41 _TESTS = [{
42 'url': 'http://archive.org/details/XD300-23_68HighlightsAResearchCntAugHumanIntellect',
3798eadc
PH
43 'md5': '8af1d4cf447933ed3c7f4871162602db',
44 'info_dict': {
e8e28989 45 'id': 'XD300-23_68HighlightsAResearchCntAugHumanIntellect',
a3e26449 46 'ext': 'ogv',
e8e28989 47 'title': '1968 Demo - FJCC Conference Presentation Reel #1',
d50aca41 48 'description': 'md5:da45c349df039f1cc8075268eb1b5c25',
a3e26449 49 'release_date': '19681210',
50 'timestamp': 1268695290,
51 'upload_date': '20100315',
52 'creator': 'SRI International',
53 'uploader': 'laura@archive.org',
871a8929 54 'thumbnail': r're:https://archive\.org/download/.*\.jpg',
55 'release_year': 1968,
56 'display_id': 'XD300-23_68HighlightsAResearchCntAugHumanIntellect.cdr',
57 'track': 'XD300-23 68HighlightsAResearchCntAugHumanIntellect',
58
a3e26449 59 },
e8e28989
S
60 }, {
61 'url': 'https://archive.org/details/Cops1922',
c12b4b80 62 'md5': '0869000b4ce265e8ca62738b336b268a',
e8e28989
S
63 'info_dict': {
64 'id': 'Cops1922',
d50aca41 65 'ext': 'mp4',
e8e28989 66 'title': 'Buster Keaton\'s "Cops" (1922)',
871a8929 67 'description': 'md5:cd6f9910c35aedd5fc237dbc3957e2ca',
a3e26449 68 'uploader': 'yorkmba99@hotmail.com',
69 'timestamp': 1387699629,
1f13021e 70 'upload_date': '20131222',
871a8929 71 'display_id': 'Cops-v2.mp4',
72 'thumbnail': r're:https://archive\.org/download/.*\.jpg',
73 'duration': 1091.96,
a3e26449 74 },
d50aca41
RA
75 }, {
76 'url': 'http://archive.org/embed/XD300-23_68HighlightsAResearchCntAugHumanIntellect',
77 'only_matching': True,
a3e26449 78 }, {
79 'url': 'https://archive.org/details/Election_Ads',
871a8929 80 'md5': 'eec5cddebd4793c6a653b69c3b11f2e6',
a3e26449 81 'info_dict': {
82 'id': 'Election_Ads/Commercial-JFK1960ElectionAdCampaignJingle.mpg',
83 'title': 'Commercial-JFK1960ElectionAdCampaignJingle.mpg',
871a8929 84 'ext': 'mpg',
85 'thumbnail': r're:https://archive\.org/download/.*\.jpg',
86 'duration': 59.77,
87 'display_id': 'Commercial-JFK1960ElectionAdCampaignJingle.mpg',
a3e26449 88 },
89 }, {
90 'url': 'https://archive.org/details/Election_Ads/Commercial-Nixon1960ElectionAdToughonDefense.mpg',
871a8929 91 'md5': 'ea1eed8234e7d4165f38c8c769edef38',
a3e26449 92 'info_dict': {
93 'id': 'Election_Ads/Commercial-Nixon1960ElectionAdToughonDefense.mpg',
94 'title': 'Commercial-Nixon1960ElectionAdToughonDefense.mpg',
871a8929 95 'ext': 'mpg',
a3e26449 96 'timestamp': 1205588045,
97 'uploader': 'mikedavisstripmaster@yahoo.com',
98 'description': '1960 Presidential Campaign Election Commercials John F Kennedy, Richard M Nixon',
99 'upload_date': '20080315',
871a8929 100 'display_id': 'Commercial-Nixon1960ElectionAdToughonDefense.mpg',
101 'duration': 59.51,
102 'license': 'http://creativecommons.org/licenses/publicdomain/',
103 'thumbnail': r're:https://archive\.org/download/.*\.jpg',
a3e26449 104 },
105 }, {
106 'url': 'https://archive.org/details/gd1977-05-08.shure57.stevenson.29303.flac16',
107 'md5': '7d07ffb42aba6537c28e053efa4b54c9',
108 'info_dict': {
109 'id': 'gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t01.flac',
110 'title': 'Turning',
111 'ext': 'flac',
871a8929 112 'track': 'Turning',
113 'creator': 'Grateful Dead',
114 'display_id': 'gd1977-05-08d01t01.flac',
115 'track_number': 1,
116 'album': '1977-05-08 - Barton Hall - Cornell University',
117 'duration': 39.8,
a3e26449 118 },
119 }, {
120 'url': 'https://archive.org/details/gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t07.flac',
121 'md5': 'a07cd8c6ab4ee1560f8a0021717130f3',
122 'info_dict': {
123 'id': 'gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t07.flac',
124 'title': 'Deal',
125 'ext': 'flac',
126 'timestamp': 1205895624,
127 'uploader': 'mvernon54@yahoo.com',
871a8929 128 'description': 'md5:6c921464414814720c6593810a5c7e3d',
a3e26449 129 'upload_date': '20080319',
130 'location': 'Barton Hall - Cornell University',
871a8929 131 'duration': 438.68,
132 'track': 'Deal',
133 'creator': 'Grateful Dead',
134 'album': '1977-05-08 - Barton Hall - Cornell University',
135 'release_date': '19770508',
136 'display_id': 'gd1977-05-08d01t07.flac',
137 'release_year': 1977,
138 'track_number': 7,
a3e26449 139 },
140 }, {
871a8929 141 # FIXME: give a better error message than just IndexError when all available formats are restricted
a3e26449 142 'url': 'https://archive.org/details/lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik',
143 'md5': '7cb019baa9b332e82ea7c10403acd180',
144 'info_dict': {
145 'id': 'lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik/disc1/01.01. Bells Of Rostov.mp3',
146 'title': 'Bells Of Rostov',
147 'ext': 'mp3',
148 },
871a8929 149 'skip': 'restricted'
a3e26449 150 }, {
151 'url': 'https://archive.org/details/lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik/disc1/02.02.+Song+And+Chorus+In+The+Polovetsian+Camp+From+%22Prince+Igor%22+(Act+2%2C+Scene+1).mp3',
152 'md5': '1d0aabe03edca83ca58d9ed3b493a3c3',
153 'info_dict': {
154 'id': 'lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik/disc1/02.02. Song And Chorus In The Polovetsian Camp From "Prince Igor" (Act 2, Scene 1).mp3',
155 'title': 'Song And Chorus In The Polovetsian Camp From "Prince Igor" (Act 2, Scene 1)',
156 'ext': 'mp3',
157 'timestamp': 1569662587,
158 'uploader': 'associate-joygen-odiongan@archive.org',
159 'description': 'md5:012b2d668ae753be36896f343d12a236',
160 'upload_date': '20190928',
161 },
871a8929 162 'skip': 'restricted'
163 }, {
164 # Original formats are private
165 'url': 'https://archive.org/details/irelandthemakingofarepublic',
166 'info_dict': {
167 'id': 'irelandthemakingofarepublic',
168 'title': 'Ireland: The Making of a Republic',
169 'upload_date': '20160610',
170 'description': 'md5:f70956a156645a658a0dc9513d9e78b7',
171 'uploader': 'dimitrios@archive.org',
172 'creator': ['British Broadcasting Corporation', 'Time-Life Films'],
173 'timestamp': 1465594947,
174 },
175 'playlist': [
176 {
177 'md5': '0b211261b26590d49df968f71b90690d',
178 'info_dict': {
179 'id': 'irelandthemakingofarepublic/irelandthemakingofarepublicreel1_01.mov',
180 'ext': 'mp4',
181 'title': 'irelandthemakingofarepublicreel1_01.mov',
182 'duration': 130.46,
183 'thumbnail': 'https://archive.org/download/irelandthemakingofarepublic/irelandthemakingofarepublic.thumbs/irelandthemakingofarepublicreel1_01_000117.jpg',
184 'display_id': 'irelandthemakingofarepublicreel1_01.mov',
185 },
186 }, {
187 'md5': '67335ee3b23a0da930841981c1e79b02',
188 'info_dict': {
189 'id': 'irelandthemakingofarepublic/irelandthemakingofarepublicreel1_02.mov',
190 'ext': 'mp4',
191 'duration': 1395.13,
192 'title': 'irelandthemakingofarepublicreel1_02.mov',
193 'display_id': 'irelandthemakingofarepublicreel1_02.mov',
194 'thumbnail': 'https://archive.org/download/irelandthemakingofarepublic/irelandthemakingofarepublic.thumbs/irelandthemakingofarepublicreel1_02_001374.jpg',
195 },
196 }, {
197 'md5': 'e470e86787893603f4a341a16c281eb5',
198 'info_dict': {
199 'id': 'irelandthemakingofarepublic/irelandthemakingofarepublicreel2.mov',
200 'ext': 'mp4',
201 'duration': 1602.67,
202 'title': 'irelandthemakingofarepublicreel2.mov',
203 'thumbnail': 'https://archive.org/download/irelandthemakingofarepublic/irelandthemakingofarepublic.thumbs/irelandthemakingofarepublicreel2_001554.jpg',
204 'display_id': 'irelandthemakingofarepublicreel2.mov',
205 },
206 }
207 ]
e8e28989 208 }]
ff7a07d5 209
a3e26449 210 @staticmethod
211 def _playlist_data(webpage):
212 element = re.findall(r'''(?xs)
213 <input
214 (?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*?
215 \s+class=['"]?js-play8-playlist['"]?
216 (?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*?
217 \s*/>
218 ''', webpage)[0]
219
220 return json.loads(extract_attributes(element)['value'])
221
5fe3a3c3 222 def _real_extract(self, url):
ac668111 223 video_id = urllib.parse.unquote_plus(self._match_id(url))
a3e26449 224 identifier, entry_id = (video_id.split('/', 1) + [None])[:2]
225
226 # Archive.org metadata API doesn't clearly demarcate playlist entries
227 # or subtitle tracks, so we get them from the embeddable player.
1f13021e 228 embed_page = self._download_webpage(f'https://archive.org/embed/{identifier}', identifier)
a3e26449 229 playlist = self._playlist_data(embed_page)
230
231 entries = {}
232 for p in playlist:
233 # If the user specified a playlist entry in the URL, ignore the
234 # rest of the playlist.
235 if entry_id and p['orig'] != entry_id:
236 continue
237
238 entries[p['orig']] = {
239 'formats': [],
240 'thumbnails': [],
241 'artist': p.get('artist'),
242 'track': p.get('title'),
1f13021e 243 'subtitles': {},
244 }
a3e26449 245
246 for track in p.get('tracks', []):
247 if track['kind'] != 'subtitles':
248 continue
a3e26449 249 entries[p['orig']][track['label']] = {
1f13021e 250 'url': 'https://archive.org/' + track['file'].lstrip('/')
251 }
5fe3a3c3 252
1f13021e 253 metadata = self._download_json('http://archive.org/metadata/' + identifier, identifier)
a3e26449 254 m = metadata['metadata']
255 identifier = m['identifier']
256
257 info = {
258 'id': identifier,
259 'title': m['title'],
260 'description': clean_html(m.get('description')),
261 'uploader': dict_get(m, ['uploader', 'adder']),
262 'creator': m.get('creator'),
263 'license': m.get('licenseurl'),
264 'release_date': unified_strdate(m.get('date')),
265 'timestamp': unified_timestamp(dict_get(m, ['publicdate', 'addeddate'])),
1f13021e 266 'webpage_url': f'https://archive.org/details/{identifier}',
a3e26449 267 'location': m.get('venue'),
268 'release_year': int_or_none(m.get('year'))}
269
270 for f in metadata['files']:
271 if f['name'] in entries:
272 entries[f['name']] = merge_dicts(entries[f['name']], {
273 'id': identifier + '/' + f['name'],
274 'title': f.get('title') or f['name'],
275 'display_id': f['name'],
276 'description': clean_html(f.get('description')),
277 'creator': f.get('creator'),
278 'duration': parse_duration(f.get('length')),
279 'track_number': int_or_none(f.get('track')),
280 'album': f.get('album'),
281 'discnumber': int_or_none(f.get('disc')),
282 'release_year': int_or_none(f.get('year'))})
283 entry = entries[f['name']]
e612f66c 284 elif traverse_obj(f, 'original', expected_type=str) in entries:
a3e26449 285 entry = entries[f['original']]
286 else:
287 continue
288
289 if f.get('format') == 'Thumbnail':
290 entry['thumbnails'].append({
291 'id': f['name'],
292 'url': 'https://archive.org/download/' + identifier + '/' + f['name'],
293 'width': int_or_none(f.get('width')),
294 'height': int_or_none(f.get('width')),
295 'filesize': int_or_none(f.get('size'))})
296
297 extension = (f['name'].rsplit('.', 1) + [None])[1]
871a8929 298
299 # We don't want to skip private formats if the user has access to them,
300 # however without access to an account with such privileges we can't implement/test this.
301 # For now to be safe, we will only skip them if there is no user logged in.
302 is_logged_in = bool(self._get_cookies('https://archive.org').get('logged-in-sig'))
303 if extension in KNOWN_EXTENSIONS and (not f.get('private') or is_logged_in):
a3e26449 304 entry['formats'].append({
305 'url': 'https://archive.org/download/' + identifier + '/' + f['name'],
306 'format': f.get('format'),
307 'width': int_or_none(f.get('width')),
308 'height': int_or_none(f.get('height')),
309 'filesize': int_or_none(f.get('size')),
871a8929 310 'protocol': 'https',
311 'source_preference': 0 if f.get('source') == 'original' else -1,
312 'format_note': f.get('source')
313 })
a3e26449 314
a3e26449 315 for entry in entries.values():
9f14daf2 316 entry['_format_sort_fields'] = ('source', )
a3e26449 317
318 if len(entries) == 1:
319 # If there's only one item, use it as the main info dict
1f13021e 320 only_video = next(iter(entries.values()))
a3e26449 321 if entry_id:
322 info = merge_dicts(only_video, info)
323 else:
324 info = merge_dicts(info, only_video)
325 else:
326 # Otherwise, we have a playlist.
327 info['_type'] = 'playlist'
328 info['entries'] = list(entries.values())
329
330 if metadata.get('reviews'):
331 info['comments'] = []
332 for review in metadata['reviews']:
333 info['comments'].append({
334 'id': review.get('review_id'),
335 'author': review.get('reviewer'),
336 'text': str_or_none(review.get('reviewtitle'), '') + '\n\n' + review.get('reviewbody'),
337 'timestamp': unified_timestamp(review.get('createdate')),
338 'parent': 'root'})
339
84bc23b4 340 return info
879e7199 341
342
343class YoutubeWebArchiveIE(InfoExtractor):
344 IE_NAME = 'web.archive:youtube'
1f13021e 345 IE_DESC = 'web.archive.org saved youtube videos, "ytarchive:" prefix'
346 _VALID_URL = r'''(?x)(?:(?P<prefix>ytarchive:)|
347 (?:https?://)?web\.archive\.org/
348 (?:web/)?(?:(?P<date>[0-9]{14})?[0-9A-Za-z_*]*/)? # /web and the version index is optional
349 (?:https?(?::|%3[Aa])//)?(?:
350 (?:\w+\.)?youtube\.com(?::(?:80|443))?/watch(?:\.php)?(?:\?|%3[fF])(?:[^\#]+(?:&|%26))?v(?:=|%3[dD]) # Youtube URL
351 |(?:wayback-fakeurl\.archive\.org/yt/) # Or the internal fake url
352 )
353 )(?P<id>[0-9A-Za-z_-]{11})
354 (?(prefix)
355 (?::(?P<date2>[0-9]{14}))?$|
356 (?:%26|[#&]|$)
357 )'''
879e7199 358
359 _TESTS = [
360 {
361 'url': 'https://web.archive.org/web/20150415002341/https://www.youtube.com/watch?v=aYAGB11YrSs',
362 'info_dict': {
363 'id': 'aYAGB11YrSs',
364 'ext': 'webm',
aa4b0545 365 'title': 'Team Fortress 2 - Sandviches!',
366 'description': 'md5:4984c0f9a07f349fc5d8e82ab7af4eaf',
367 'upload_date': '20110926',
368 'uploader': 'Zeurel',
369 'channel_id': 'UCukCyHaD-bK3in_pKpfH9Eg',
370 'duration': 32,
371 'uploader_id': 'Zeurel',
217753f4
M
372 'uploader_url': 'https://www.youtube.com/user/Zeurel',
373 'thumbnail': r're:https?://.*\.(jpg|webp)',
374 'channel_url': 'https://www.youtube.com/channel/UCukCyHaD-bK3in_pKpfH9Eg',
879e7199 375 }
aa4b0545 376 }, {
879e7199 377 # Internal link
378 'url': 'https://web.archive.org/web/2oe/http://wayback-fakeurl.archive.org/yt/97t7Xj_iBv0',
379 'info_dict': {
380 'id': '97t7Xj_iBv0',
381 'ext': 'mp4',
aa4b0545 382 'title': 'Why Machines That Bend Are Better',
383 'description': 'md5:00404df2c632d16a674ff8df1ecfbb6c',
384 'upload_date': '20190312',
385 'uploader': 'Veritasium',
386 'channel_id': 'UCHnyfMqiRRG1u-2MsSQLbXA',
387 'duration': 771,
388 'uploader_id': '1veritasium',
217753f4
M
389 'uploader_url': 'https://www.youtube.com/user/1veritasium',
390 'thumbnail': r're:https?://.*\.(jpg|webp)',
391 'channel_url': 'https://www.youtube.com/channel/UCHnyfMqiRRG1u-2MsSQLbXA',
879e7199 392 }
aa4b0545 393 }, {
394 # Video from 2012, webm format itag 45. Newest capture is deleted video, with an invalid description.
395 # Should use the date in the link. Title ends with '- Youtube'. Capture has description in eow-description
879e7199 396 'url': 'https://web.archive.org/web/20120712231619/http://www.youtube.com/watch?v=AkhihxRKcrs&gl=US&hl=en',
397 'info_dict': {
398 'id': 'AkhihxRKcrs',
399 'ext': 'webm',
aa4b0545 400 'title': 'Limited Run: Mondo\'s Modern Classic 1 of 3 (SDCC 2012)',
401 'upload_date': '20120712',
402 'duration': 398,
403 'description': 'md5:ff4de6a7980cb65d951c2f6966a4f2f3',
404 'uploader_id': 'machinima',
217753f4
M
405 'uploader_url': 'https://www.youtube.com/user/machinima',
406 'thumbnail': r're:https?://.*\.(jpg|webp)',
407 'uploader': 'machinima'
879e7199 408 }
aa4b0545 409 }, {
410 # FLV video. Video file URL does not provide itag information
879e7199 411 'url': 'https://web.archive.org/web/20081211103536/http://www.youtube.com/watch?v=jNQXAC9IVRw',
412 'info_dict': {
413 'id': 'jNQXAC9IVRw',
aa4b0545 414 'ext': 'flv',
415 'title': 'Me at the zoo',
416 'upload_date': '20050423',
417 'channel_id': 'UC4QobU6STFB0P71PMvOGN5A',
418 'duration': 19,
419 'description': 'md5:10436b12e07ac43ff8df65287a56efb4',
420 'uploader_id': 'jawed',
217753f4
M
421 'uploader_url': 'https://www.youtube.com/user/jawed',
422 'channel_url': 'https://www.youtube.com/channel/UC4QobU6STFB0P71PMvOGN5A',
423 'thumbnail': r're:https?://.*\.(jpg|webp)',
424 'uploader': 'jawed',
879e7199 425 }
aa4b0545 426 }, {
879e7199 427 'url': 'https://web.archive.org/web/20110712231407/http://www.youtube.com/watch?v=lTx3G6h2xyA',
428 'info_dict': {
429 'id': 'lTx3G6h2xyA',
430 'ext': 'flv',
aa4b0545 431 'title': 'Madeon - Pop Culture (live mashup)',
432 'upload_date': '20110711',
433 'uploader': 'Madeon',
434 'channel_id': 'UCqMDNf3Pn5L7pcNkuSEeO3w',
435 'duration': 204,
436 'description': 'md5:f7535343b6eda34a314eff8b85444680',
437 'uploader_id': 'itsmadeon',
217753f4
M
438 'uploader_url': 'https://www.youtube.com/user/itsmadeon',
439 'channel_url': 'https://www.youtube.com/channel/UCqMDNf3Pn5L7pcNkuSEeO3w',
440 'thumbnail': r're:https?://.*\.(jpg|webp)',
879e7199 441 }
aa4b0545 442 }, {
443 # First capture is of dead video, second is the oldest from CDX response.
444 'url': 'https://web.archive.org/https://www.youtube.com/watch?v=1JYutPM8O6E',
879e7199 445 'info_dict': {
aa4b0545 446 'id': '1JYutPM8O6E',
879e7199 447 'ext': 'mp4',
aa4b0545 448 'title': 'Fake Teen Doctor Strikes AGAIN! - Weekly Weird News',
449 'upload_date': '20160218',
450 'channel_id': 'UCdIaNUarhzLSXGoItz7BHVA',
217753f4 451 'duration': 1235,
aa4b0545 452 'description': 'md5:21032bae736421e89c2edf36d1936947',
453 'uploader_id': 'MachinimaETC',
217753f4
M
454 'uploader_url': 'https://www.youtube.com/user/MachinimaETC',
455 'channel_url': 'https://www.youtube.com/channel/UCdIaNUarhzLSXGoItz7BHVA',
456 'thumbnail': r're:https?://.*\.(jpg|webp)',
457 'uploader': 'ETC News',
aa4b0545 458 }
459 }, {
460 # First capture of dead video, capture date in link links to dead capture.
461 'url': 'https://web.archive.org/web/20180803221945/https://www.youtube.com/watch?v=6FPhZJGvf4E',
879e7199 462 'info_dict': {
aa4b0545 463 'id': '6FPhZJGvf4E',
879e7199 464 'ext': 'mp4',
aa4b0545 465 'title': 'WTF: Video Games Still Launch BROKEN?! - T.U.G.S.',
466 'upload_date': '20160219',
467 'channel_id': 'UCdIaNUarhzLSXGoItz7BHVA',
217753f4 468 'duration': 797,
aa4b0545 469 'description': 'md5:a1dbf12d9a3bd7cb4c5e33b27d77ffe7',
470 'uploader_id': 'MachinimaETC',
217753f4
M
471 'uploader_url': 'https://www.youtube.com/user/MachinimaETC',
472 'channel_url': 'https://www.youtube.com/channel/UCdIaNUarhzLSXGoItz7BHVA',
473 'thumbnail': r're:https?://.*\.(jpg|webp)',
474 'uploader': 'ETC News',
879e7199 475 },
476 'expected_warnings': [
aa4b0545 477 r'unable to download capture webpage \(it may not be archived\)'
879e7199 478 ]
aa4b0545 479 }, { # Very old YouTube page, has - YouTube in title.
480 'url': 'http://web.archive.org/web/20070302011044/http://youtube.com/watch?v=-06-KB9XTzg',
481 'info_dict': {
482 'id': '-06-KB9XTzg',
483 'ext': 'flv',
484 'title': 'New Coin Hack!! 100% Safe!!'
485 }
486 }, {
487 'url': 'web.archive.org/https://www.youtube.com/watch?v=dWW7qP423y8',
488 'info_dict': {
489 'id': 'dWW7qP423y8',
490 'ext': 'mp4',
491 'title': 'It\'s Bootleg AirPods Time.',
492 'upload_date': '20211021',
493 'channel_id': 'UC7Jwj9fkrf1adN4fMmTkpug',
217753f4 494 'channel_url': 'https://www.youtube.com/channel/UC7Jwj9fkrf1adN4fMmTkpug',
aa4b0545 495 'duration': 810,
496 'description': 'md5:7b567f898d8237b256f36c1a07d6d7bc',
217753f4 497 'thumbnail': r're:https?://.*\.(jpg|webp)',
aa4b0545 498 'uploader': 'DankPods',
aa4b0545 499 }
500 }, {
501 # player response contains '};' See: https://github.com/ytdl-org/youtube-dl/issues/27093
502 'url': 'https://web.archive.org/web/20200827003909if_/http://www.youtube.com/watch?v=6Dh-RL__uN4',
503 'info_dict': {
504 'id': '6Dh-RL__uN4',
505 'ext': 'mp4',
506 'title': 'bitch lasagna',
507 'upload_date': '20181005',
508 'channel_id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw',
217753f4 509 'channel_url': 'https://www.youtube.com/channel/UC-lHJZR3Gqxm24_Vd_AJ5Yw',
aa4b0545 510 'duration': 135,
511 'description': 'md5:2dbe4051feeff2dab5f41f82bb6d11d0',
512 'uploader': 'PewDiePie',
513 'uploader_id': 'PewDiePie',
217753f4
M
514 'uploader_url': 'https://www.youtube.com/user/PewDiePie',
515 'thumbnail': r're:https?://.*\.(jpg|webp)',
516 }
517 }, {
518 # ~June 2010 Capture. swfconfig
519 'url': 'https://web.archive.org/web/0/https://www.youtube.com/watch?v=8XeW5ilk-9Y',
520 'info_dict': {
521 'id': '8XeW5ilk-9Y',
522 'ext': 'flv',
523 'title': 'Story of Stuff, The Critique Part 4 of 4',
524 'duration': 541,
525 'description': 'md5:28157da06f2c5e94c97f7f3072509972',
526 'uploader': 'HowTheWorldWorks',
527 'uploader_id': 'HowTheWorldWorks',
528 'thumbnail': r're:https?://.*\.(jpg|webp)',
529 'uploader_url': 'https://www.youtube.com/user/HowTheWorldWorks',
530 'upload_date': '20090520',
531 }
532 }, {
533 # Jan 2011: watch-video-date/eow-date surrounded by whitespace
534 'url': 'https://web.archive.org/web/20110126141719/http://www.youtube.com/watch?v=Q_yjX80U7Yc',
535 'info_dict': {
536 'id': 'Q_yjX80U7Yc',
537 'ext': 'flv',
538 'title': 'Spray Paint Art by Clay Butler: Purple Fantasy Forest',
539 'uploader_id': 'claybutlermusic',
540 'description': 'md5:4595264559e3d0a0ceb3f011f6334543',
541 'upload_date': '20090803',
542 'uploader': 'claybutlermusic',
543 'thumbnail': r're:https?://.*\.(jpg|webp)',
544 'duration': 132,
545 'uploader_url': 'https://www.youtube.com/user/claybutlermusic',
546 }
547 }, {
548 # ~May 2009 swfArgs. ytcfg is spread out over various vars
549 'url': 'https://web.archive.org/web/0/https://www.youtube.com/watch?v=c5uJgG05xUY',
550 'info_dict': {
551 'id': 'c5uJgG05xUY',
552 'ext': 'webm',
553 'title': 'Story of Stuff, The Critique Part 1 of 4',
554 'uploader_id': 'HowTheWorldWorks',
555 'uploader': 'HowTheWorldWorks',
556 'uploader_url': 'https://www.youtube.com/user/HowTheWorldWorks',
557 'upload_date': '20090513',
558 'description': 'md5:4ca77d79538064e41e4cc464e93f44f0',
559 'thumbnail': r're:https?://.*\.(jpg|webp)',
560 'duration': 754,
561 }
562 }, {
563 # ~June 2012. Upload date is in another lang so cannot extract.
564 'url': 'https://web.archive.org/web/20120607174520/http://www.youtube.com/watch?v=xWTLLl-dQaA',
565 'info_dict': {
566 'id': 'xWTLLl-dQaA',
567 'ext': 'mp4',
568 'title': 'Black Nerd eHarmony Video Bio Parody (SPOOF)',
569 'uploader_url': 'https://www.youtube.com/user/BlackNerdComedy',
570 'description': 'md5:e25f0133aaf9e6793fb81c18021d193e',
571 'uploader_id': 'BlackNerdComedy',
572 'uploader': 'BlackNerdComedy',
573 'duration': 182,
574 'thumbnail': r're:https?://.*\.(jpg|webp)',
575 }
576 }, {
577 # ~July 2013
578 'url': 'https://web.archive.org/web/*/https://www.youtube.com/watch?v=9eO1aasHyTM',
579 'info_dict': {
580 'id': '9eO1aasHyTM',
581 'ext': 'mp4',
582 'title': 'Polar-oid',
583 'description': 'Cameras and bears are dangerous!',
584 'uploader_url': 'https://www.youtube.com/user/punkybird',
585 'uploader_id': 'punkybird',
586 'duration': 202,
587 'channel_id': 'UC62R2cBezNBOqxSerfb1nMQ',
588 'channel_url': 'https://www.youtube.com/channel/UC62R2cBezNBOqxSerfb1nMQ',
589 'upload_date': '20060428',
590 'uploader': 'punkybird',
591 }
592 }, {
593 # April 2020: Player response in player config
594 'url': 'https://web.archive.org/web/20200416034815/https://www.youtube.com/watch?v=Cf7vS8jc7dY&gl=US&hl=en',
595 'info_dict': {
596 'id': 'Cf7vS8jc7dY',
597 'ext': 'mp4',
598 'title': 'A Dramatic Pool Story (by Jamie Spicer-Lewis) - Game Grumps Animated',
599 'duration': 64,
600 'upload_date': '20200408',
601 'uploader_id': 'GameGrumps',
602 'uploader': 'GameGrumps',
603 'channel_url': 'https://www.youtube.com/channel/UC9CuvdOVfMPvKCiwdGKL3cQ',
604 'channel_id': 'UC9CuvdOVfMPvKCiwdGKL3cQ',
605 'thumbnail': r're:https?://.*\.(jpg|webp)',
606 'description': 'md5:c625bb3c02c4f5fb4205971e468fa341',
607 'uploader_url': 'https://www.youtube.com/user/GameGrumps',
608 }
609 }, {
610 # watch7-user-header with yt-user-info
611 'url': 'ytarchive:kbh4T_b4Ixw:20160307085057',
612 'info_dict': {
613 'id': 'kbh4T_b4Ixw',
614 'ext': 'mp4',
615 'title': 'Shovel Knight OST - Strike the Earth! Plains of Passage 16 bit SNES style remake / remix',
616 'channel_url': 'https://www.youtube.com/channel/UCnTaGvsHmMy792DWeT6HbGA',
617 'uploader': 'Nelward music',
618 'duration': 213,
619 'description': 'md5:804b4a9ce37b050a5fefdbb23aeba54d',
620 'thumbnail': r're:https?://.*\.(jpg|webp)',
621 'upload_date': '20150503',
622 'channel_id': 'UCnTaGvsHmMy792DWeT6HbGA',
623 }
624 }, {
625 # April 2012
626 'url': 'https://web.archive.org/web/0/https://www.youtube.com/watch?v=SOm7mPoPskU',
627 'info_dict': {
628 'id': 'SOm7mPoPskU',
629 'ext': 'mp4',
630 'title': 'Boyfriend - Justin Bieber Parody',
631 'uploader_url': 'https://www.youtube.com/user/thecomputernerd01',
632 'uploader': 'thecomputernerd01',
633 'thumbnail': r're:https?://.*\.(jpg|webp)',
634 'description': 'md5:dd7fa635519c2a5b4d566beaecad7491',
635 'duration': 200,
636 'upload_date': '20120407',
637 'uploader_id': 'thecomputernerd01',
aa4b0545 638 }
639 }, {
640 'url': 'https://web.archive.org/web/http://www.youtube.com/watch?v=kH-G_aIBlFw',
641 'only_matching': True
642 }, {
643 'url': 'https://web.archive.org/web/20050214000000_if/http://www.youtube.com/watch?v=0altSZ96U4M',
644 'only_matching': True
645 }, {
879e7199 646 # Video not archived, only capture is unavailable video page
647 'url': 'https://web.archive.org/web/20210530071008/https://www.youtube.com/watch?v=lHJTf93HL1s&spfreload=10',
aa4b0545 648 'only_matching': True
649 }, { # Encoded url
879e7199 650 'url': 'https://web.archive.org/web/20120712231619/http%3A//www.youtube.com/watch%3Fgl%3DUS%26v%3DAkhihxRKcrs%26hl%3Den',
aa4b0545 651 'only_matching': True
652 }, {
879e7199 653 'url': 'https://web.archive.org/web/20120712231619/http%3A//www.youtube.com/watch%3Fv%3DAkhihxRKcrs%26gl%3DUS%26hl%3Den',
aa4b0545 654 'only_matching': True
655 }, {
656 'url': 'https://web.archive.org/web/20060527081937/http://www.youtube.com:80/watch.php?v=ELTFsLT73fA&amp;search=soccer',
657 'only_matching': True
658 }, {
659 'url': 'https://web.archive.org/http://www.youtube.com:80/watch?v=-05VVye-ffg',
660 'only_matching': True
1f13021e 661 }, {
662 'url': 'ytarchive:BaW_jenozKc:20050214000000',
663 'only_matching': True
88f23a18 664 }, {
1f13021e 665 'url': 'ytarchive:BaW_jenozKc',
666 'only_matching': True
667 },
879e7199 668 ]
b7c47b74 669 _YT_INITIAL_DATA_RE = YoutubeBaseInfoExtractor._YT_INITIAL_DATA_RE
19b4e59a 670 _YT_INITIAL_PLAYER_RESPONSE_RE = fr'''(?x:
b7c47b74 671 (?:window\s*\[\s*["\']ytInitialPlayerResponse["\']\s*\]|ytInitialPlayerResponse)\s*=[(\s]*|
19b4e59a 672 {YoutubeBaseInfoExtractor._YT_INITIAL_PLAYER_RESPONSE_RE}
673 )'''
aa4b0545 674
675 _YT_DEFAULT_THUMB_SERVERS = ['i.ytimg.com'] # thumbnails most likely archived on these servers
676 _YT_ALL_THUMB_SERVERS = orderedSet(
677 _YT_DEFAULT_THUMB_SERVERS + ['img.youtube.com', *[f'{c}{n or ""}.ytimg.com' for c in ('i', 's') for n in (*range(0, 5), 9)]])
678
679 _WAYBACK_BASE_URL = 'https://web.archive.org/web/%sif_/'
680 _OLDEST_CAPTURE_DATE = 20050214000000
681 _NEWEST_CAPTURE_DATE = 20500101000000
682
c8e856a5 683 def _call_cdx_api(self, item_id, url, filters: list = None, collapse: list = None, query: dict = None, note=None, fatal=False):
aa4b0545 684 # CDX docs: https://github.com/internetarchive/wayback/blob/master/wayback-cdx-server/README.md
685 query = {
686 'url': url,
687 'output': 'json',
688 'fl': 'original,mimetype,length,timestamp',
689 'limit': 500,
690 'filter': ['statuscode:200'] + (filters or []),
691 'collapse': collapse or [],
692 **(query or {})
693 }
c8e856a5 694 res = self._download_json(
695 'https://web.archive.org/cdx/search/cdx', item_id,
696 note or 'Downloading CDX API JSON', query=query, fatal=fatal)
aa4b0545 697 if isinstance(res, list) and len(res) >= 2:
698 # format response to make it easier to use
699 return list(dict(zip(res[0], v)) for v in res[1:])
700 elif not isinstance(res, list) or len(res) != 0:
701 self.report_warning('Error while parsing CDX API response' + bug_reports_message())
702
aa4b0545 703 def _extract_webpage_title(self, webpage):
04f3fd2c 704 page_title = self._html_extract_title(webpage, default='')
aa4b0545 705 # YouTube video pages appear to always have either 'YouTube -' as prefix or '- YouTube' as suffix.
706 return self._html_search_regex(
707 r'(?:YouTube\s*-\s*(.*)$)|(?:(.*)\s*-\s*YouTube$)',
708 page_title, 'title', default='')
709
710 def _extract_metadata(self, video_id, webpage):
aa4b0545 711 search_meta = ((lambda x: self._html_search_meta(x, webpage, default=None)) if webpage else (lambda x: None))
b7c47b74 712 player_response = self._search_json(
713 self._YT_INITIAL_PLAYER_RESPONSE_RE, webpage, 'initial player response',
f0bc6e20 714 video_id, default={})
b7c47b74 715 initial_data = self._search_json(
f0bc6e20 716 self._YT_INITIAL_DATA_RE, webpage, 'initial data', video_id, default={})
aa4b0545 717
217753f4
M
718 ytcfg = {}
719 for j in re.findall(r'yt\.setConfig\(\s*(?P<json>{\s*(?s:.+?)\s*})\s*\);', webpage): # ~June 2010
720 ytcfg.update(self._parse_json(j, video_id, fatal=False, ignore_extra=True, transform_source=js_to_json, errnote='') or {})
721
722 # XXX: this also may contain a 'ptchn' key
723 player_config = (
724 self._search_json(
725 r'(?:yt\.playerConfig|ytplayer\.config|swfConfig)\s*=',
726 webpage, 'player config', video_id, default=None)
727 or ytcfg.get('PLAYER_CONFIG') or {})
728
729 # XXX: this may also contain a 'creator' key.
730 swf_args = self._search_json(r'swfArgs\s*=', webpage, 'swf config', video_id, default={})
731 if swf_args and not traverse_obj(player_config, ('args',)):
732 player_config['args'] = swf_args
733
734 if not player_response:
735 # April 2020
736 player_response = self._parse_json(
737 traverse_obj(player_config, ('args', 'player_response')) or '{}', video_id, fatal=False)
738
aa4b0545 739 initial_data_video = traverse_obj(
740 initial_data, ('contents', 'twoColumnWatchNextResults', 'results', 'results', 'contents', ..., 'videoPrimaryInfoRenderer'),
741 expected_type=dict, get_all=False, default={})
742
743 video_details = traverse_obj(
744 player_response, 'videoDetails', expected_type=dict, get_all=False, default={})
745
746 microformats = traverse_obj(
747 player_response, ('microformat', 'playerMicroformatRenderer'), expected_type=dict, get_all=False, default={})
748
749 video_title = (
750 video_details.get('title')
751 or YoutubeBaseInfoExtractor._get_text(microformats, 'title')
752 or YoutubeBaseInfoExtractor._get_text(initial_data_video, 'title')
217753f4 753 or traverse_obj(player_config, ('args', 'title'))
aa4b0545 754 or self._extract_webpage_title(webpage)
755 or search_meta(['og:title', 'twitter:title', 'title']))
756
217753f4
M
757 def id_from_url(url, type_):
758 return self._search_regex(
759 rf'(?:{type_})/([^/#&?]+)', url or '', f'{type_} id', default=None)
760
761 # XXX: would the get_elements_by_... functions be better suited here?
762 _CHANNEL_URL_HREF_RE = r'href="[^"]*(?P<url>https?://www\.youtube\.com/(?:user|channel)/[^"]+)"'
763 uploader_or_channel_url = self._search_regex(
764 [fr'<(?:link\s*itemprop=\"url\"|a\s*id=\"watch-username\").*?\b{_CHANNEL_URL_HREF_RE}>', # @fd05024
765 fr'<div\s*id=\"(?:watch-channel-stats|watch-headline-user-info)\"[^>]*>\s*<a[^>]*\b{_CHANNEL_URL_HREF_RE}'], # ~ May 2009, ~June 2012
766 webpage, 'uploader or channel url', default=None)
767
768 owner_profile_url = url_or_none(microformats.get('ownerProfileUrl')) # @a6211d2
769
770 # Uploader refers to the /user/ id ONLY
771 uploader_id = (
772 id_from_url(owner_profile_url, 'user')
773 or id_from_url(uploader_or_channel_url, 'user')
774 or ytcfg.get('VIDEO_USERNAME'))
775 uploader_url = f'https://www.youtube.com/user/{uploader_id}' if uploader_id else None
776
777 # XXX: do we want to differentiate uploader and channel?
778 uploader = (
779 self._search_regex(
780 [r'<a\s*id="watch-username"[^>]*>\s*<strong>([^<]+)</strong>', # June 2010
781 r'var\s*watchUsername\s*=\s*\'(.+?)\';', # ~May 2009
782 r'<div\s*\bid=\"watch-channel-stats"[^>]*>\s*<a[^>]*>\s*(.+?)\s*</a', # ~May 2009
783 r'<a\s*id="watch-userbanner"[^>]*title="\s*(.+?)\s*"'], # ~June 2012
784 webpage, 'uploader', default=None)
785 or self._html_search_regex(
786 [r'(?s)<div\s*class="yt-user-info".*?<a[^>]*[^>]*>\s*(.*?)\s*</a', # March 2016
787 r'(?s)<a[^>]*yt-user-name[^>]*>\s*(.*?)\s*</a'], # july 2013
788 get_element_by_id('watch7-user-header', webpage), 'uploader', default=None)
789 or self._html_search_regex(
790 r'<button\s*href="/user/[^>]*>\s*<span[^>]*>\s*(.+?)\s*<', # April 2012
791 get_element_by_id('watch-headline-user-info', webpage), 'uploader', default=None)
792 or traverse_obj(player_config, ('args', 'creator'))
793 or video_details.get('author'))
794
aa4b0545 795 channel_id = str_or_none(
796 video_details.get('channelId')
797 or microformats.get('externalChannelId')
798 or search_meta('channelId')
799 or self._search_regex(
800 r'data-channel-external-id=(["\'])(?P<id>(?:(?!\1).)+)\1', # @b45a9e6
217753f4
M
801 webpage, 'channel id', default=None, group='id')
802 or id_from_url(owner_profile_url, 'channel')
803 or id_from_url(uploader_or_channel_url, 'channel')
804 or traverse_obj(player_config, ('args', 'ucid')))
aa4b0545 805
217753f4 806 channel_url = f'https://www.youtube.com/channel/{channel_id}' if channel_id else None
aa4b0545 807 duration = int_or_none(
808 video_details.get('lengthSeconds')
809 or microformats.get('lengthSeconds')
217753f4 810 or traverse_obj(player_config, ('args', ('length_seconds', 'l')), get_all=False)
aa4b0545 811 or parse_duration(search_meta('duration')))
812 description = (
813 video_details.get('shortDescription')
814 or YoutubeBaseInfoExtractor._get_text(microformats, 'description')
815 or clean_html(get_element_by_id('eow-description', webpage)) # @9e6dd23
816 or search_meta(['description', 'og:description', 'twitter:description']))
817
aa4b0545 818 upload_date = unified_strdate(
819 dict_get(microformats, ('uploadDate', 'publishDate'))
820 or search_meta(['uploadDate', 'datePublished'])
821 or self._search_regex(
217753f4
M
822 [r'(?s)id="eow-date.*?>\s*(.*?)\s*</span>',
823 r'(?:id="watch-uploader-info".*?>.*?|["\']simpleText["\']\s*:\s*["\'])(?:Published|Uploaded|Streamed live|Started) on (.+?)[<"\']', # @7998520
824 r'class\s*=\s*"(?:watch-video-date|watch-video-added post-date)"[^>]*>\s*([^<]+?)\s*<'], # ~June 2010, ~Jan 2009 (respectively)
aa4b0545 825 webpage, 'upload date', default=None))
826
827 return {
828 'title': video_title,
829 'description': description,
830 'upload_date': upload_date,
831 'uploader': uploader,
832 'channel_id': channel_id,
833 'channel_url': channel_url,
834 'duration': duration,
835 'uploader_url': uploader_url,
836 'uploader_id': uploader_id,
837 }
838
839 def _extract_thumbnails(self, video_id):
840 try_all = 'thumbnails' in self._configuration_arg('check_all')
841 thumbnail_base_urls = ['http://{server}/vi{webp}/{video_id}'.format(
842 webp='_webp' if ext == 'webp' else '', video_id=video_id, server=server)
843 for server in (self._YT_ALL_THUMB_SERVERS if try_all else self._YT_DEFAULT_THUMB_SERVERS) for ext in (('jpg', 'webp') if try_all else ('jpg',))]
844
845 thumbnails = []
846 for url in thumbnail_base_urls:
847 response = self._call_cdx_api(
848 video_id, url, filters=['mimetype:image/(?:webp|jpeg)'],
849 collapse=['urlkey'], query={'matchType': 'prefix'})
850 if not response:
851 continue
852 thumbnails.extend(
853 {
854 'url': (self._WAYBACK_BASE_URL % (int_or_none(thumbnail_dict.get('timestamp')) or self._OLDEST_CAPTURE_DATE)) + thumbnail_dict.get('original'),
855 'filesize': int_or_none(thumbnail_dict.get('length')),
856 'preference': int_or_none(thumbnail_dict.get('length'))
857 } for thumbnail_dict in response)
858 if not try_all:
859 break
860
861 self._remove_duplicate_formats(thumbnails)
862 return thumbnails
863
864 def _get_capture_dates(self, video_id, url_date):
865 capture_dates = []
866 # Note: CDX API will not find watch pages with extra params in the url.
867 response = self._call_cdx_api(
868 video_id, f'https://www.youtube.com/watch?v={video_id}',
869 filters=['mimetype:text/html'], collapse=['timestamp:6', 'digest'], query={'matchType': 'prefix'}) or []
86e5f3ed 870 all_captures = sorted(int_or_none(r['timestamp']) for r in response if int_or_none(r['timestamp']) is not None)
aa4b0545 871
872 # Prefer the new polymer UI captures as we support extracting more metadata from them
873 # WBM captures seem to all switch to this layout ~July 2020
1f13021e 874 modern_captures = [x for x in all_captures if x >= 20200701000000]
aa4b0545 875 if modern_captures:
876 capture_dates.append(modern_captures[0])
877 capture_dates.append(url_date)
878 if all_captures:
879 capture_dates.append(all_captures[0])
880
881 if 'captures' in self._configuration_arg('check_all'):
882 capture_dates.extend(modern_captures + all_captures)
883
884 # Fallbacks if any of the above fail
885 capture_dates.extend([self._OLDEST_CAPTURE_DATE, self._NEWEST_CAPTURE_DATE])
1f13021e 886 return orderedSet(filter(None, capture_dates))
879e7199 887
888 def _real_extract(self, url):
1f13021e 889 video_id, url_date, url_date_2 = self._match_valid_url(url).group('id', 'date', 'date2')
890 url_date = url_date or url_date_2
aa4b0545 891
892 urlh = None
217753f4
M
893 retry_manager = self.RetryManager(fatal=False)
894 for retry in retry_manager:
895 try:
896 urlh = self._request_webpage(
897 HEADRequest('https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s' % video_id),
898 video_id, note='Fetching archived video file url', expected_status=True)
899 except ExtractorError as e:
900 # HTTP Error 404 is expected if the video is not saved.
3d2623a8 901 if isinstance(e.cause, HTTPError) and e.cause.status == 404:
217753f4
M
902 self.raise_no_formats(
903 'The requested video is not archived, indexed, or there is an issue with web.archive.org (try again later)', expected=True)
904 else:
905 retry.error = e
906
907 if retry_manager.error:
908 self.raise_no_formats(retry_manager.error, expected=True, video_id=video_id)
aa4b0545 909
910 capture_dates = self._get_capture_dates(video_id, int_or_none(url_date))
1f13021e 911 self.write_debug('Captures to try: ' + join_nonempty(*capture_dates, delim=', '))
aa4b0545 912 info = {'id': video_id}
913 for capture in capture_dates:
aa4b0545 914 webpage = self._download_webpage(
915 (self._WAYBACK_BASE_URL + 'http://www.youtube.com/watch?v=%s') % (capture, video_id),
916 video_id=video_id, fatal=False, errnote='unable to download capture webpage (it may not be archived)',
917 note='Downloading capture webpage')
918 current_info = self._extract_metadata(video_id, webpage or '')
919 # Try avoid getting deleted video metadata
920 if current_info.get('title'):
921 info = merge_dicts(info, current_info)
922 if 'captures' not in self._configuration_arg('check_all'):
923 break
924
925 info['thumbnails'] = self._extract_thumbnails(video_id)
926
927 if urlh:
3d2623a8 928 url = compat_urllib_parse_unquote(urlh.url)
aa4b0545 929 video_file_url_qs = parse_qs(url)
930 # Attempt to recover any ext & format info from playback url & response headers
931 format = {'url': url, 'filesize': int_or_none(urlh.headers.get('x-archive-orig-content-length'))}
932 itag = try_get(video_file_url_qs, lambda x: x['itag'][0])
933 if itag and itag in YoutubeIE._formats:
934 format.update(YoutubeIE._formats[itag])
935 format.update({'format_id': itag})
936 else:
937 mime = try_get(video_file_url_qs, lambda x: x['mime'][0])
938 ext = (mimetype2ext(mime)
939 or urlhandle_detect_ext(urlh)
940 or mimetype2ext(urlh.headers.get('x-archive-guessed-content-type')))
941 format.update({'ext': ext})
942 info['formats'] = [format]
943 if not info.get('duration'):
944 info['duration'] = str_to_int(try_get(video_file_url_qs, lambda x: x['dur'][0]))
945
946 if not info.get('title'):
947 info['title'] = video_id
948 return info