]>
Commit | Line | Data |
---|---|---|
48aae2d2 | 1 | # coding: utf-8 |
23e7cba8 S |
2 | from __future__ import unicode_literals |
3 | ||
4 | import re | |
5 | ||
6 | from .common import InfoExtractor | |
23e7cba8 S |
7 | from ..utils import ( |
8 | float_or_none, | |
01d22d47 | 9 | xpath_text, |
575036b4 | 10 | remove_end, |
cf5881fc YCH |
11 | int_or_none, |
12 | ExtractorError, | |
23e7cba8 S |
13 | ) |
14 | ||
15 | ||
445d72b8 YCH |
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() | |
20 | ||
21 | ||
22 | class TwitterCardIE(TwitterBaseIE): | |
014e8803 | 23 | IE_NAME = 'twitter:card' |
0ae937a7 | 24 | _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/(?:cards/tfw/v1|videos/tweet)/(?P<id>\d+)' |
c3dea3f8 | 25 | _TESTS = [ |
26 | { | |
27 | 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889', | |
acb6e97e | 28 | # MD5 checksums are different in different places |
c3dea3f8 | 29 | 'info_dict': { |
30 | 'id': '560070183650213889', | |
31 | 'ext': 'mp4', | |
0ae937a7 | 32 | 'title': 'Twitter Card', |
c3dea3f8 | 33 | 'thumbnail': 're:^https?://.*\.jpg$', |
34 | 'duration': 30.033, | |
35 | } | |
23e7cba8 | 36 | }, |
c3dea3f8 | 37 | { |
38 | 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768', | |
39 | 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8', | |
40 | 'info_dict': { | |
41 | 'id': '623160978427936768', | |
42 | 'ext': 'mp4', | |
0ae937a7 | 43 | 'title': 'Twitter Card', |
c3dea3f8 | 44 | 'thumbnail': 're:^https?://.*\.jpg', |
45 | 'duration': 80.155, | |
46 | }, | |
4a7b7903 YCH |
47 | }, |
48 | { | |
49 | 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977', | |
acb6e97e | 50 | 'md5': 'd4724ffe6d2437886d004fa5de1043b3', |
4a7b7903 YCH |
51 | 'info_dict': { |
52 | 'id': 'dq4Oj5quskI', | |
53 | 'ext': 'mp4', | |
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', | |
59 | }, | |
31752f76 | 60 | 'add_ie': ['Youtube'], |
5f1b2aea YCH |
61 | }, |
62 | { | |
63 | 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568', | |
64 | 'md5': 'ab2745d0b0ce53319a534fccaa986439', | |
65 | 'info_dict': { | |
66 | 'id': 'iBb2x00UVlv', | |
67 | 'ext': 'mp4', | |
68 | 'upload_date': '20151113', | |
69 | 'uploader_id': '1189339351084113920', | |
acb6e97e YCH |
70 | 'uploader': 'ArsenalTerje', |
71 | 'title': 'Vine by ArsenalTerje', | |
5f1b2aea YCH |
72 | }, |
73 | 'add_ie': ['Vine'], | |
0ae937a7 YCH |
74 | }, { |
75 | 'url': 'https://twitter.com/i/videos/tweet/705235433198714880', | |
76 | 'md5': '3846d0a07109b5ab622425449b59049d', | |
77 | 'info_dict': { | |
78 | 'id': '705235433198714880', | |
79 | 'ext': 'mp4', | |
80 | 'title': 'Twitter web player', | |
81 | 'thumbnail': 're:^https?://.*\.jpg', | |
82 | }, | |
83 | }, | |
c3dea3f8 | 84 | ] |
23e7cba8 S |
85 | |
86 | def _real_extract(self, url): | |
87 | video_id = self._match_id(url) | |
88 | ||
23e7cba8 S |
89 | config = None |
90 | formats = [] | |
b8972bd6 YCH |
91 | duration = None |
92 | ||
c8398a9b YCH |
93 | webpage = self._download_webpage(url, video_id) |
94 | ||
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) | |
98 | if iframe_url: | |
99 | return self.url_result(iframe_url) | |
100 | ||
101 | config = self._parse_json(self._html_search_regex( | |
102 | r'data-(?:player-)?config="([^"]+)"', webpage, 'data player config'), | |
103 | video_id) | |
104 | ||
395fd4b0 YCH |
105 | if config.get('source_type') == 'vine': |
106 | return self.url_result(config['player_url'], 'Vine') | |
107 | ||
845817aa YCH |
108 | def _search_dimensions_in_video_url(a_format, video_url): |
109 | m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url) | |
110 | if m: | |
111 | a_format.update({ | |
112 | 'width': int(m.group('width')), | |
113 | 'height': int(m.group('height')), | |
114 | }) | |
115 | ||
e68d3a01 | 116 | video_url = config.get('video_url') or config.get('playlist', [{}])[0].get('source') |
c8398a9b | 117 | |
e68d3a01 | 118 | if video_url: |
c8398a9b YCH |
119 | f = { |
120 | 'url': video_url, | |
121 | } | |
122 | ||
845817aa YCH |
123 | _search_dimensions_in_video_url(f, video_url) |
124 | ||
c8398a9b YCH |
125 | formats.append(f) |
126 | ||
127 | vmap_url = config.get('vmapUrl') or config.get('vmap_url') | |
128 | if vmap_url: | |
129 | formats.append({ | |
130 | 'url': self._get_vmap_video_url(vmap_url, video_id), | |
131 | }) | |
132 | ||
03879ff0 YCH |
133 | media_info = None |
134 | ||
135 | for entity in config.get('status', {}).get('entities', []): | |
136 | if 'mediaInfo' in entity: | |
137 | media_info = entity['mediaInfo'] | |
138 | ||
c8398a9b YCH |
139 | if media_info: |
140 | for media_variant in media_info['variants']: | |
141 | media_url = media_variant['url'] | |
142 | if media_url.endswith('.m3u8'): | |
143 | formats.extend(self._extract_m3u8_formats(media_url, video_id, ext='mp4', m3u8_id='hls')) | |
144 | elif media_url.endswith('.mpd'): | |
145 | formats.extend(self._extract_mpd_formats(media_url, video_id, mpd_id='dash')) | |
146 | else: | |
147 | vbr = int_or_none(media_variant.get('bitRate'), scale=1000) | |
148 | a_format = { | |
149 | 'url': media_url, | |
150 | 'format_id': 'http-%d' % vbr if vbr else 'http', | |
151 | 'vbr': vbr, | |
152 | } | |
153 | # Reported bitRate may be zero | |
154 | if not a_format['vbr']: | |
155 | del a_format['vbr'] | |
156 | ||
845817aa YCH |
157 | _search_dimensions_in_video_url(a_format, media_url) |
158 | ||
c8398a9b YCH |
159 | formats.append(a_format) |
160 | ||
161 | duration = float_or_none(media_info.get('duration', {}).get('nanos'), scale=1e9) | |
b8972bd6 | 162 | |
23e7cba8 S |
163 | self._sort_formats(formats) |
164 | ||
0ae937a7 YCH |
165 | title = self._search_regex(r'<title>([^<]+)</title>', webpage, 'title') |
166 | thumbnail = config.get('posterImageUrl') or config.get('image_src') | |
b8972bd6 | 167 | duration = float_or_none(config.get('duration')) or duration |
23e7cba8 S |
168 | |
169 | return { | |
170 | 'id': video_id, | |
0ae937a7 | 171 | 'title': title, |
23e7cba8 S |
172 | 'thumbnail': thumbnail, |
173 | 'duration': duration, | |
174 | 'formats': formats, | |
175 | } | |
f57f84f6 | 176 | |
177 | ||
e04edad6 | 178 | class TwitterIE(InfoExtractor): |
014e8803 | 179 | IE_NAME = 'twitter' |
575036b4 YCH |
180 | _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)' |
181 | _TEMPLATE_URL = 'https://twitter.com/%s/status/%s' | |
f57f84f6 | 182 | |
cf5881fc | 183 | _TESTS = [{ |
48aae2d2 | 184 | 'url': 'https://twitter.com/freethenipple/status/643211948184596480', |
f57f84f6 | 185 | 'info_dict': { |
48aae2d2 | 186 | 'id': '643211948184596480', |
f57f84f6 | 187 | 'ext': 'mp4', |
575036b4 | 188 | 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!', |
f57f84f6 | 189 | 'thumbnail': 're:^https?://.*\.jpg', |
48aae2d2 YCH |
190 | 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"', |
191 | 'uploader': 'FREE THE NIPPLE', | |
192 | 'uploader_id': 'freethenipple', | |
f57f84f6 | 193 | }, |
b8972bd6 YCH |
194 | 'params': { |
195 | 'skip_download': True, # requires ffmpeg | |
196 | }, | |
cf5881fc YCH |
197 | }, { |
198 | 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1', | |
199 | 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42', | |
200 | 'info_dict': { | |
201 | 'id': '657991469417025536', | |
202 | 'ext': 'mp4', | |
203 | 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai', | |
204 | 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"', | |
205 | 'thumbnail': 're:^https?://.*\.png', | |
206 | 'uploader': 'Gifs', | |
207 | 'uploader_id': 'giphz', | |
208 | }, | |
7efc1c2b | 209 | 'expected_warnings': ['height', 'width'], |
b703ebee JMF |
210 | }, { |
211 | 'url': 'https://twitter.com/starwars/status/665052190608723968', | |
212 | 'md5': '39b7199856dee6cd4432e72c74bc69d4', | |
213 | 'info_dict': { | |
214 | 'id': '665052190608723968', | |
215 | 'ext': 'mp4', | |
216 | 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.', | |
217 | 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."', | |
218 | 'uploader_id': 'starwars', | |
219 | 'uploader': 'Star Wars', | |
220 | }, | |
0ae937a7 YCH |
221 | }, { |
222 | 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880', | |
223 | 'info_dict': { | |
224 | 'id': '705235433198714880', | |
225 | 'ext': 'mp4', | |
226 | 'title': 'Brent Yarina - Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight.', | |
227 | 'description': 'Brent Yarina on Twitter: "Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight."', | |
228 | 'uploader_id': 'BTNBrentYarina', | |
229 | 'uploader': 'Brent Yarina', | |
230 | }, | |
231 | 'params': { | |
232 | # The same video as https://twitter.com/i/videos/tweet/705235433198714880 | |
233 | # Test case of TwitterCardIE | |
234 | 'skip_download': True, | |
235 | }, | |
03879ff0 YCH |
236 | }, { |
237 | 'url': 'https://twitter.com/jaydingeer/status/700207533655363584', | |
238 | 'md5': '', | |
239 | 'info_dict': { | |
240 | 'id': '700207533655363584', | |
241 | 'ext': 'mp4', | |
242 | 'title': 'jay - BEAT PROD: @suhmeduh #Damndaniel', | |
243 | 'description': 'jay on Twitter: "BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ"', | |
244 | 'thumbnail': 're:^https?://.*\.jpg', | |
245 | 'uploader': 'jay', | |
246 | 'uploader_id': 'jaydingeer', | |
247 | }, | |
248 | 'params': { | |
249 | 'skip_download': True, # requires ffmpeg | |
250 | }, | |
395fd4b0 YCH |
251 | }, { |
252 | 'url': 'https://twitter.com/Filmdrunk/status/713801302971588609', | |
253 | 'md5': '89a15ed345d13b86e9a5a5e051fa308a', | |
254 | 'info_dict': { | |
255 | 'id': 'MIOxnrUteUd', | |
256 | 'ext': 'mp4', | |
257 | 'title': 'Dr.Pepperの飲み方 #japanese #バカ #ドクペ #電動ガン', | |
258 | 'uploader': 'TAKUMA', | |
259 | 'uploader_id': '1004126642786242560', | |
260 | 'upload_date': '20140615', | |
261 | }, | |
262 | 'add_ie': ['Vine'], | |
36b7d9db YCH |
263 | }, { |
264 | 'url': 'https://twitter.com/captainamerica/status/719944021058060289', | |
265 | # md5 constantly changes | |
266 | 'info_dict': { | |
267 | 'id': '719944021058060289', | |
268 | 'ext': 'mp4', | |
269 | 'title': 'Captain America - @King0fNerd Are you sure you made the right choice? Find out in theaters.', | |
270 | 'description': 'Captain America on Twitter: "@King0fNerd Are you sure you made the right choice? Find out in theaters. https://t.co/GpgYi9xMJI"', | |
271 | 'uploader_id': 'captainamerica', | |
272 | 'uploader': 'Captain America', | |
273 | }, | |
cf5881fc | 274 | }] |
f57f84f6 | 275 | |
276 | def _real_extract(self, url): | |
575036b4 YCH |
277 | mobj = re.match(self._VALID_URL, url) |
278 | user_id = mobj.group('user_id') | |
279 | twid = mobj.group('id') | |
280 | ||
281 | webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid) | |
282 | ||
283 | username = remove_end(self._og_search_title(webpage), ' on Twitter') | |
284 | ||
b703ebee | 285 | title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”') |
575036b4 YCH |
286 | |
287 | # strip 'https -_t.co_BJYgOjSeGA' junk from filenames | |
b703ebee | 288 | title = re.sub(r'\s+(https?://[^ ]+)', '', title) |
575036b4 | 289 | |
cf5881fc | 290 | info = { |
575036b4 YCH |
291 | 'uploader_id': user_id, |
292 | 'uploader': username, | |
f57f84f6 | 293 | 'webpage_url': url, |
b703ebee | 294 | 'description': '%s on Twitter: "%s"' % (username, description), |
f57f84f6 | 295 | 'title': username + ' - ' + title, |
296 | } | |
cf5881fc | 297 | |
cf5881fc | 298 | mobj = re.search(r'''(?x) |
7efc1c2b | 299 | <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s* |
cf5881fc YCH |
300 | <source[^>]+video-src="(?P<url>[^"]+)" |
301 | ''', webpage) | |
302 | ||
303 | if mobj: | |
7efc1c2b YCH |
304 | more_info = mobj.group('more_info') |
305 | height = int_or_none(self._search_regex( | |
306 | r'data-height="(\d+)"', more_info, 'height', fatal=False)) | |
307 | width = int_or_none(self._search_regex( | |
308 | r'data-width="(\d+)"', more_info, 'width', fatal=False)) | |
309 | thumbnail = self._search_regex( | |
310 | r'poster="([^"]+)"', more_info, 'poster', fatal=False) | |
cf5881fc YCH |
311 | info.update({ |
312 | 'id': twid, | |
313 | 'url': mobj.group('url'), | |
7efc1c2b YCH |
314 | 'height': height, |
315 | 'width': width, | |
316 | 'thumbnail': thumbnail, | |
cf5881fc YCH |
317 | }) |
318 | return info | |
319 | ||
0ae937a7 YCH |
320 | if 'class="PlayableMedia' in webpage: |
321 | info.update({ | |
322 | '_type': 'url_transparent', | |
323 | 'ie_key': 'TwitterCard', | |
324 | 'url': '%s//twitter.com/i/videos/tweet/%s' % (self.http_scheme(), twid), | |
325 | }) | |
326 | ||
327 | return info | |
328 | ||
132e3b74 | 329 | raise ExtractorError('There\'s no video in this tweet.') |
445d72b8 YCH |
330 | |
331 | ||
332 | class TwitterAmplifyIE(TwitterBaseIE): | |
333 | IE_NAME = 'twitter:amplify' | |
334 | _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})' | |
335 | ||
336 | _TEST = { | |
337 | 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951', | |
338 | 'md5': '7df102d0b9fd7066b86f3159f8e81bf6', | |
339 | 'info_dict': { | |
340 | 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951', | |
341 | 'ext': 'mp4', | |
342 | 'title': 'Twitter Video', | |
bdbf4ba4 | 343 | 'thumbnail': 're:^https?://.*', |
445d72b8 YCH |
344 | }, |
345 | } | |
346 | ||
347 | def _real_extract(self, url): | |
348 | video_id = self._match_id(url) | |
349 | webpage = self._download_webpage(url, video_id) | |
350 | ||
351 | vmap_url = self._html_search_meta( | |
352 | 'twitter:amplify:vmap', webpage, 'vmap url') | |
353 | video_url = self._get_vmap_video_url(vmap_url, video_id) | |
354 | ||
bdbf4ba4 YCH |
355 | thumbnails = [] |
356 | thumbnail = self._html_search_meta( | |
357 | 'twitter:image:src', webpage, 'thumbnail', fatal=False) | |
358 | ||
359 | def _find_dimension(target): | |
360 | w = int_or_none(self._html_search_meta( | |
361 | 'twitter:%s:width' % target, webpage, fatal=False)) | |
362 | h = int_or_none(self._html_search_meta( | |
363 | 'twitter:%s:height' % target, webpage, fatal=False)) | |
364 | return w, h | |
365 | ||
366 | if thumbnail: | |
367 | thumbnail_w, thumbnail_h = _find_dimension('image') | |
368 | thumbnails.append({ | |
369 | 'url': thumbnail, | |
370 | 'width': thumbnail_w, | |
371 | 'height': thumbnail_h, | |
372 | }) | |
373 | ||
374 | video_w, video_h = _find_dimension('player') | |
375 | formats = [{ | |
376 | 'url': video_url, | |
377 | 'width': video_w, | |
378 | 'height': video_h, | |
379 | }] | |
380 | ||
445d72b8 YCH |
381 | return { |
382 | 'id': video_id, | |
383 | 'title': 'Twitter Video', | |
bdbf4ba4 YCH |
384 | 'formats': formats, |
385 | 'thumbnails': thumbnails, | |
445d72b8 | 386 | } |