2 from __future__ import unicode_literals
6 from .common import InfoExtractor
16 class TwitterBaseIE(InfoExtractor):
17 def _get_vmap_video_url(self, vmap_url, video_id):
18 vmap_data = self._download_xml(vmap_url, video_id)
19 return xpath_text(vmap_data, './/MediaFile').strip()
22 class TwitterCardIE(TwitterBaseIE):
23 IE_NAME = 'twitter:card'
24 _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/(?:cards/tfw/v1|videos/tweet)/(?P<id>\d+)'
27 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
28 # MD5 checksums are different in different places
30 'id': '560070183650213889',
32 'title': 'Twitter Card',
33 'thumbnail': 're:^https?://.*\.jpg$',
38 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
39 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
41 'id': '623160978427936768',
43 'title': 'Twitter Card',
44 'thumbnail': 're:^https?://.*\.jpg',
49 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
50 'md5': 'd4724ffe6d2437886d004fa5de1043b3',
54 'title': 'Ubuntu 11.10 Overview',
55 'description': 'Take a quick peek at what\'s new and improved in Ubuntu 11.10.\n\nOnce installed take a look at 10 Things to Do After Installing: http://www.omgubuntu.co.uk/2011/10/10-things-to-do-after-installing-ubuntu-11-10/',
56 'upload_date': '20111013',
57 'uploader': 'OMG! Ubuntu!',
58 'uploader_id': 'omgubuntu',
60 'add_ie': ['Youtube'],
63 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
64 'md5': 'ab2745d0b0ce53319a534fccaa986439',
68 'upload_date': '20151113',
69 'uploader_id': '1189339351084113920',
70 'uploader': 'ArsenalTerje',
71 'title': 'Vine by ArsenalTerje',
75 'url': 'https://twitter.com/i/videos/tweet/705235433198714880',
76 'md5': '3846d0a07109b5ab622425449b59049d',
78 'id': '705235433198714880',
80 'title': 'Twitter web player',
81 'thumbnail': 're:^https?://.*\.jpg',
86 def _real_extract(self, url):
87 video_id = self._match_id(url)
93 webpage = self._download_webpage(url, video_id)
95 iframe_url = self._html_search_regex(
96 r'<iframe[^>]+src="((?:https?:)?//(?:www.youtube.com/embed/[^"]+|(?:www\.)?vine\.co/v/\w+/card))"',
97 webpage, 'video iframe', default=None)
99 return self.url_result(iframe_url)
101 config = self._parse_json(self._html_search_regex(
102 r'data-(?:player-)?config="([^"]+)"', webpage, 'data player config'),
105 def _search_dimensions_in_video_url(a_format, video_url):
106 m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
109 'width': int(m.group('width')),
110 'height': int(m.group('height')),
113 video_url = config.get('video_url') or config.get('playlist', [{}])[0].get('source')
120 _search_dimensions_in_video_url(f, video_url)
124 vmap_url = config.get('vmapUrl') or config.get('vmap_url')
127 'url': self._get_vmap_video_url(vmap_url, video_id),
132 for entity in config.get('status', {}).get('entities', []):
133 if 'mediaInfo' in entity:
134 media_info = entity['mediaInfo']
137 for media_variant in media_info['variants']:
138 media_url = media_variant['url']
139 if media_url.endswith('.m3u8'):
140 formats.extend(self._extract_m3u8_formats(media_url, video_id, ext='mp4', m3u8_id='hls'))
141 elif media_url.endswith('.mpd'):
142 formats.extend(self._extract_mpd_formats(media_url, video_id, mpd_id='dash'))
144 vbr = int_or_none(media_variant.get('bitRate'), scale=1000)
147 'format_id': 'http-%d' % vbr if vbr else 'http',
150 # Reported bitRate may be zero
151 if not a_format['vbr']:
154 _search_dimensions_in_video_url(a_format, media_url)
156 formats.append(a_format)
158 duration = float_or_none(media_info.get('duration', {}).get('nanos'), scale=1e9)
160 self._sort_formats(formats)
162 title = self._search_regex(r'<title>([^<]+)</title>', webpage, 'title')
163 thumbnail = config.get('posterImageUrl') or config.get('image_src')
164 duration = float_or_none(config.get('duration')) or duration
169 'thumbnail': thumbnail,
170 'duration': duration,
175 class TwitterIE(InfoExtractor):
177 _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
178 _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
181 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
183 'id': '643211948184596480',
185 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
186 'thumbnail': 're:^https?://.*\.jpg',
187 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
188 'uploader': 'FREE THE NIPPLE',
189 'uploader_id': 'freethenipple',
192 'skip_download': True, # requires ffmpeg
195 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
196 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
198 'id': '657991469417025536',
200 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
201 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
202 'thumbnail': 're:^https?://.*\.png',
204 'uploader_id': 'giphz',
206 'expected_warnings': ['height', 'width'],
208 'url': 'https://twitter.com/starwars/status/665052190608723968',
209 'md5': '39b7199856dee6cd4432e72c74bc69d4',
211 'id': '665052190608723968',
213 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
214 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
215 'uploader_id': 'starwars',
216 'uploader': 'Star Wars',
219 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880',
221 'id': '705235433198714880',
223 'title': 'Brent Yarina - Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight.',
224 'description': 'Brent Yarina on Twitter: "Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight."',
225 'uploader_id': 'BTNBrentYarina',
226 'uploader': 'Brent Yarina',
229 # The same video as https://twitter.com/i/videos/tweet/705235433198714880
230 # Test case of TwitterCardIE
231 'skip_download': True,
234 'url': 'https://twitter.com/jaydingeer/status/700207533655363584',
237 'id': '700207533655363584',
239 'title': 'jay - BEAT PROD: @suhmeduh #Damndaniel',
240 'description': 'jay on Twitter: "BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ"',
241 'thumbnail': 're:^https?://.*\.jpg',
243 'uploader_id': 'jaydingeer',
246 'skip_download': True, # requires ffmpeg
250 def _real_extract(self, url):
251 mobj = re.match(self._VALID_URL, url)
252 user_id = mobj.group('user_id')
253 twid = mobj.group('id')
255 webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
257 username = remove_end(self._og_search_title(webpage), ' on Twitter')
259 title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
261 # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
262 title = re.sub(r'\s+(https?://[^ ]+)', '', title)
265 'uploader_id': user_id,
266 'uploader': username,
268 'description': '%s on Twitter: "%s"' % (username, description),
269 'title': username + ' - ' + title,
272 card_id = self._search_regex(
273 r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
275 card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
277 '_type': 'url_transparent',
278 'ie_key': 'TwitterCard',
283 mobj = re.search(r'''(?x)
284 <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s*
285 <source[^>]+video-src="(?P<url>[^"]+)"
289 more_info = mobj.group('more_info')
290 height = int_or_none(self._search_regex(
291 r'data-height="(\d+)"', more_info, 'height', fatal=False))
292 width = int_or_none(self._search_regex(
293 r'data-width="(\d+)"', more_info, 'width', fatal=False))
294 thumbnail = self._search_regex(
295 r'poster="([^"]+)"', more_info, 'poster', fatal=False)
298 'url': mobj.group('url'),
301 'thumbnail': thumbnail,
305 if 'class="PlayableMedia' in webpage:
307 '_type': 'url_transparent',
308 'ie_key': 'TwitterCard',
309 'url': '%s//twitter.com/i/videos/tweet/%s' % (self.http_scheme(), twid),
314 raise ExtractorError('There\'s no video in this tweet.')
317 class TwitterAmplifyIE(TwitterBaseIE):
318 IE_NAME = 'twitter:amplify'
319 _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
322 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
323 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
325 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
327 'title': 'Twitter Video',
328 'thumbnail': 're:^https?://.*',
332 def _real_extract(self, url):
333 video_id = self._match_id(url)
334 webpage = self._download_webpage(url, video_id)
336 vmap_url = self._html_search_meta(
337 'twitter:amplify:vmap', webpage, 'vmap url')
338 video_url = self._get_vmap_video_url(vmap_url, video_id)
341 thumbnail = self._html_search_meta(
342 'twitter:image:src', webpage, 'thumbnail', fatal=False)
344 def _find_dimension(target):
345 w = int_or_none(self._html_search_meta(
346 'twitter:%s:width' % target, webpage, fatal=False))
347 h = int_or_none(self._html_search_meta(
348 'twitter:%s:height' % target, webpage, fatal=False))
352 thumbnail_w, thumbnail_h = _find_dimension('image')
355 'width': thumbnail_w,
356 'height': thumbnail_h,
359 video_w, video_h = _find_dimension('player')
368 'title': 'Twitter Video',
370 'thumbnails': thumbnails,