]>
Commit | Line | Data |
---|---|---|
a3e26449 | 1 | import re |
2 | import json | |
a4a554a7 | 3 | from .common import InfoExtractor |
aa4b0545 | 4 | from .youtube import YoutubeIE, YoutubeBaseInfoExtractor |
879e7199 | 5 | from ..compat import ( |
6 | compat_urllib_parse_unquote, | |
7 | compat_urllib_parse_unquote_plus, | |
879e7199 | 8 | compat_HTTPError |
9 | ) | |
d50aca41 | 10 | from ..utils import ( |
aa4b0545 | 11 | bug_reports_message, |
d50aca41 | 12 | clean_html, |
a3e26449 | 13 | dict_get, |
879e7199 | 14 | extract_attributes, |
15 | ExtractorError, | |
aa4b0545 | 16 | get_element_by_id, |
879e7199 | 17 | HEADRequest, |
a3e26449 | 18 | int_or_none, |
1f13021e | 19 | join_nonempty, |
879e7199 | 20 | KNOWN_EXTENSIONS, |
a3e26449 | 21 | merge_dicts, |
879e7199 | 22 | mimetype2ext, |
aa4b0545 | 23 | orderedSet, |
879e7199 | 24 | parse_duration, |
4dfbf869 | 25 | parse_qs, |
879e7199 | 26 | str_to_int, |
27 | str_or_none, | |
aa4b0545 | 28 | traverse_obj, |
879e7199 | 29 | try_get, |
30 | unified_strdate, | |
31 | unified_timestamp, | |
aa4b0545 | 32 | urlhandle_detect_ext, |
33 | url_or_none | |
d50aca41 | 34 | ) |
5fe3a3c3 PH |
35 | |
36 | ||
a4a554a7 | 37 | class 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', | |
54 | }, | |
e8e28989 S |
55 | }, { |
56 | 'url': 'https://archive.org/details/Cops1922', | |
c12b4b80 | 57 | 'md5': '0869000b4ce265e8ca62738b336b268a', |
e8e28989 S |
58 | 'info_dict': { |
59 | 'id': 'Cops1922', | |
d50aca41 | 60 | 'ext': 'mp4', |
e8e28989 | 61 | 'title': 'Buster Keaton\'s "Cops" (1922)', |
a3e26449 | 62 | 'description': 'md5:43a603fd6c5b4b90d12a96b921212b9c', |
63 | 'uploader': 'yorkmba99@hotmail.com', | |
64 | 'timestamp': 1387699629, | |
1f13021e | 65 | 'upload_date': '20131222', |
a3e26449 | 66 | }, |
d50aca41 RA |
67 | }, { |
68 | 'url': 'http://archive.org/embed/XD300-23_68HighlightsAResearchCntAugHumanIntellect', | |
69 | 'only_matching': True, | |
a3e26449 | 70 | }, { |
71 | 'url': 'https://archive.org/details/Election_Ads', | |
72 | 'md5': '284180e857160cf866358700bab668a3', | |
73 | 'info_dict': { | |
74 | 'id': 'Election_Ads/Commercial-JFK1960ElectionAdCampaignJingle.mpg', | |
75 | 'title': 'Commercial-JFK1960ElectionAdCampaignJingle.mpg', | |
76 | 'ext': 'mp4', | |
77 | }, | |
78 | }, { | |
79 | 'url': 'https://archive.org/details/Election_Ads/Commercial-Nixon1960ElectionAdToughonDefense.mpg', | |
80 | 'md5': '7915213ef02559b5501fe630e1a53f59', | |
81 | 'info_dict': { | |
82 | 'id': 'Election_Ads/Commercial-Nixon1960ElectionAdToughonDefense.mpg', | |
83 | 'title': 'Commercial-Nixon1960ElectionAdToughonDefense.mpg', | |
84 | 'ext': 'mp4', | |
85 | 'timestamp': 1205588045, | |
86 | 'uploader': 'mikedavisstripmaster@yahoo.com', | |
87 | 'description': '1960 Presidential Campaign Election Commercials John F Kennedy, Richard M Nixon', | |
88 | 'upload_date': '20080315', | |
89 | }, | |
90 | }, { | |
91 | 'url': 'https://archive.org/details/gd1977-05-08.shure57.stevenson.29303.flac16', | |
92 | 'md5': '7d07ffb42aba6537c28e053efa4b54c9', | |
93 | 'info_dict': { | |
94 | 'id': 'gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t01.flac', | |
95 | 'title': 'Turning', | |
96 | 'ext': 'flac', | |
97 | }, | |
98 | }, { | |
99 | 'url': 'https://archive.org/details/gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t07.flac', | |
100 | 'md5': 'a07cd8c6ab4ee1560f8a0021717130f3', | |
101 | 'info_dict': { | |
102 | 'id': 'gd1977-05-08.shure57.stevenson.29303.flac16/gd1977-05-08d01t07.flac', | |
103 | 'title': 'Deal', | |
104 | 'ext': 'flac', | |
105 | 'timestamp': 1205895624, | |
106 | 'uploader': 'mvernon54@yahoo.com', | |
107 | 'description': 'md5:6a31f1996db0aa0fc9da6d6e708a1bb0', | |
108 | 'upload_date': '20080319', | |
109 | 'location': 'Barton Hall - Cornell University', | |
110 | }, | |
111 | }, { | |
112 | 'url': 'https://archive.org/details/lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik', | |
113 | 'md5': '7cb019baa9b332e82ea7c10403acd180', | |
114 | 'info_dict': { | |
115 | 'id': 'lp_the-music-of-russia_various-artists-a-askaryan-alexander-melik/disc1/01.01. Bells Of Rostov.mp3', | |
116 | 'title': 'Bells Of Rostov', | |
117 | 'ext': 'mp3', | |
118 | }, | |
119 | }, { | |
120 | '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', | |
121 | 'md5': '1d0aabe03edca83ca58d9ed3b493a3c3', | |
122 | 'info_dict': { | |
123 | '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', | |
124 | 'title': 'Song And Chorus In The Polovetsian Camp From "Prince Igor" (Act 2, Scene 1)', | |
125 | 'ext': 'mp3', | |
126 | 'timestamp': 1569662587, | |
127 | 'uploader': 'associate-joygen-odiongan@archive.org', | |
128 | 'description': 'md5:012b2d668ae753be36896f343d12a236', | |
129 | 'upload_date': '20190928', | |
130 | }, | |
e8e28989 | 131 | }] |
ff7a07d5 | 132 | |
a3e26449 | 133 | @staticmethod |
134 | def _playlist_data(webpage): | |
135 | element = re.findall(r'''(?xs) | |
136 | <input | |
137 | (?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*? | |
138 | \s+class=['"]?js-play8-playlist['"]? | |
139 | (?:\s+[a-zA-Z0-9:._-]+(?:=[a-zA-Z0-9:._-]*|="[^"]*"|='[^']*'|))*? | |
140 | \s*/> | |
141 | ''', webpage)[0] | |
142 | ||
143 | return json.loads(extract_attributes(element)['value']) | |
144 | ||
5fe3a3c3 | 145 | def _real_extract(self, url): |
a3e26449 | 146 | video_id = compat_urllib_parse_unquote_plus(self._match_id(url)) |
147 | identifier, entry_id = (video_id.split('/', 1) + [None])[:2] | |
148 | ||
149 | # Archive.org metadata API doesn't clearly demarcate playlist entries | |
150 | # or subtitle tracks, so we get them from the embeddable player. | |
1f13021e | 151 | embed_page = self._download_webpage(f'https://archive.org/embed/{identifier}', identifier) |
a3e26449 | 152 | playlist = self._playlist_data(embed_page) |
153 | ||
154 | entries = {} | |
155 | for p in playlist: | |
156 | # If the user specified a playlist entry in the URL, ignore the | |
157 | # rest of the playlist. | |
158 | if entry_id and p['orig'] != entry_id: | |
159 | continue | |
160 | ||
161 | entries[p['orig']] = { | |
162 | 'formats': [], | |
163 | 'thumbnails': [], | |
164 | 'artist': p.get('artist'), | |
165 | 'track': p.get('title'), | |
1f13021e | 166 | 'subtitles': {}, |
167 | } | |
a3e26449 | 168 | |
169 | for track in p.get('tracks', []): | |
170 | if track['kind'] != 'subtitles': | |
171 | continue | |
a3e26449 | 172 | entries[p['orig']][track['label']] = { |
1f13021e | 173 | 'url': 'https://archive.org/' + track['file'].lstrip('/') |
174 | } | |
5fe3a3c3 | 175 | |
1f13021e | 176 | metadata = self._download_json('http://archive.org/metadata/' + identifier, identifier) |
a3e26449 | 177 | m = metadata['metadata'] |
178 | identifier = m['identifier'] | |
179 | ||
180 | info = { | |
181 | 'id': identifier, | |
182 | 'title': m['title'], | |
183 | 'description': clean_html(m.get('description')), | |
184 | 'uploader': dict_get(m, ['uploader', 'adder']), | |
185 | 'creator': m.get('creator'), | |
186 | 'license': m.get('licenseurl'), | |
187 | 'release_date': unified_strdate(m.get('date')), | |
188 | 'timestamp': unified_timestamp(dict_get(m, ['publicdate', 'addeddate'])), | |
1f13021e | 189 | 'webpage_url': f'https://archive.org/details/{identifier}', |
a3e26449 | 190 | 'location': m.get('venue'), |
191 | 'release_year': int_or_none(m.get('year'))} | |
192 | ||
193 | for f in metadata['files']: | |
194 | if f['name'] in entries: | |
195 | entries[f['name']] = merge_dicts(entries[f['name']], { | |
196 | 'id': identifier + '/' + f['name'], | |
197 | 'title': f.get('title') or f['name'], | |
198 | 'display_id': f['name'], | |
199 | 'description': clean_html(f.get('description')), | |
200 | 'creator': f.get('creator'), | |
201 | 'duration': parse_duration(f.get('length')), | |
202 | 'track_number': int_or_none(f.get('track')), | |
203 | 'album': f.get('album'), | |
204 | 'discnumber': int_or_none(f.get('disc')), | |
205 | 'release_year': int_or_none(f.get('year'))}) | |
206 | entry = entries[f['name']] | |
e612f66c | 207 | elif traverse_obj(f, 'original', expected_type=str) in entries: |
a3e26449 | 208 | entry = entries[f['original']] |
209 | else: | |
210 | continue | |
211 | ||
212 | if f.get('format') == 'Thumbnail': | |
213 | entry['thumbnails'].append({ | |
214 | 'id': f['name'], | |
215 | 'url': 'https://archive.org/download/' + identifier + '/' + f['name'], | |
216 | 'width': int_or_none(f.get('width')), | |
217 | 'height': int_or_none(f.get('width')), | |
218 | 'filesize': int_or_none(f.get('size'))}) | |
219 | ||
220 | extension = (f['name'].rsplit('.', 1) + [None])[1] | |
221 | if extension in KNOWN_EXTENSIONS: | |
222 | entry['formats'].append({ | |
223 | 'url': 'https://archive.org/download/' + identifier + '/' + f['name'], | |
224 | 'format': f.get('format'), | |
225 | 'width': int_or_none(f.get('width')), | |
226 | 'height': int_or_none(f.get('height')), | |
227 | 'filesize': int_or_none(f.get('size')), | |
228 | 'protocol': 'https'}) | |
229 | ||
a3e26449 | 230 | for entry in entries.values(): |
1f13021e | 231 | self._sort_formats(entry['formats']) |
a3e26449 | 232 | |
233 | if len(entries) == 1: | |
234 | # If there's only one item, use it as the main info dict | |
1f13021e | 235 | only_video = next(iter(entries.values())) |
a3e26449 | 236 | if entry_id: |
237 | info = merge_dicts(only_video, info) | |
238 | else: | |
239 | info = merge_dicts(info, only_video) | |
240 | else: | |
241 | # Otherwise, we have a playlist. | |
242 | info['_type'] = 'playlist' | |
243 | info['entries'] = list(entries.values()) | |
244 | ||
245 | if metadata.get('reviews'): | |
246 | info['comments'] = [] | |
247 | for review in metadata['reviews']: | |
248 | info['comments'].append({ | |
249 | 'id': review.get('review_id'), | |
250 | 'author': review.get('reviewer'), | |
251 | 'text': str_or_none(review.get('reviewtitle'), '') + '\n\n' + review.get('reviewbody'), | |
252 | 'timestamp': unified_timestamp(review.get('createdate')), | |
253 | 'parent': 'root'}) | |
254 | ||
84bc23b4 | 255 | return info |
879e7199 | 256 | |
257 | ||
258 | class YoutubeWebArchiveIE(InfoExtractor): | |
259 | IE_NAME = 'web.archive:youtube' | |
1f13021e | 260 | IE_DESC = 'web.archive.org saved youtube videos, "ytarchive:" prefix' |
261 | _VALID_URL = r'''(?x)(?:(?P<prefix>ytarchive:)| | |
262 | (?:https?://)?web\.archive\.org/ | |
263 | (?:web/)?(?:(?P<date>[0-9]{14})?[0-9A-Za-z_*]*/)? # /web and the version index is optional | |
264 | (?:https?(?::|%3[Aa])//)?(?: | |
265 | (?:\w+\.)?youtube\.com(?::(?:80|443))?/watch(?:\.php)?(?:\?|%3[fF])(?:[^\#]+(?:&|%26))?v(?:=|%3[dD]) # Youtube URL | |
266 | |(?:wayback-fakeurl\.archive\.org/yt/) # Or the internal fake url | |
267 | ) | |
268 | )(?P<id>[0-9A-Za-z_-]{11}) | |
269 | (?(prefix) | |
270 | (?::(?P<date2>[0-9]{14}))?$| | |
271 | (?:%26|[#&]|$) | |
272 | )''' | |
879e7199 | 273 | |
274 | _TESTS = [ | |
275 | { | |
276 | 'url': 'https://web.archive.org/web/20150415002341/https://www.youtube.com/watch?v=aYAGB11YrSs', | |
277 | 'info_dict': { | |
278 | 'id': 'aYAGB11YrSs', | |
279 | 'ext': 'webm', | |
aa4b0545 | 280 | 'title': 'Team Fortress 2 - Sandviches!', |
281 | 'description': 'md5:4984c0f9a07f349fc5d8e82ab7af4eaf', | |
282 | 'upload_date': '20110926', | |
283 | 'uploader': 'Zeurel', | |
284 | 'channel_id': 'UCukCyHaD-bK3in_pKpfH9Eg', | |
285 | 'duration': 32, | |
286 | 'uploader_id': 'Zeurel', | |
287 | 'uploader_url': 'http://www.youtube.com/user/Zeurel' | |
879e7199 | 288 | } |
aa4b0545 | 289 | }, { |
879e7199 | 290 | # Internal link |
291 | 'url': 'https://web.archive.org/web/2oe/http://wayback-fakeurl.archive.org/yt/97t7Xj_iBv0', | |
292 | 'info_dict': { | |
293 | 'id': '97t7Xj_iBv0', | |
294 | 'ext': 'mp4', | |
aa4b0545 | 295 | 'title': 'Why Machines That Bend Are Better', |
296 | 'description': 'md5:00404df2c632d16a674ff8df1ecfbb6c', | |
297 | 'upload_date': '20190312', | |
298 | 'uploader': 'Veritasium', | |
299 | 'channel_id': 'UCHnyfMqiRRG1u-2MsSQLbXA', | |
300 | 'duration': 771, | |
301 | 'uploader_id': '1veritasium', | |
302 | 'uploader_url': 'http://www.youtube.com/user/1veritasium' | |
879e7199 | 303 | } |
aa4b0545 | 304 | }, { |
305 | # Video from 2012, webm format itag 45. Newest capture is deleted video, with an invalid description. | |
306 | # Should use the date in the link. Title ends with '- Youtube'. Capture has description in eow-description | |
879e7199 | 307 | 'url': 'https://web.archive.org/web/20120712231619/http://www.youtube.com/watch?v=AkhihxRKcrs&gl=US&hl=en', |
308 | 'info_dict': { | |
309 | 'id': 'AkhihxRKcrs', | |
310 | 'ext': 'webm', | |
aa4b0545 | 311 | 'title': 'Limited Run: Mondo\'s Modern Classic 1 of 3 (SDCC 2012)', |
312 | 'upload_date': '20120712', | |
313 | 'duration': 398, | |
314 | 'description': 'md5:ff4de6a7980cb65d951c2f6966a4f2f3', | |
315 | 'uploader_id': 'machinima', | |
316 | 'uploader_url': 'http://www.youtube.com/user/machinima' | |
879e7199 | 317 | } |
aa4b0545 | 318 | }, { |
319 | # FLV video. Video file URL does not provide itag information | |
879e7199 | 320 | 'url': 'https://web.archive.org/web/20081211103536/http://www.youtube.com/watch?v=jNQXAC9IVRw', |
321 | 'info_dict': { | |
322 | 'id': 'jNQXAC9IVRw', | |
aa4b0545 | 323 | 'ext': 'flv', |
324 | 'title': 'Me at the zoo', | |
325 | 'upload_date': '20050423', | |
326 | 'channel_id': 'UC4QobU6STFB0P71PMvOGN5A', | |
327 | 'duration': 19, | |
328 | 'description': 'md5:10436b12e07ac43ff8df65287a56efb4', | |
329 | 'uploader_id': 'jawed', | |
330 | 'uploader_url': 'http://www.youtube.com/user/jawed' | |
879e7199 | 331 | } |
aa4b0545 | 332 | }, { |
879e7199 | 333 | 'url': 'https://web.archive.org/web/20110712231407/http://www.youtube.com/watch?v=lTx3G6h2xyA', |
334 | 'info_dict': { | |
335 | 'id': 'lTx3G6h2xyA', | |
336 | 'ext': 'flv', | |
aa4b0545 | 337 | 'title': 'Madeon - Pop Culture (live mashup)', |
338 | 'upload_date': '20110711', | |
339 | 'uploader': 'Madeon', | |
340 | 'channel_id': 'UCqMDNf3Pn5L7pcNkuSEeO3w', | |
341 | 'duration': 204, | |
342 | 'description': 'md5:f7535343b6eda34a314eff8b85444680', | |
343 | 'uploader_id': 'itsmadeon', | |
344 | 'uploader_url': 'http://www.youtube.com/user/itsmadeon' | |
879e7199 | 345 | } |
aa4b0545 | 346 | }, { |
347 | # First capture is of dead video, second is the oldest from CDX response. | |
348 | 'url': 'https://web.archive.org/https://www.youtube.com/watch?v=1JYutPM8O6E', | |
879e7199 | 349 | 'info_dict': { |
aa4b0545 | 350 | 'id': '1JYutPM8O6E', |
879e7199 | 351 | 'ext': 'mp4', |
aa4b0545 | 352 | 'title': 'Fake Teen Doctor Strikes AGAIN! - Weekly Weird News', |
353 | 'upload_date': '20160218', | |
354 | 'channel_id': 'UCdIaNUarhzLSXGoItz7BHVA', | |
355 | 'duration': 1236, | |
356 | 'description': 'md5:21032bae736421e89c2edf36d1936947', | |
357 | 'uploader_id': 'MachinimaETC', | |
358 | 'uploader_url': 'http://www.youtube.com/user/MachinimaETC' | |
359 | } | |
360 | }, { | |
361 | # First capture of dead video, capture date in link links to dead capture. | |
362 | 'url': 'https://web.archive.org/web/20180803221945/https://www.youtube.com/watch?v=6FPhZJGvf4E', | |
879e7199 | 363 | 'info_dict': { |
aa4b0545 | 364 | 'id': '6FPhZJGvf4E', |
879e7199 | 365 | 'ext': 'mp4', |
aa4b0545 | 366 | 'title': 'WTF: Video Games Still Launch BROKEN?! - T.U.G.S.', |
367 | 'upload_date': '20160219', | |
368 | 'channel_id': 'UCdIaNUarhzLSXGoItz7BHVA', | |
369 | 'duration': 798, | |
370 | 'description': 'md5:a1dbf12d9a3bd7cb4c5e33b27d77ffe7', | |
371 | 'uploader_id': 'MachinimaETC', | |
372 | 'uploader_url': 'http://www.youtube.com/user/MachinimaETC' | |
879e7199 | 373 | }, |
374 | 'expected_warnings': [ | |
aa4b0545 | 375 | r'unable to download capture webpage \(it may not be archived\)' |
879e7199 | 376 | ] |
aa4b0545 | 377 | }, { # Very old YouTube page, has - YouTube in title. |
378 | 'url': 'http://web.archive.org/web/20070302011044/http://youtube.com/watch?v=-06-KB9XTzg', | |
379 | 'info_dict': { | |
380 | 'id': '-06-KB9XTzg', | |
381 | 'ext': 'flv', | |
382 | 'title': 'New Coin Hack!! 100% Safe!!' | |
383 | } | |
384 | }, { | |
385 | 'url': 'web.archive.org/https://www.youtube.com/watch?v=dWW7qP423y8', | |
386 | 'info_dict': { | |
387 | 'id': 'dWW7qP423y8', | |
388 | 'ext': 'mp4', | |
389 | 'title': 'It\'s Bootleg AirPods Time.', | |
390 | 'upload_date': '20211021', | |
391 | 'channel_id': 'UC7Jwj9fkrf1adN4fMmTkpug', | |
392 | 'channel_url': 'http://www.youtube.com/channel/UC7Jwj9fkrf1adN4fMmTkpug', | |
393 | 'duration': 810, | |
394 | 'description': 'md5:7b567f898d8237b256f36c1a07d6d7bc', | |
395 | 'uploader': 'DankPods', | |
396 | 'uploader_id': 'UC7Jwj9fkrf1adN4fMmTkpug', | |
397 | 'uploader_url': 'http://www.youtube.com/channel/UC7Jwj9fkrf1adN4fMmTkpug' | |
398 | } | |
399 | }, { | |
400 | # player response contains '};' See: https://github.com/ytdl-org/youtube-dl/issues/27093 | |
401 | 'url': 'https://web.archive.org/web/20200827003909if_/http://www.youtube.com/watch?v=6Dh-RL__uN4', | |
402 | 'info_dict': { | |
403 | 'id': '6Dh-RL__uN4', | |
404 | 'ext': 'mp4', | |
405 | 'title': 'bitch lasagna', | |
406 | 'upload_date': '20181005', | |
407 | 'channel_id': 'UC-lHJZR3Gqxm24_Vd_AJ5Yw', | |
408 | 'channel_url': 'http://www.youtube.com/channel/UC-lHJZR3Gqxm24_Vd_AJ5Yw', | |
409 | 'duration': 135, | |
410 | 'description': 'md5:2dbe4051feeff2dab5f41f82bb6d11d0', | |
411 | 'uploader': 'PewDiePie', | |
412 | 'uploader_id': 'PewDiePie', | |
413 | 'uploader_url': 'http://www.youtube.com/user/PewDiePie' | |
414 | } | |
415 | }, { | |
416 | 'url': 'https://web.archive.org/web/http://www.youtube.com/watch?v=kH-G_aIBlFw', | |
417 | 'only_matching': True | |
418 | }, { | |
419 | 'url': 'https://web.archive.org/web/20050214000000_if/http://www.youtube.com/watch?v=0altSZ96U4M', | |
420 | 'only_matching': True | |
421 | }, { | |
879e7199 | 422 | # Video not archived, only capture is unavailable video page |
423 | 'url': 'https://web.archive.org/web/20210530071008/https://www.youtube.com/watch?v=lHJTf93HL1s&spfreload=10', | |
aa4b0545 | 424 | 'only_matching': True |
425 | }, { # Encoded url | |
879e7199 | 426 | 'url': 'https://web.archive.org/web/20120712231619/http%3A//www.youtube.com/watch%3Fgl%3DUS%26v%3DAkhihxRKcrs%26hl%3Den', |
aa4b0545 | 427 | 'only_matching': True |
428 | }, { | |
879e7199 | 429 | 'url': 'https://web.archive.org/web/20120712231619/http%3A//www.youtube.com/watch%3Fv%3DAkhihxRKcrs%26gl%3DUS%26hl%3Den', |
aa4b0545 | 430 | 'only_matching': True |
431 | }, { | |
432 | 'url': 'https://web.archive.org/web/20060527081937/http://www.youtube.com:80/watch.php?v=ELTFsLT73fA&search=soccer', | |
433 | 'only_matching': True | |
434 | }, { | |
435 | 'url': 'https://web.archive.org/http://www.youtube.com:80/watch?v=-05VVye-ffg', | |
436 | 'only_matching': True | |
1f13021e | 437 | }, { |
438 | 'url': 'ytarchive:BaW_jenozKc:20050214000000', | |
439 | 'only_matching': True | |
88f23a18 | 440 | }, { |
1f13021e | 441 | 'url': 'ytarchive:BaW_jenozKc', |
442 | 'only_matching': True | |
443 | }, | |
879e7199 | 444 | ] |
aa4b0545 | 445 | _YT_INITIAL_DATA_RE = r'(?:(?:(?:window\s*\[\s*["\']ytInitialData["\']\s*\]|ytInitialData)\s*=\s*({.+?})\s*;)|%s)' % YoutubeBaseInfoExtractor._YT_INITIAL_DATA_RE |
446 | _YT_INITIAL_PLAYER_RESPONSE_RE = r'(?:(?:(?:window\s*\[\s*["\']ytInitialPlayerResponse["\']\s*\]|ytInitialPlayerResponse)\s*=[(\s]*({.+?})[)\s]*;)|%s)' % YoutubeBaseInfoExtractor._YT_INITIAL_PLAYER_RESPONSE_RE | |
447 | _YT_INITIAL_BOUNDARY_RE = r'(?:(?:var\s+meta|</script|\n)|%s)' % YoutubeBaseInfoExtractor._YT_INITIAL_BOUNDARY_RE | |
448 | ||
449 | _YT_DEFAULT_THUMB_SERVERS = ['i.ytimg.com'] # thumbnails most likely archived on these servers | |
450 | _YT_ALL_THUMB_SERVERS = orderedSet( | |
451 | _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)]]) | |
452 | ||
453 | _WAYBACK_BASE_URL = 'https://web.archive.org/web/%sif_/' | |
454 | _OLDEST_CAPTURE_DATE = 20050214000000 | |
455 | _NEWEST_CAPTURE_DATE = 20500101000000 | |
456 | ||
c8e856a5 | 457 | def _call_cdx_api(self, item_id, url, filters: list = None, collapse: list = None, query: dict = None, note=None, fatal=False): |
aa4b0545 | 458 | # CDX docs: https://github.com/internetarchive/wayback/blob/master/wayback-cdx-server/README.md |
459 | query = { | |
460 | 'url': url, | |
461 | 'output': 'json', | |
462 | 'fl': 'original,mimetype,length,timestamp', | |
463 | 'limit': 500, | |
464 | 'filter': ['statuscode:200'] + (filters or []), | |
465 | 'collapse': collapse or [], | |
466 | **(query or {}) | |
467 | } | |
c8e856a5 | 468 | res = self._download_json( |
469 | 'https://web.archive.org/cdx/search/cdx', item_id, | |
470 | note or 'Downloading CDX API JSON', query=query, fatal=fatal) | |
aa4b0545 | 471 | if isinstance(res, list) and len(res) >= 2: |
472 | # format response to make it easier to use | |
473 | return list(dict(zip(res[0], v)) for v in res[1:]) | |
474 | elif not isinstance(res, list) or len(res) != 0: | |
475 | self.report_warning('Error while parsing CDX API response' + bug_reports_message()) | |
476 | ||
477 | def _extract_yt_initial_variable(self, webpage, regex, video_id, name): | |
478 | return self._parse_json(self._search_regex( | |
86e5f3ed | 479 | (fr'{regex}\s*{self._YT_INITIAL_BOUNDARY_RE}', |
aa4b0545 | 480 | regex), webpage, name, default='{}'), video_id, fatal=False) |
481 | ||
482 | def _extract_webpage_title(self, webpage): | |
04f3fd2c | 483 | page_title = self._html_extract_title(webpage, default='') |
aa4b0545 | 484 | # YouTube video pages appear to always have either 'YouTube -' as prefix or '- YouTube' as suffix. |
485 | return self._html_search_regex( | |
486 | r'(?:YouTube\s*-\s*(.*)$)|(?:(.*)\s*-\s*YouTube$)', | |
487 | page_title, 'title', default='') | |
488 | ||
489 | def _extract_metadata(self, video_id, webpage): | |
aa4b0545 | 490 | search_meta = ((lambda x: self._html_search_meta(x, webpage, default=None)) if webpage else (lambda x: None)) |
491 | player_response = self._extract_yt_initial_variable( | |
492 | webpage, self._YT_INITIAL_PLAYER_RESPONSE_RE, video_id, 'initial player response') or {} | |
493 | initial_data = self._extract_yt_initial_variable( | |
494 | webpage, self._YT_INITIAL_DATA_RE, video_id, 'initial player response') or {} | |
495 | ||
496 | initial_data_video = traverse_obj( | |
497 | initial_data, ('contents', 'twoColumnWatchNextResults', 'results', 'results', 'contents', ..., 'videoPrimaryInfoRenderer'), | |
498 | expected_type=dict, get_all=False, default={}) | |
499 | ||
500 | video_details = traverse_obj( | |
501 | player_response, 'videoDetails', expected_type=dict, get_all=False, default={}) | |
502 | ||
503 | microformats = traverse_obj( | |
504 | player_response, ('microformat', 'playerMicroformatRenderer'), expected_type=dict, get_all=False, default={}) | |
505 | ||
506 | video_title = ( | |
507 | video_details.get('title') | |
508 | or YoutubeBaseInfoExtractor._get_text(microformats, 'title') | |
509 | or YoutubeBaseInfoExtractor._get_text(initial_data_video, 'title') | |
510 | or self._extract_webpage_title(webpage) | |
511 | or search_meta(['og:title', 'twitter:title', 'title'])) | |
512 | ||
513 | channel_id = str_or_none( | |
514 | video_details.get('channelId') | |
515 | or microformats.get('externalChannelId') | |
516 | or search_meta('channelId') | |
517 | or self._search_regex( | |
518 | r'data-channel-external-id=(["\'])(?P<id>(?:(?!\1).)+)\1', # @b45a9e6 | |
519 | webpage, 'channel id', default=None, group='id')) | |
520 | channel_url = f'http://www.youtube.com/channel/{channel_id}' if channel_id else None | |
521 | ||
522 | duration = int_or_none( | |
523 | video_details.get('lengthSeconds') | |
524 | or microformats.get('lengthSeconds') | |
525 | or parse_duration(search_meta('duration'))) | |
526 | description = ( | |
527 | video_details.get('shortDescription') | |
528 | or YoutubeBaseInfoExtractor._get_text(microformats, 'description') | |
529 | or clean_html(get_element_by_id('eow-description', webpage)) # @9e6dd23 | |
530 | or search_meta(['description', 'og:description', 'twitter:description'])) | |
531 | ||
532 | uploader = video_details.get('author') | |
533 | ||
534 | # Uploader ID and URL | |
535 | uploader_mobj = re.search( | |
536 | r'<link itemprop="url" href="(?P<uploader_url>https?://www\.youtube\.com/(?:user|channel)/(?P<uploader_id>[^"]+))">', # @fd05024 | |
537 | webpage) | |
538 | if uploader_mobj is not None: | |
539 | uploader_id, uploader_url = uploader_mobj.group('uploader_id'), uploader_mobj.group('uploader_url') | |
540 | else: | |
541 | # @a6211d2 | |
542 | uploader_url = url_or_none(microformats.get('ownerProfileUrl')) | |
543 | uploader_id = self._search_regex( | |
544 | r'(?:user|channel)/([^/]+)', uploader_url or '', 'uploader id', default=None) | |
545 | ||
546 | upload_date = unified_strdate( | |
547 | dict_get(microformats, ('uploadDate', 'publishDate')) | |
548 | or search_meta(['uploadDate', 'datePublished']) | |
549 | or self._search_regex( | |
550 | [r'(?s)id="eow-date.*?>(.*?)</span>', | |
551 | r'(?:id="watch-uploader-info".*?>.*?|["\']simpleText["\']\s*:\s*["\'])(?:Published|Uploaded|Streamed live|Started) on (.+?)[<"\']'], # @7998520 | |
552 | webpage, 'upload date', default=None)) | |
553 | ||
554 | return { | |
555 | 'title': video_title, | |
556 | 'description': description, | |
557 | 'upload_date': upload_date, | |
558 | 'uploader': uploader, | |
559 | 'channel_id': channel_id, | |
560 | 'channel_url': channel_url, | |
561 | 'duration': duration, | |
562 | 'uploader_url': uploader_url, | |
563 | 'uploader_id': uploader_id, | |
564 | } | |
565 | ||
566 | def _extract_thumbnails(self, video_id): | |
567 | try_all = 'thumbnails' in self._configuration_arg('check_all') | |
568 | thumbnail_base_urls = ['http://{server}/vi{webp}/{video_id}'.format( | |
569 | webp='_webp' if ext == 'webp' else '', video_id=video_id, server=server) | |
570 | 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',))] | |
571 | ||
572 | thumbnails = [] | |
573 | for url in thumbnail_base_urls: | |
574 | response = self._call_cdx_api( | |
575 | video_id, url, filters=['mimetype:image/(?:webp|jpeg)'], | |
576 | collapse=['urlkey'], query={'matchType': 'prefix'}) | |
577 | if not response: | |
578 | continue | |
579 | thumbnails.extend( | |
580 | { | |
581 | 'url': (self._WAYBACK_BASE_URL % (int_or_none(thumbnail_dict.get('timestamp')) or self._OLDEST_CAPTURE_DATE)) + thumbnail_dict.get('original'), | |
582 | 'filesize': int_or_none(thumbnail_dict.get('length')), | |
583 | 'preference': int_or_none(thumbnail_dict.get('length')) | |
584 | } for thumbnail_dict in response) | |
585 | if not try_all: | |
586 | break | |
587 | ||
588 | self._remove_duplicate_formats(thumbnails) | |
589 | return thumbnails | |
590 | ||
591 | def _get_capture_dates(self, video_id, url_date): | |
592 | capture_dates = [] | |
593 | # Note: CDX API will not find watch pages with extra params in the url. | |
594 | response = self._call_cdx_api( | |
595 | video_id, f'https://www.youtube.com/watch?v={video_id}', | |
596 | filters=['mimetype:text/html'], collapse=['timestamp:6', 'digest'], query={'matchType': 'prefix'}) or [] | |
86e5f3ed | 597 | all_captures = sorted(int_or_none(r['timestamp']) for r in response if int_or_none(r['timestamp']) is not None) |
aa4b0545 | 598 | |
599 | # Prefer the new polymer UI captures as we support extracting more metadata from them | |
600 | # WBM captures seem to all switch to this layout ~July 2020 | |
1f13021e | 601 | modern_captures = [x for x in all_captures if x >= 20200701000000] |
aa4b0545 | 602 | if modern_captures: |
603 | capture_dates.append(modern_captures[0]) | |
604 | capture_dates.append(url_date) | |
605 | if all_captures: | |
606 | capture_dates.append(all_captures[0]) | |
607 | ||
608 | if 'captures' in self._configuration_arg('check_all'): | |
609 | capture_dates.extend(modern_captures + all_captures) | |
610 | ||
611 | # Fallbacks if any of the above fail | |
612 | capture_dates.extend([self._OLDEST_CAPTURE_DATE, self._NEWEST_CAPTURE_DATE]) | |
1f13021e | 613 | return orderedSet(filter(None, capture_dates)) |
879e7199 | 614 | |
615 | def _real_extract(self, url): | |
1f13021e | 616 | video_id, url_date, url_date_2 = self._match_valid_url(url).group('id', 'date', 'date2') |
617 | url_date = url_date or url_date_2 | |
aa4b0545 | 618 | |
619 | urlh = None | |
879e7199 | 620 | try: |
aa4b0545 | 621 | urlh = self._request_webpage( |
622 | HEADRequest('https://web.archive.org/web/2oe_/http://wayback-fakeurl.archive.org/yt/%s' % video_id), | |
623 | video_id, note='Fetching archived video file url', expected_status=True) | |
879e7199 | 624 | except ExtractorError as e: |
625 | # HTTP Error 404 is expected if the video is not saved. | |
626 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404: | |
aa4b0545 | 627 | self.raise_no_formats( |
628 | 'The requested video is not archived, indexed, or there is an issue with web.archive.org', | |
879e7199 | 629 | expected=True) |
aa4b0545 | 630 | else: |
631 | raise | |
632 | ||
633 | capture_dates = self._get_capture_dates(video_id, int_or_none(url_date)) | |
1f13021e | 634 | self.write_debug('Captures to try: ' + join_nonempty(*capture_dates, delim=', ')) |
aa4b0545 | 635 | info = {'id': video_id} |
636 | for capture in capture_dates: | |
aa4b0545 | 637 | webpage = self._download_webpage( |
638 | (self._WAYBACK_BASE_URL + 'http://www.youtube.com/watch?v=%s') % (capture, video_id), | |
639 | video_id=video_id, fatal=False, errnote='unable to download capture webpage (it may not be archived)', | |
640 | note='Downloading capture webpage') | |
641 | current_info = self._extract_metadata(video_id, webpage or '') | |
642 | # Try avoid getting deleted video metadata | |
643 | if current_info.get('title'): | |
644 | info = merge_dicts(info, current_info) | |
645 | if 'captures' not in self._configuration_arg('check_all'): | |
646 | break | |
647 | ||
648 | info['thumbnails'] = self._extract_thumbnails(video_id) | |
649 | ||
650 | if urlh: | |
1f13021e | 651 | url = compat_urllib_parse_unquote(urlh.geturl()) |
aa4b0545 | 652 | video_file_url_qs = parse_qs(url) |
653 | # Attempt to recover any ext & format info from playback url & response headers | |
654 | format = {'url': url, 'filesize': int_or_none(urlh.headers.get('x-archive-orig-content-length'))} | |
655 | itag = try_get(video_file_url_qs, lambda x: x['itag'][0]) | |
656 | if itag and itag in YoutubeIE._formats: | |
657 | format.update(YoutubeIE._formats[itag]) | |
658 | format.update({'format_id': itag}) | |
659 | else: | |
660 | mime = try_get(video_file_url_qs, lambda x: x['mime'][0]) | |
661 | ext = (mimetype2ext(mime) | |
662 | or urlhandle_detect_ext(urlh) | |
663 | or mimetype2ext(urlh.headers.get('x-archive-guessed-content-type'))) | |
664 | format.update({'ext': ext}) | |
665 | info['formats'] = [format] | |
666 | if not info.get('duration'): | |
667 | info['duration'] = str_to_int(try_get(video_file_url_qs, lambda x: x['dur'][0])) | |
668 | ||
669 | if not info.get('title'): | |
670 | info['title'] = video_id | |
671 | return info |