2 from __future__
import unicode_literals
6 from .common
import InfoExtractor
10 compat_urllib_parse_unquote
,
11 compat_urllib_parse_urlparse
,
25 from .periscope
import (
31 class TwitterBaseIE(InfoExtractor
):
32 _API_BASE
= 'https://api.twitter.com/1.1/'
33 _BASE_REGEX
= r
'https?://(?:(?:www|m(?:obile)?)\.)?twitter\.com/'
36 def _extract_variant_formats(self
, variant
, video_id
):
37 variant_url
= variant
.get('url')
40 elif '.m3u8' in variant_url
:
41 return self
._extract
_m
3u8_formats
(
42 variant_url
, video_id
, 'mp4', 'm3u8_native',
43 m3u8_id
='hls', fatal
=False)
45 tbr
= int_or_none(dict_get(variant
, ('bitrate', 'bit_rate')), 1000) or None
48 'format_id': 'http' + ('-%d' % tbr
if tbr
else ''),
51 self
._search
_dimensions
_in
_video
_url
(f
, variant_url
)
54 def _extract_formats_from_vmap_url(self
, vmap_url
, video_id
):
55 vmap_data
= self
._download
_xml
(vmap_url
, video_id
)
58 for video_variant
in vmap_data
.findall('.//{http://twitter.com/schema/videoVMapV2.xsd}videoVariant'):
59 video_variant
.attrib
['url'] = compat_urllib_parse_unquote(
60 video_variant
.attrib
['url'])
61 urls
.append(video_variant
.attrib
['url'])
62 formats
.extend(self
._extract
_variant
_formats
(
63 video_variant
.attrib
, video_id
))
64 video_url
= strip_or_none(xpath_text(vmap_data
, './/MediaFile'))
65 if video_url
not in urls
:
66 formats
.extend(self
._extract
_variant
_formats
({'url': video_url}
, video_id
))
70 def _search_dimensions_in_video_url(a_format
, video_url
):
71 m
= re
.search(r
'/(?P<width>\d+)x(?P<height>\d+)/', video_url
)
74 'width': int(m
.group('width')),
75 'height': int(m
.group('height')),
78 def _call_api(self
, path
, video_id
, query
={}):
80 'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw',
82 if not self
._GUEST
_TOKEN
:
83 self
._GUEST
_TOKEN
= self
._download
_json
(
84 self
._API
_BASE
+ 'guest/activate.json', video_id
,
85 'Downloading guest token', data
=b
'',
86 headers
=headers
)['guest_token']
87 headers
['x-guest-token'] = self
._GUEST
_TOKEN
89 return self
._download
_json
(
90 self
._API
_BASE
+ path
, video_id
, headers
=headers
, query
=query
)
91 except ExtractorError
as e
:
92 if isinstance(e
.cause
, compat_HTTPError
) and e
.cause
.code
== 403:
93 raise ExtractorError(self
._parse
_json
(
94 e
.cause
.read().decode(),
95 video_id
)['errors'][0]['message'], expected
=True)
99 class TwitterCardIE(InfoExtractor
):
100 IE_NAME
= 'twitter:card'
101 _VALID_URL
= TwitterBaseIE
._BASE
_REGEX
+ r
'i/(?:cards/tfw/v1|videos(?:/tweet)?)/(?P<id>\d+)'
104 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
105 # MD5 checksums are different in different places
107 'id': '560070183650213889',
109 'title': "Twitter - You can now shoot, edit and share video on Twitter. Capture life's most moving moments from your perspective.",
110 'description': 'md5:18d3e24bb4f6e5007487dd546e53bd96',
111 'uploader': 'Twitter',
112 'uploader_id': 'Twitter',
113 'thumbnail': r
're:^https?://.*\.jpg',
115 'timestamp': 1422366112,
116 'upload_date': '20150127',
120 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
121 'md5': '7137eca597f72b9abbe61e5ae0161399',
123 'id': '623160978427936768',
125 'title': "NASA - Fly over Pluto's icy Norgay Mountains and Sputnik Plain in this @NASANewHorizons #PlutoFlyby video.",
126 'description': "Fly over Pluto's icy Norgay Mountains and Sputnik Plain in this @NASANewHorizons #PlutoFlyby video. https://t.co/BJYgOjSeGA",
128 'uploader_id': 'NASA',
129 'timestamp': 1437408129,
130 'upload_date': '20150720',
134 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
135 'md5': 'b6d9683dd3f48e340ded81c0e917ad46',
139 'title': 'Ubuntu 11.10 Overview',
140 'description': 'md5:a831e97fa384863d6e26ce48d1c43376',
141 'upload_date': '20111013',
142 'uploader': 'OMG! UBUNTU!',
143 'uploader_id': 'omgubuntu',
145 'add_ie': ['Youtube'],
148 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
149 'md5': '6dabeaca9e68cbb71c99c322a4b42a11',
153 'upload_date': '20151113',
154 'uploader_id': '1189339351084113920',
155 'uploader': 'ArsenalTerje',
156 'title': 'Vine by ArsenalTerje',
157 'timestamp': 1447451307,
161 'url': 'https://twitter.com/i/videos/tweet/705235433198714880',
162 'md5': '884812a2adc8aaf6fe52b15ccbfa3b88',
164 'id': '705235433198714880',
166 'title': "Brent Yarina - Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight.",
167 'description': "Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight. https://t.co/OrxcJ28Bns",
168 'uploader': 'Brent Yarina',
169 'uploader_id': 'BTNBrentYarina',
170 'timestamp': 1456976204,
171 'upload_date': '20160303',
173 'skip': 'This content is no longer available.',
175 'url': 'https://twitter.com/i/videos/752274308186120192',
176 'only_matching': True,
180 def _real_extract(self
, url
):
181 status_id
= self
._match
_id
(url
)
182 return self
.url_result(
183 'https://twitter.com/statuses/' + status_id
,
184 TwitterIE
.ie_key(), status_id
)
187 class TwitterIE(TwitterBaseIE
):
189 _VALID_URL
= TwitterBaseIE
._BASE
_REGEX
+ r
'(?:(?:i/web|[^/]+)/status|statuses)/(?P<id>\d+)'
192 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
194 'id': '643211948184596480',
196 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
197 'thumbnail': r
're:^https?://.*\.jpg',
198 'description': 'FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ',
199 'uploader': 'FREE THE NIPPLE',
200 'uploader_id': 'freethenipple',
202 'timestamp': 1442188653,
203 'upload_date': '20150913',
207 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
208 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
210 'id': '657991469417025536',
212 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
213 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
214 'thumbnail': r
're:^https?://.*\.png',
216 'uploader_id': 'giphz',
218 'expected_warnings': ['height', 'width'],
219 'skip': 'Account suspended',
221 'url': 'https://twitter.com/starwars/status/665052190608723968',
223 'id': '665052190608723968',
225 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
226 'description': 'A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens. https://t.co/OkSqT2fjWJ',
227 'uploader_id': 'starwars',
228 'uploader': 'Star Wars',
229 'timestamp': 1447395772,
230 'upload_date': '20151113',
233 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880',
235 'id': '705235433198714880',
237 'title': "Brent Yarina - Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight.",
238 'description': "Khalil Iverson's missed highlight dunk. And made highlight dunk. In one highlight. https://t.co/OrxcJ28Bns",
239 'uploader_id': 'BTNBrentYarina',
240 'uploader': 'Brent Yarina',
241 'timestamp': 1456976204,
242 'upload_date': '20160303',
245 # The same video as https://twitter.com/i/videos/tweet/705235433198714880
246 # Test case of TwitterCardIE
247 'skip_download': True,
250 'url': 'https://twitter.com/jaydingeer/status/700207533655363584',
252 'id': '700207533655363584',
254 'title': 'simon vertugo - BEAT PROD: @suhmeduh #Damndaniel',
255 'description': 'BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ',
256 'thumbnail': r
're:^https?://.*\.jpg',
257 'uploader': 'simon vertugo',
258 'uploader_id': 'simonvertugo',
260 'timestamp': 1455777459,
261 'upload_date': '20160218',
264 'url': 'https://twitter.com/Filmdrunk/status/713801302971588609',
265 'md5': '89a15ed345d13b86e9a5a5e051fa308a',
269 'title': 'Dr.Pepperの飲み方 #japanese #バカ #ドクペ #電動ガン',
270 'uploader': 'TAKUMA',
271 'uploader_id': '1004126642786242560',
272 'timestamp': 1402826626,
273 'upload_date': '20140615',
277 'url': 'https://twitter.com/captainamerica/status/719944021058060289',
279 'id': '719944021058060289',
281 'title': 'Captain America - @King0fNerd Are you sure you made the right choice? Find out in theaters.',
282 'description': '@King0fNerd Are you sure you made the right choice? Find out in theaters. https://t.co/GpgYi9xMJI',
283 'uploader_id': 'CaptainAmerica',
284 'uploader': 'Captain America',
286 'timestamp': 1460483005,
287 'upload_date': '20160412',
290 'url': 'https://twitter.com/OPP_HSD/status/779210622571536384',
292 'id': '1zqKVVlkqLaKB',
294 'title': 'Sgt Kerry Schmidt - Ontario Provincial Police - Road rage, mischief, assault, rollover and fire in one occurrence',
295 'upload_date': '20160923',
296 'uploader_id': '1PmKqpJdOJQoY',
297 'uploader': 'Sgt Kerry Schmidt - Ontario Provincial Police',
298 'timestamp': 1474613214,
300 'add_ie': ['Periscope'],
302 # has mp4 formats via mobile API
303 'url': 'https://twitter.com/news_al3alm/status/852138619213144067',
305 'id': '852138619213144067',
307 'title': 'عالم الأخبار - كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة',
308 'description': 'كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة https://t.co/xg6OhpyKfN',
309 'uploader': 'عالم الأخبار',
310 'uploader_id': 'news_al3alm',
312 'timestamp': 1492000653,
313 'upload_date': '20170412',
315 'skip': 'Account suspended',
317 'url': 'https://twitter.com/i/web/status/910031516746514432',
319 'id': '910031516746514432',
321 '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.',
322 'thumbnail': r
're:^https?://.*\.jpg',
323 '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',
324 'uploader': 'Préfet de Guadeloupe',
325 'uploader_id': 'Prefet971',
327 'timestamp': 1505803395,
328 'upload_date': '20170919',
331 'skip_download': True, # requires ffmpeg
334 # card via api.twitter.com/1.1/videos/tweet/config
335 'url': 'https://twitter.com/LisPower1/status/1001551623938805763',
337 'id': '1001551623938805763',
339 'title': 're:.*?Shep is on a roll today.*?',
340 'thumbnail': r
're:^https?://.*\.jpg',
341 'description': 'md5:37b9f2ff31720cef23b2bd42ee8a0f09',
342 'uploader': 'Lis Power',
343 'uploader_id': 'LisPower1',
345 'timestamp': 1527623489,
346 'upload_date': '20180529',
349 'skip_download': True, # requires ffmpeg
352 'url': 'https://twitter.com/foobar/status/1087791357756956680',
354 'id': '1087791357756956680',
356 '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!',
357 'thumbnail': r
're:^https?://.*\.jpg',
358 'description': 'md5:6dfd341a3310fb97d80d2bf7145df976',
359 'uploader': 'Twitter',
360 'uploader_id': 'Twitter',
362 'timestamp': 1548184644,
363 'upload_date': '20190122',
366 # not available in Periscope
367 'url': 'https://twitter.com/ViviEducation/status/1136534865145286656',
369 'id': '1vOGwqejwoWxB',
371 'title': 'Vivi - Vivi founder @lior_rauchy announcing our new student feedback tool live at @EduTECH_AU #EduTECH2019',
373 'uploader_id': '1eVjYOLGkGrQL',
375 'add_ie': ['TwitterBroadcast'],
378 'url': 'https://twitter.com/GunB1g/status/1163218564784017422',
379 'only_matching': True,
381 # promo_video_website card
382 'url': 'https://twitter.com/GunB1g/status/1163218564784017422',
383 'only_matching': True,
385 # promo_video_convo card
386 'url': 'https://twitter.com/poco_dandy/status/1047395834013384704',
387 'only_matching': True,
390 'url': 'https://twitter.com/poco_dandy/status/1150646424461176832',
391 'only_matching': True,
394 def _real_extract(self
, url
):
395 twid
= self
._match
_id
(url
)
396 status
= self
._call
_api
(
397 'statuses/show/%s.json' % twid
, twid
, {
398 'cards_platform': 'Web-12',
400 'include_reply_count': 1,
401 'include_user_entities': 0,
402 'tweet_mode': 'extended',
405 title
= description
= status
['full_text'].replace('\n', ' ')
406 # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
407 title
= re
.sub(r
'\s+(https?://[^ ]+)', '', title
)
408 user
= status
.get('user') or {}
409 uploader
= user
.get('name')
411 title
= '%s - %s' % (uploader
, title
)
412 uploader_id
= user
.get('screen_name')
415 for hashtag
in (try_get(status
, lambda x
: x
['entities']['hashtags'], list) or []):
416 hashtag_text
= hashtag
.get('text')
419 tags
.append(hashtag_text
)
424 'description': description
,
425 'uploader': uploader
,
426 'timestamp': unified_timestamp(status
.get('created_at')),
427 'uploader_id': uploader_id
,
428 'uploader_url': 'https://twitter.com/' + uploader_id
if uploader_id
else None,
429 'like_count': int_or_none(status
.get('favorite_count')),
430 'repost_count': int_or_none(status
.get('retweet_count')),
431 'comment_count': int_or_none(status
.get('reply_count')),
432 'age_limit': 18 if status
.get('possibly_sensitive') else 0,
436 media
= try_get(status
, lambda x
: x
['extended_entities']['media'][0])
437 if media
and media
.get('type') != 'photo':
438 video_info
= media
.get('video_info') or {}
441 for variant
in video_info
.get('variants', []):
442 formats
.extend(self
._extract
_variant
_formats
(variant
, twid
))
443 self
._sort
_formats
(formats
)
446 media_url
= media
.get('media_url_https') or media
.get('media_url')
448 def add_thumbnail(name
, size
):
451 'url': update_url_query(media_url
, {'name': name}
),
452 'width': int_or_none(size
.get('w') or size
.get('width')),
453 'height': int_or_none(size
.get('h') or size
.get('height')),
455 for name
, size
in media
.get('sizes', {}).items():
456 add_thumbnail(name
, size
)
457 add_thumbnail('orig', media
.get('original_info') or {})
461 'thumbnails': thumbnails
,
462 'duration': float_or_none(video_info
.get('duration_millis'), 1000),
465 card
= status
.get('card')
467 binding_values
= card
['binding_values']
469 def get_binding_value(k
):
470 o
= binding_values
.get(k
) or {}
471 return try_get(o
, lambda x
: x
[x
['type'].lower() + '_value'])
473 card_name
= card
['name'].split(':')[-1]
474 if card_name
== 'player':
477 'url': get_binding_value('player_url'),
479 elif card_name
== 'periscope_broadcast':
482 'url': get_binding_value('url') or get_binding_value('player_url'),
483 'ie_key': PeriscopeIE
.ie_key(),
485 elif card_name
== 'broadcast':
488 'url': get_binding_value('broadcast_url'),
489 'ie_key': TwitterBroadcastIE
.ie_key(),
491 elif card_name
== 'summary':
494 'url': get_binding_value('card_url'),
496 # amplify, promo_video_website, promo_video_convo, appplayer, ...
498 is_amplify
= card_name
== 'amplify'
499 vmap_url
= get_binding_value('amplify_url_vmap') if is_amplify
else get_binding_value('player_stream_url')
500 content_id
= get_binding_value('%s_content_id' % (card_name
if is_amplify
else 'player'))
501 formats
= self
._extract
_formats
_from
_vmap
_url
(vmap_url
, content_id
or twid
)
502 self
._sort
_formats
(formats
)
505 for suffix
in ('_small', '', '_large', '_x_large', '_original'):
506 image
= get_binding_value('player_image' + suffix
) or {}
507 image_url
= image
.get('url')
508 if not image_url
or '/player-placeholder' in image_url
:
511 'id': suffix
[1:] if suffix
else 'medium',
513 'width': int_or_none(image
.get('width')),
514 'height': int_or_none(image
.get('height')),
519 'thumbnails': thumbnails
,
520 'duration': int_or_none(get_binding_value(
521 'content_duration_seconds')),
524 expanded_url
= try_get(status
, lambda x
: x
['entities']['urls'][0]['expanded_url'])
526 raise ExtractorError("There's no video in this tweet.")
534 class TwitterAmplifyIE(TwitterBaseIE
):
535 IE_NAME
= 'twitter:amplify'
536 _VALID_URL
= r
'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
539 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
540 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
542 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
544 'title': 'Twitter Video',
545 'thumbnail': 're:^https?://.*',
549 def _real_extract(self
, url
):
550 video_id
= self
._match
_id
(url
)
551 webpage
= self
._download
_webpage
(url
, video_id
)
553 vmap_url
= self
._html
_search
_meta
(
554 'twitter:amplify:vmap', webpage
, 'vmap url')
555 formats
= self
._extract
_formats
_from
_vmap
_url
(vmap_url
, video_id
)
558 thumbnail
= self
._html
_search
_meta
(
559 'twitter:image:src', webpage
, 'thumbnail', fatal
=False)
561 def _find_dimension(target
):
562 w
= int_or_none(self
._html
_search
_meta
(
563 'twitter:%s:width' % target
, webpage
, fatal
=False))
564 h
= int_or_none(self
._html
_search
_meta
(
565 'twitter:%s:height' % target
, webpage
, fatal
=False))
569 thumbnail_w
, thumbnail_h
= _find_dimension('image')
572 'width': thumbnail_w
,
573 'height': thumbnail_h
,
576 video_w
, video_h
= _find_dimension('player')
584 'title': 'Twitter Video',
586 'thumbnails': thumbnails
,
590 class TwitterBroadcastIE(TwitterBaseIE
, PeriscopeBaseIE
):
591 IE_NAME
= 'twitter:broadcast'
592 _VALID_URL
= TwitterBaseIE
._BASE
_REGEX
+ r
'i/broadcasts/(?P<id>[0-9a-zA-Z]{13})'
595 # untitled Periscope video
596 'url': 'https://twitter.com/i/broadcasts/1yNGaQLWpejGj',
598 'id': '1yNGaQLWpejGj',
600 'title': 'Andrea May Sahouri - Periscope Broadcast',
601 'uploader': 'Andrea May Sahouri',
602 'uploader_id': '1PXEdBZWpGwKe',
606 def _real_extract(self
, url
):
607 broadcast_id
= self
._match
_id
(url
)
608 broadcast
= self
._call
_api
(
609 'broadcasts/show.json', broadcast_id
,
610 {'ids': broadcast_id}
)['broadcasts'][broadcast_id
]
611 info
= self
._parse
_broadcast
_data
(broadcast
, broadcast_id
)
612 media_key
= broadcast
['media_key']
613 source
= self
._call
_api
(
614 'live_video_stream/status/' + media_key
, media_key
)['source']
615 m3u8_url
= source
.get('noRedirectPlaybackUrl') or source
['location']
616 if '/live_video_stream/geoblocked/' in m3u8_url
:
617 self
.raise_geo_restricted()
618 m3u8_id
= compat_parse_qs(compat_urllib_parse_urlparse(
619 m3u8_url
).query
).get('type', [None])[0]
620 state
, width
, height
= self
._extract
_common
_format
_info
(broadcast
)
621 info
['formats'] = self
._extract
_pscp
_m
3u8_formats
(
622 m3u8_url
, broadcast_id
, m3u8_id
, state
, width
, height
)
626 class TwitterShortenerIE(TwitterBaseIE
):
627 IE_NAME
= 'twitter:shortener'
628 _VALID_URL
= r
'https?://t.co/(?P<id>[^?]+)|tco:(?P<eid>[^?]+)'
629 _BASE_URL
= 'https://t.co/'
631 def _real_extract(self
, url
):
632 mobj
= re
.match(self
._VALID
_URL
, url
)
633 eid
, id = mobj
.group('eid', 'id')
636 url
= self
._BASE
_URL
+ id
637 new_url
= self
._request
_webpage
(url
, id, headers
={'User-Agent': 'curl'}
).geturl()
638 __UNSAFE_LINK
= "https://twitter.com/safety/unsafe_link_warning?unsafe_link="
639 if new_url
.startswith(__UNSAFE_LINK
):
640 new_url
= new_url
.replace(__UNSAFE_LINK
, "")
641 return self
.url_result(new_url
)