]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/twitter.py
[twitter] Fix for private videos (#2772)
[yt-dlp.git] / yt_dlp / extractor / twitter.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_HTTPError,
9 compat_parse_qs,
10 compat_urllib_parse_unquote,
11 compat_urllib_parse_urlparse,
12 )
13 from ..utils import (
14 dict_get,
15 ExtractorError,
16 format_field,
17 float_or_none,
18 int_or_none,
19 traverse_obj,
20 try_get,
21 strip_or_none,
22 unified_timestamp,
23 update_url_query,
24 url_or_none,
25 xpath_text,
26 )
27
28 from .periscope import (
29 PeriscopeBaseIE,
30 PeriscopeIE,
31 )
32
33
34 class TwitterBaseIE(InfoExtractor):
35 _API_BASE = 'https://api.twitter.com/1.1/'
36 _BASE_REGEX = r'https?://(?:(?:www|m(?:obile)?)\.)?twitter\.com/'
37 _GUEST_TOKEN = None
38
39 def _extract_variant_formats(self, variant, video_id):
40 variant_url = variant.get('url')
41 if not variant_url:
42 return [], {}
43 elif '.m3u8' in variant_url:
44 return self._extract_m3u8_formats_and_subtitles(
45 variant_url, video_id, 'mp4', 'm3u8_native',
46 m3u8_id='hls', fatal=False)
47 else:
48 tbr = int_or_none(dict_get(variant, ('bitrate', 'bit_rate')), 1000) or None
49 f = {
50 'url': variant_url,
51 'format_id': 'http' + ('-%d' % tbr if tbr else ''),
52 'tbr': tbr,
53 }
54 self._search_dimensions_in_video_url(f, variant_url)
55 return [f], {}
56
57 def _extract_formats_from_vmap_url(self, vmap_url, video_id):
58 vmap_url = url_or_none(vmap_url)
59 if not vmap_url:
60 return [], {}
61 vmap_data = self._download_xml(vmap_url, video_id)
62 formats = []
63 subtitles = {}
64 urls = []
65 for video_variant in vmap_data.findall('.//{http://twitter.com/schema/videoVMapV2.xsd}videoVariant'):
66 video_variant.attrib['url'] = compat_urllib_parse_unquote(
67 video_variant.attrib['url'])
68 urls.append(video_variant.attrib['url'])
69 fmts, subs = self._extract_variant_formats(
70 video_variant.attrib, video_id)
71 formats.extend(fmts)
72 subtitles = self._merge_subtitles(subtitles, subs)
73 video_url = strip_or_none(xpath_text(vmap_data, './/MediaFile'))
74 if video_url not in urls:
75 fmts, subs = self._extract_variant_formats({'url': video_url}, video_id)
76 formats.extend(fmts)
77 subtitles = self._merge_subtitles(subtitles, subs)
78 return formats, subtitles
79
80 @staticmethod
81 def _search_dimensions_in_video_url(a_format, video_url):
82 m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
83 if m:
84 a_format.update({
85 'width': int(m.group('width')),
86 'height': int(m.group('height')),
87 })
88
89 def _call_api(self, path, video_id, query={}):
90 headers = {
91 'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw',
92 }
93 token = self._get_cookies(self._API_BASE).get('ct0')
94 if token:
95 headers['x-csrf-token'] = token.value
96 if not self._GUEST_TOKEN:
97 self._GUEST_TOKEN = self._download_json(
98 self._API_BASE + 'guest/activate.json', video_id,
99 'Downloading guest token', data=b'',
100 headers=headers)['guest_token']
101 headers['x-guest-token'] = self._GUEST_TOKEN
102 try:
103 return self._download_json(
104 self._API_BASE + path, video_id, headers=headers, query=query)
105 except ExtractorError as e:
106 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
107 raise ExtractorError(self._parse_json(
108 e.cause.read().decode(),
109 video_id)['errors'][0]['message'], expected=True)
110 raise
111
112
113 class TwitterCardIE(InfoExtractor):
114 IE_NAME = 'twitter:card'
115 _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/(?:cards/tfw/v1|videos(?:/tweet)?)/(?P<id>\d+)'
116 _TESTS = [
117 {
118 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
119 # MD5 checksums are different in different places
120 'info_dict': {
121 'id': '560070183650213889',
122 'ext': 'mp4',
123 'title': "Twitter - You can now shoot, edit and share video on Twitter. Capture life's most moving moments from your perspective.",
124 'description': 'md5:18d3e24bb4f6e5007487dd546e53bd96',
125 'uploader': 'Twitter',
126 'uploader_id': 'Twitter',
127 'thumbnail': r're:^https?://.*\.jpg',
128 'duration': 30.033,
129 'timestamp': 1422366112,
130 'upload_date': '20150127',
131 },
132 },
133 {
134 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
135 'md5': '7137eca597f72b9abbe61e5ae0161399',
136 'info_dict': {
137 'id': '623160978427936768',
138 'ext': 'mp4',
139 'title': "NASA - Fly over Pluto's icy Norgay Mountains and Sputnik Plain in this @NASANewHorizons #PlutoFlyby video.",
140 'description': "Fly over Pluto's icy Norgay Mountains and Sputnik Plain in this @NASANewHorizons #PlutoFlyby video. https://t.co/BJYgOjSeGA",
141 'uploader': 'NASA',
142 'uploader_id': 'NASA',
143 'timestamp': 1437408129,
144 'upload_date': '20150720',
145 },
146 },
147 {
148 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
149 'md5': 'b6d9683dd3f48e340ded81c0e917ad46',
150 'info_dict': {
151 'id': 'dq4Oj5quskI',
152 'ext': 'mp4',
153 'title': 'Ubuntu 11.10 Overview',
154 'description': 'md5:a831e97fa384863d6e26ce48d1c43376',
155 'upload_date': '20111013',
156 'uploader': 'OMG! UBUNTU!',
157 'uploader_id': 'omgubuntu',
158 },
159 'add_ie': ['Youtube'],
160 },
161 {
162 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
163 'md5': '6dabeaca9e68cbb71c99c322a4b42a11',
164 'info_dict': {
165 'id': 'iBb2x00UVlv',
166 'ext': 'mp4',
167 'upload_date': '20151113',
168 'uploader_id': '1189339351084113920',
169 'uploader': 'ArsenalTerje',
170 'title': 'Vine by ArsenalTerje',
171 'timestamp': 1447451307,
172 },
173 'add_ie': ['Vine'],
174 }, {
175 'url': 'https://twitter.com/i/videos/tweet/705235433198714880',
176 'md5': '884812a2adc8aaf6fe52b15ccbfa3b88',
177 'info_dict': {
178 'id': '705235433198714880',
179 'ext': 'mp4',
180 'title': "Brent Yarina - Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight.",
181 'description': "Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight. https://t.co/OrxcJ28Bns",
182 'uploader': 'Brent Yarina',
183 'uploader_id': 'BTNBrentYarina',
184 'timestamp': 1456976204,
185 'upload_date': '20160303',
186 },
187 'skip': 'This content is no longer available.',
188 }, {
189 'url': 'https://twitter.com/i/videos/752274308186120192',
190 'only_matching': True,
191 },
192 ]
193
194 def _real_extract(self, url):
195 status_id = self._match_id(url)
196 return self.url_result(
197 'https://twitter.com/statuses/' + status_id,
198 TwitterIE.ie_key(), status_id)
199
200
201 class TwitterIE(TwitterBaseIE):
202 IE_NAME = 'twitter'
203 _VALID_URL = TwitterBaseIE._BASE_REGEX + r'(?:(?:i/web|[^/]+)/status|statuses)/(?P<id>\d+)'
204
205 _TESTS = [{
206 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
207 'info_dict': {
208 'id': '643211948184596480',
209 'ext': 'mp4',
210 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
211 'thumbnail': r're:^https?://.*\.jpg',
212 'description': 'FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ',
213 'uploader': 'FREE THE NIPPLE',
214 'uploader_id': 'freethenipple',
215 'duration': 12.922,
216 'timestamp': 1442188653,
217 'upload_date': '20150913',
218 'age_limit': 18,
219 },
220 }, {
221 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
222 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
223 'info_dict': {
224 'id': '657991469417025536',
225 'ext': 'mp4',
226 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
227 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
228 'thumbnail': r're:^https?://.*\.png',
229 'uploader': 'Gifs',
230 'uploader_id': 'giphz',
231 },
232 'expected_warnings': ['height', 'width'],
233 'skip': 'Account suspended',
234 }, {
235 'url': 'https://twitter.com/starwars/status/665052190608723968',
236 'info_dict': {
237 'id': '665052190608723968',
238 'ext': 'mp4',
239 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
240 'description': 'A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens. https://t.co/OkSqT2fjWJ',
241 'uploader_id': 'starwars',
242 'uploader': 'Star Wars',
243 'timestamp': 1447395772,
244 'upload_date': '20151113',
245 },
246 }, {
247 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880',
248 'info_dict': {
249 'id': '705235433198714880',
250 'ext': 'mp4',
251 'title': "Brent Yarina - Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight.",
252 'description': "Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight. https://t.co/OrxcJ28Bns",
253 'uploader_id': 'BTNBrentYarina',
254 'uploader': 'Brent Yarina',
255 'timestamp': 1456976204,
256 'upload_date': '20160303',
257 },
258 'params': {
259 # The same video as https://twitter.com/i/videos/tweet/705235433198714880
260 # Test case of TwitterCardIE
261 'skip_download': True,
262 },
263 }, {
264 'url': 'https://twitter.com/jaydingeer/status/700207533655363584',
265 'info_dict': {
266 'id': '700207533655363584',
267 'ext': 'mp4',
268 'title': 'simon vertugo - BEAT PROD: @suhmeduh #Damndaniel',
269 'description': 'BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ',
270 'thumbnail': r're:^https?://.*\.jpg',
271 'uploader': 'simon vertugo',
272 'uploader_id': 'simonvertugo',
273 'duration': 30.0,
274 'timestamp': 1455777459,
275 'upload_date': '20160218',
276 },
277 }, {
278 'url': 'https://twitter.com/Filmdrunk/status/713801302971588609',
279 'md5': '89a15ed345d13b86e9a5a5e051fa308a',
280 'info_dict': {
281 'id': 'MIOxnrUteUd',
282 'ext': 'mp4',
283 'title': 'Dr.Pepperの飲み方 #japanese #バカ #ドクペ #電動ガン',
284 'uploader': 'TAKUMA',
285 'uploader_id': '1004126642786242560',
286 'timestamp': 1402826626,
287 'upload_date': '20140615',
288 },
289 'add_ie': ['Vine'],
290 }, {
291 'url': 'https://twitter.com/captainamerica/status/719944021058060289',
292 'info_dict': {
293 'id': '719944021058060289',
294 'ext': 'mp4',
295 'title': 'Captain America - @King0fNerd Are you sure you made the right choice? Find out in theaters.',
296 'description': '@King0fNerd Are you sure you made the right choice? Find out in theaters. https://t.co/GpgYi9xMJI',
297 'uploader_id': 'CaptainAmerica',
298 'uploader': 'Captain America',
299 'duration': 3.17,
300 'timestamp': 1460483005,
301 'upload_date': '20160412',
302 },
303 }, {
304 'url': 'https://twitter.com/OPP_HSD/status/779210622571536384',
305 'info_dict': {
306 'id': '1zqKVVlkqLaKB',
307 'ext': 'mp4',
308 'title': 'Sgt Kerry Schmidt - Ontario Provincial Police - Road rage, mischief, assault, rollover and fire in one occurrence',
309 'upload_date': '20160923',
310 'uploader_id': '1PmKqpJdOJQoY',
311 'uploader': 'Sgt Kerry Schmidt - Ontario Provincial Police',
312 'timestamp': 1474613214,
313 },
314 'add_ie': ['Periscope'],
315 }, {
316 # has mp4 formats via mobile API
317 'url': 'https://twitter.com/news_al3alm/status/852138619213144067',
318 'info_dict': {
319 'id': '852138619213144067',
320 'ext': 'mp4',
321 'title': 'عالم الأخبار - كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة',
322 'description': 'كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة https://t.co/xg6OhpyKfN',
323 'uploader': 'عالم الأخبار',
324 'uploader_id': 'news_al3alm',
325 'duration': 277.4,
326 'timestamp': 1492000653,
327 'upload_date': '20170412',
328 },
329 'skip': 'Account suspended',
330 }, {
331 'url': 'https://twitter.com/i/web/status/910031516746514432',
332 'info_dict': {
333 'id': '910031516746514432',
334 'ext': 'mp4',
335 'title': 'Préfet de Guadeloupe - [Direct] #Maria Le centre se trouve actuellement au sud de Basse-Terre. Restez confinés. Réfugiez-vous dans la pièce la + sûre.',
336 'thumbnail': r're:^https?://.*\.jpg',
337 'description': '[Direct] #Maria Le centre se trouve actuellement au sud de Basse-Terre. Restez confinés. Réfugiez-vous dans la pièce la + sûre. https://t.co/mwx01Rs4lo',
338 'uploader': 'Préfet de Guadeloupe',
339 'uploader_id': 'Prefet971',
340 'duration': 47.48,
341 'timestamp': 1505803395,
342 'upload_date': '20170919',
343 },
344 'params': {
345 'skip_download': True, # requires ffmpeg
346 },
347 }, {
348 # card via api.twitter.com/1.1/videos/tweet/config
349 'url': 'https://twitter.com/LisPower1/status/1001551623938805763',
350 'info_dict': {
351 'id': '1001551623938805763',
352 'ext': 'mp4',
353 'title': 're:.*?Shep is on a roll today.*?',
354 'thumbnail': r're:^https?://.*\.jpg',
355 'description': 'md5:37b9f2ff31720cef23b2bd42ee8a0f09',
356 'uploader': 'Lis Power',
357 'uploader_id': 'LisPower1',
358 'duration': 111.278,
359 'timestamp': 1527623489,
360 'upload_date': '20180529',
361 },
362 'params': {
363 'skip_download': True, # requires ffmpeg
364 },
365 }, {
366 'url': 'https://twitter.com/foobar/status/1087791357756956680',
367 'info_dict': {
368 'id': '1087791357756956680',
369 'ext': 'mp4',
370 'title': 'Twitter - A new is coming. Some of you got an opt-in to try it now. Check out the emoji button, quick keyboard shortcuts, upgraded trends, advanced search, and more. Let us know your thoughts!',
371 'thumbnail': r're:^https?://.*\.jpg',
372 'description': 'md5:6dfd341a3310fb97d80d2bf7145df976',
373 'uploader': 'Twitter',
374 'uploader_id': 'Twitter',
375 'duration': 61.567,
376 'timestamp': 1548184644,
377 'upload_date': '20190122',
378 },
379 }, {
380 # not available in Periscope
381 'url': 'https://twitter.com/ViviEducation/status/1136534865145286656',
382 'info_dict': {
383 'id': '1vOGwqejwoWxB',
384 'ext': 'mp4',
385 'title': 'Vivi - Vivi founder @lior_rauchy announcing our new student feedback tool live at @EduTECH_AU #EduTECH2019',
386 'uploader': 'Vivi',
387 'uploader_id': '1eVjYOLGkGrQL',
388 },
389 'add_ie': ['TwitterBroadcast'],
390 }, {
391 # unified card
392 'url': 'https://twitter.com/BrooklynNets/status/1349794411333394432?s=20',
393 'info_dict': {
394 'id': '1349794411333394432',
395 'ext': 'mp4',
396 'title': 'md5:d1c4941658e4caaa6cb579260d85dcba',
397 'thumbnail': r're:^https?://.*\.jpg',
398 'description': 'md5:71ead15ec44cee55071547d6447c6a3e',
399 'uploader': 'Brooklyn Nets',
400 'uploader_id': 'BrooklynNets',
401 'duration': 324.484,
402 'timestamp': 1610651040,
403 'upload_date': '20210114',
404 },
405 'params': {
406 'skip_download': True,
407 },
408 }, {
409 # Twitch Clip Embed
410 'url': 'https://twitter.com/GunB1g/status/1163218564784017422',
411 'only_matching': True,
412 }, {
413 # promo_video_website card
414 'url': 'https://twitter.com/GunB1g/status/1163218564784017422',
415 'only_matching': True,
416 }, {
417 # promo_video_convo card
418 'url': 'https://twitter.com/poco_dandy/status/1047395834013384704',
419 'only_matching': True,
420 }, {
421 # appplayer card
422 'url': 'https://twitter.com/poco_dandy/status/1150646424461176832',
423 'only_matching': True,
424 }, {
425 # video_direct_message card
426 'url': 'https://twitter.com/qarev001/status/1348948114569269251',
427 'only_matching': True,
428 }, {
429 # poll2choice_video card
430 'url': 'https://twitter.com/CAF_Online/status/1349365911120195585',
431 'only_matching': True,
432 }, {
433 # poll3choice_video card
434 'url': 'https://twitter.com/SamsungMobileSA/status/1348609186725289984',
435 'only_matching': True,
436 }, {
437 # poll4choice_video card
438 'url': 'https://twitter.com/SouthamptonFC/status/1347577658079641604',
439 'only_matching': True,
440 }]
441
442 def _real_extract(self, url):
443 twid = self._match_id(url)
444 status = self._call_api(
445 'statuses/show/%s.json' % twid, twid, {
446 'cards_platform': 'Web-12',
447 'include_cards': 1,
448 'include_reply_count': 1,
449 'include_user_entities': 0,
450 'tweet_mode': 'extended',
451 })
452
453 title = description = status['full_text'].replace('\n', ' ')
454 # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
455 title = re.sub(r'\s+(https?://[^ ]+)', '', title)
456 user = status.get('user') or {}
457 uploader = user.get('name')
458 if uploader:
459 title = '%s - %s' % (uploader, title)
460 uploader_id = user.get('screen_name')
461
462 tags = []
463 for hashtag in (try_get(status, lambda x: x['entities']['hashtags'], list) or []):
464 hashtag_text = hashtag.get('text')
465 if not hashtag_text:
466 continue
467 tags.append(hashtag_text)
468
469 info = {
470 'id': twid,
471 'title': title,
472 'description': description,
473 'uploader': uploader,
474 'timestamp': unified_timestamp(status.get('created_at')),
475 'uploader_id': uploader_id,
476 'uploader_url': format_field(uploader_id, template='https://twitter.com/%s'),
477 'like_count': int_or_none(status.get('favorite_count')),
478 'repost_count': int_or_none(status.get('retweet_count')),
479 'comment_count': int_or_none(status.get('reply_count')),
480 'age_limit': 18 if status.get('possibly_sensitive') else 0,
481 'tags': tags,
482 }
483
484 def extract_from_video_info(media):
485 video_info = media.get('video_info') or {}
486
487 formats = []
488 subtitles = {}
489 for variant in video_info.get('variants', []):
490 fmts, subs = self._extract_variant_formats(variant, twid)
491 subtitles = self._merge_subtitles(subtitles, subs)
492 formats.extend(fmts)
493 self._sort_formats(formats, ('res', 'br', 'size', 'proto')) # The codec of http formats are unknown
494
495 thumbnails = []
496 media_url = media.get('media_url_https') or media.get('media_url')
497 if media_url:
498 def add_thumbnail(name, size):
499 thumbnails.append({
500 'id': name,
501 'url': update_url_query(media_url, {'name': name}),
502 'width': int_or_none(size.get('w') or size.get('width')),
503 'height': int_or_none(size.get('h') or size.get('height')),
504 })
505 for name, size in media.get('sizes', {}).items():
506 add_thumbnail(name, size)
507 add_thumbnail('orig', media.get('original_info') or {})
508
509 info.update({
510 'formats': formats,
511 'subtitles': subtitles,
512 'thumbnails': thumbnails,
513 'duration': float_or_none(video_info.get('duration_millis'), 1000),
514 })
515
516 media = traverse_obj(status, ((None, 'quoted_status'), 'extended_entities', 'media', 0), get_all=False)
517 if media and media.get('type') != 'photo':
518 extract_from_video_info(media)
519 else:
520 card = status.get('card')
521 if card:
522 binding_values = card['binding_values']
523
524 def get_binding_value(k):
525 o = binding_values.get(k) or {}
526 return try_get(o, lambda x: x[x['type'].lower() + '_value'])
527
528 card_name = card['name'].split(':')[-1]
529 if card_name == 'player':
530 info.update({
531 '_type': 'url',
532 'url': get_binding_value('player_url'),
533 })
534 elif card_name == 'periscope_broadcast':
535 info.update({
536 '_type': 'url',
537 'url': get_binding_value('url') or get_binding_value('player_url'),
538 'ie_key': PeriscopeIE.ie_key(),
539 })
540 elif card_name == 'broadcast':
541 info.update({
542 '_type': 'url',
543 'url': get_binding_value('broadcast_url'),
544 'ie_key': TwitterBroadcastIE.ie_key(),
545 })
546 elif card_name == 'summary':
547 info.update({
548 '_type': 'url',
549 'url': get_binding_value('card_url'),
550 })
551 elif card_name == 'unified_card':
552 media_entities = self._parse_json(get_binding_value('unified_card'), twid)['media_entities']
553 extract_from_video_info(next(iter(media_entities.values())))
554 # amplify, promo_video_website, promo_video_convo, appplayer,
555 # video_direct_message, poll2choice_video, poll3choice_video,
556 # poll4choice_video, ...
557 else:
558 is_amplify = card_name == 'amplify'
559 vmap_url = get_binding_value('amplify_url_vmap') if is_amplify else get_binding_value('player_stream_url')
560 content_id = get_binding_value('%s_content_id' % (card_name if is_amplify else 'player'))
561 formats, subtitles = self._extract_formats_from_vmap_url(vmap_url, content_id or twid)
562 self._sort_formats(formats)
563
564 thumbnails = []
565 for suffix in ('_small', '', '_large', '_x_large', '_original'):
566 image = get_binding_value('player_image' + suffix) or {}
567 image_url = image.get('url')
568 if not image_url or '/player-placeholder' in image_url:
569 continue
570 thumbnails.append({
571 'id': suffix[1:] if suffix else 'medium',
572 'url': image_url,
573 'width': int_or_none(image.get('width')),
574 'height': int_or_none(image.get('height')),
575 })
576
577 info.update({
578 'formats': formats,
579 'subtitles': subtitles,
580 'thumbnails': thumbnails,
581 'duration': int_or_none(get_binding_value(
582 'content_duration_seconds')),
583 })
584 else:
585 expanded_url = try_get(status, lambda x: x['entities']['urls'][0]['expanded_url'])
586 if not expanded_url:
587 raise ExtractorError("There's no video in this tweet.")
588 info.update({
589 '_type': 'url',
590 'url': expanded_url,
591 })
592 return info
593
594
595 class TwitterAmplifyIE(TwitterBaseIE):
596 IE_NAME = 'twitter:amplify'
597 _VALID_URL = r'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
598
599 _TEST = {
600 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
601 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
602 'info_dict': {
603 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
604 'ext': 'mp4',
605 'title': 'Twitter Video',
606 'thumbnail': 're:^https?://.*',
607 },
608 }
609
610 def _real_extract(self, url):
611 video_id = self._match_id(url)
612 webpage = self._download_webpage(url, video_id)
613
614 vmap_url = self._html_search_meta(
615 'twitter:amplify:vmap', webpage, 'vmap url')
616 formats = self._extract_formats_from_vmap_url(vmap_url, video_id)
617
618 thumbnails = []
619 thumbnail = self._html_search_meta(
620 'twitter:image:src', webpage, 'thumbnail', fatal=False)
621
622 def _find_dimension(target):
623 w = int_or_none(self._html_search_meta(
624 'twitter:%s:width' % target, webpage, fatal=False))
625 h = int_or_none(self._html_search_meta(
626 'twitter:%s:height' % target, webpage, fatal=False))
627 return w, h
628
629 if thumbnail:
630 thumbnail_w, thumbnail_h = _find_dimension('image')
631 thumbnails.append({
632 'url': thumbnail,
633 'width': thumbnail_w,
634 'height': thumbnail_h,
635 })
636
637 video_w, video_h = _find_dimension('player')
638 formats[0].update({
639 'width': video_w,
640 'height': video_h,
641 })
642
643 return {
644 'id': video_id,
645 'title': 'Twitter Video',
646 'formats': formats,
647 'thumbnails': thumbnails,
648 }
649
650
651 class TwitterBroadcastIE(TwitterBaseIE, PeriscopeBaseIE):
652 IE_NAME = 'twitter:broadcast'
653 _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/broadcasts/(?P<id>[0-9a-zA-Z]{13})'
654
655 _TEST = {
656 # untitled Periscope video
657 'url': 'https://twitter.com/i/broadcasts/1yNGaQLWpejGj',
658 'info_dict': {
659 'id': '1yNGaQLWpejGj',
660 'ext': 'mp4',
661 'title': 'Andrea May Sahouri - Periscope Broadcast',
662 'uploader': 'Andrea May Sahouri',
663 'uploader_id': '1PXEdBZWpGwKe',
664 },
665 }
666
667 def _real_extract(self, url):
668 broadcast_id = self._match_id(url)
669 broadcast = self._call_api(
670 'broadcasts/show.json', broadcast_id,
671 {'ids': broadcast_id})['broadcasts'][broadcast_id]
672 info = self._parse_broadcast_data(broadcast, broadcast_id)
673 media_key = broadcast['media_key']
674 source = self._call_api(
675 'live_video_stream/status/' + media_key, media_key)['source']
676 m3u8_url = source.get('noRedirectPlaybackUrl') or source['location']
677 if '/live_video_stream/geoblocked/' in m3u8_url:
678 self.raise_geo_restricted()
679 m3u8_id = compat_parse_qs(compat_urllib_parse_urlparse(
680 m3u8_url).query).get('type', [None])[0]
681 state, width, height = self._extract_common_format_info(broadcast)
682 info['formats'] = self._extract_pscp_m3u8_formats(
683 m3u8_url, broadcast_id, m3u8_id, state, width, height)
684 return info
685
686
687 class TwitterShortenerIE(TwitterBaseIE):
688 IE_NAME = 'twitter:shortener'
689 _VALID_URL = r'https?://t.co/(?P<id>[^?]+)|tco:(?P<eid>[^?]+)'
690 _BASE_URL = 'https://t.co/'
691
692 def _real_extract(self, url):
693 mobj = self._match_valid_url(url)
694 eid, id = mobj.group('eid', 'id')
695 if eid:
696 id = eid
697 url = self._BASE_URL + id
698 new_url = self._request_webpage(url, id, headers={'User-Agent': 'curl'}).geturl()
699 __UNSAFE_LINK = "https://twitter.com/safety/unsafe_link_warning?unsafe_link="
700 if new_url.startswith(__UNSAFE_LINK):
701 new_url = new_url.replace(__UNSAFE_LINK, "")
702 return self.url_result(new_url)