]>
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 | |
18ca61c5 RA |
7 | from ..compat import ( |
8 | compat_HTTPError, | |
9 | compat_parse_qs, | |
10 | compat_urllib_parse_unquote, | |
11 | compat_urllib_parse_urlparse, | |
12 | ) | |
23e7cba8 | 13 | from ..utils import ( |
2edfd745 YCH |
14 | dict_get, |
15 | ExtractorError, | |
23e7cba8 | 16 | float_or_none, |
cf5881fc | 17 | int_or_none, |
2edfd745 | 18 | try_get, |
18ca61c5 RA |
19 | strip_or_none, |
20 | unified_timestamp, | |
21 | update_url_query, | |
2edfd745 | 22 | xpath_text, |
23e7cba8 S |
23 | ) |
24 | ||
18ca61c5 RA |
25 | from .periscope import ( |
26 | PeriscopeBaseIE, | |
27 | PeriscopeIE, | |
28 | ) | |
f0bc5a86 | 29 | |
23e7cba8 | 30 | |
445d72b8 | 31 | class TwitterBaseIE(InfoExtractor): |
18ca61c5 RA |
32 | _API_BASE = 'https://api.twitter.com/1.1/' |
33 | _BASE_REGEX = r'https?://(?:(?:www|m(?:obile)?)\.)?twitter\.com/' | |
34 | _GUEST_TOKEN = None | |
35 | ||
36 | def _extract_variant_formats(self, variant, video_id): | |
37 | variant_url = variant.get('url') | |
38 | if not variant_url: | |
39 | return [] | |
40 | elif '.m3u8' in variant_url: | |
41 | return self._extract_m3u8_formats( | |
42 | variant_url, video_id, 'mp4', 'm3u8_native', | |
43 | m3u8_id='hls', fatal=False) | |
44 | else: | |
45 | tbr = int_or_none(dict_get(variant, ('bitrate', 'bit_rate')), 1000) or None | |
46 | f = { | |
47 | 'url': variant_url, | |
48 | 'format_id': 'http' + ('-%d' % tbr if tbr else ''), | |
49 | 'tbr': tbr, | |
50 | } | |
51 | self._search_dimensions_in_video_url(f, variant_url) | |
52 | return [f] | |
53 | ||
9be31e77 | 54 | def _extract_formats_from_vmap_url(self, vmap_url, video_id): |
445d72b8 | 55 | vmap_data = self._download_xml(vmap_url, video_id) |
18ca61c5 RA |
56 | formats = [] |
57 | urls = [] | |
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)) | |
67 | return formats | |
445d72b8 | 68 | |
2edfd745 YCH |
69 | @staticmethod |
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) | |
72 | if m: | |
73 | a_format.update({ | |
74 | 'width': int(m.group('width')), | |
75 | 'height': int(m.group('height')), | |
76 | }) | |
77 | ||
18ca61c5 RA |
78 | def _call_api(self, path, video_id, query={}): |
79 | headers = { | |
80 | 'Authorization': 'Bearer AAAAAAAAAAAAAAAAAAAAAPYXBAAAAAAACLXUNDekMxqa8h%2F40K4moUkGsoc%3DTYfbDKbT3jJPCEVnMYqilB28NHfOPqkca3qaAxGfsyKCs0wRbw', | |
81 | } | |
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 | |
88 | try: | |
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) | |
96 | raise | |
97 | ||
98 | ||
99 | class TwitterCardIE(InfoExtractor): | |
014e8803 | 100 | IE_NAME = 'twitter:card' |
18ca61c5 | 101 | _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/(?:cards/tfw/v1|videos(?:/tweet)?)/(?P<id>\d+)' |
c3dea3f8 | 102 | _TESTS = [ |
103 | { | |
104 | 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889', | |
acb6e97e | 105 | # MD5 checksums are different in different places |
c3dea3f8 | 106 | 'info_dict': { |
107 | 'id': '560070183650213889', | |
108 | 'ext': 'mp4', | |
18ca61c5 RA |
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', | |
c3dea3f8 | 114 | 'duration': 30.033, |
18ca61c5 RA |
115 | 'timestamp': 1422366112, |
116 | 'upload_date': '20150127', | |
3615bfe1 | 117 | }, |
23e7cba8 | 118 | }, |
c3dea3f8 | 119 | { |
120 | 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768', | |
18ca61c5 | 121 | 'md5': '7137eca597f72b9abbe61e5ae0161399', |
c3dea3f8 | 122 | 'info_dict': { |
123 | 'id': '623160978427936768', | |
124 | 'ext': 'mp4', | |
18ca61c5 RA |
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", | |
127 | 'uploader': 'NASA', | |
128 | 'uploader_id': 'NASA', | |
129 | 'timestamp': 1437408129, | |
130 | 'upload_date': '20150720', | |
c3dea3f8 | 131 | }, |
4a7b7903 YCH |
132 | }, |
133 | { | |
134 | 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977', | |
f0bc5a86 | 135 | 'md5': 'b6d9683dd3f48e340ded81c0e917ad46', |
4a7b7903 YCH |
136 | 'info_dict': { |
137 | 'id': 'dq4Oj5quskI', | |
138 | 'ext': 'mp4', | |
139 | 'title': 'Ubuntu 11.10 Overview', | |
f0bc5a86 | 140 | 'description': 'md5:a831e97fa384863d6e26ce48d1c43376', |
4a7b7903 | 141 | 'upload_date': '20111013', |
18ca61c5 | 142 | 'uploader': 'OMG! UBUNTU!', |
4a7b7903 YCH |
143 | 'uploader_id': 'omgubuntu', |
144 | }, | |
31752f76 | 145 | 'add_ie': ['Youtube'], |
5f1b2aea YCH |
146 | }, |
147 | { | |
148 | 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568', | |
3615bfe1 | 149 | 'md5': '6dabeaca9e68cbb71c99c322a4b42a11', |
5f1b2aea YCH |
150 | 'info_dict': { |
151 | 'id': 'iBb2x00UVlv', | |
152 | 'ext': 'mp4', | |
153 | 'upload_date': '20151113', | |
154 | 'uploader_id': '1189339351084113920', | |
acb6e97e YCH |
155 | 'uploader': 'ArsenalTerje', |
156 | 'title': 'Vine by ArsenalTerje', | |
e8f20ffa | 157 | 'timestamp': 1447451307, |
5f1b2aea YCH |
158 | }, |
159 | 'add_ie': ['Vine'], | |
0ae937a7 YCH |
160 | }, { |
161 | 'url': 'https://twitter.com/i/videos/tweet/705235433198714880', | |
3615bfe1 | 162 | 'md5': '884812a2adc8aaf6fe52b15ccbfa3b88', |
0ae937a7 YCH |
163 | 'info_dict': { |
164 | 'id': '705235433198714880', | |
165 | 'ext': 'mp4', | |
18ca61c5 RA |
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', | |
0ae937a7 | 172 | }, |
18ca61c5 | 173 | 'skip': 'This content is no longer available.', |
748a462f S |
174 | }, { |
175 | 'url': 'https://twitter.com/i/videos/752274308186120192', | |
176 | 'only_matching': True, | |
0ae937a7 | 177 | }, |
c3dea3f8 | 178 | ] |
23e7cba8 S |
179 | |
180 | def _real_extract(self, url): | |
18ca61c5 RA |
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) | |
c8398a9b | 185 | |
03879ff0 | 186 | |
18ca61c5 | 187 | class TwitterIE(TwitterBaseIE): |
014e8803 | 188 | IE_NAME = 'twitter' |
18ca61c5 | 189 | _VALID_URL = TwitterBaseIE._BASE_REGEX + r'(?:(?:i/web|[^/]+)/status|statuses)/(?P<id>\d+)' |
f57f84f6 | 190 | |
cf5881fc | 191 | _TESTS = [{ |
48aae2d2 | 192 | 'url': 'https://twitter.com/freethenipple/status/643211948184596480', |
f57f84f6 | 193 | 'info_dict': { |
48aae2d2 | 194 | 'id': '643211948184596480', |
f57f84f6 | 195 | 'ext': 'mp4', |
575036b4 | 196 | 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!', |
ec85ded8 | 197 | 'thumbnail': r're:^https?://.*\.jpg', |
18ca61c5 | 198 | 'description': 'FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ', |
48aae2d2 YCH |
199 | 'uploader': 'FREE THE NIPPLE', |
200 | 'uploader_id': 'freethenipple', | |
3b65a6fb | 201 | 'duration': 12.922, |
18ca61c5 RA |
202 | 'timestamp': 1442188653, |
203 | 'upload_date': '20150913', | |
204 | 'age_limit': 18, | |
f57f84f6 | 205 | }, |
cf5881fc YCH |
206 | }, { |
207 | 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1', | |
208 | 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42', | |
209 | 'info_dict': { | |
210 | 'id': '657991469417025536', | |
211 | 'ext': 'mp4', | |
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"', | |
ec85ded8 | 214 | 'thumbnail': r're:^https?://.*\.png', |
cf5881fc YCH |
215 | 'uploader': 'Gifs', |
216 | 'uploader_id': 'giphz', | |
217 | }, | |
7efc1c2b | 218 | 'expected_warnings': ['height', 'width'], |
fc0a45fa | 219 | 'skip': 'Account suspended', |
b703ebee JMF |
220 | }, { |
221 | 'url': 'https://twitter.com/starwars/status/665052190608723968', | |
b703ebee JMF |
222 | 'info_dict': { |
223 | 'id': '665052190608723968', | |
224 | 'ext': 'mp4', | |
225 | 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.', | |
18ca61c5 | 226 | 'description': 'A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens. https://t.co/OkSqT2fjWJ', |
b703ebee JMF |
227 | 'uploader_id': 'starwars', |
228 | 'uploader': 'Star Wars', | |
18ca61c5 RA |
229 | 'timestamp': 1447395772, |
230 | 'upload_date': '20151113', | |
b703ebee | 231 | }, |
0ae937a7 YCH |
232 | }, { |
233 | 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880', | |
234 | 'info_dict': { | |
235 | 'id': '705235433198714880', | |
236 | 'ext': 'mp4', | |
18ca61c5 RA |
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", | |
0ae937a7 YCH |
239 | 'uploader_id': 'BTNBrentYarina', |
240 | 'uploader': 'Brent Yarina', | |
18ca61c5 RA |
241 | 'timestamp': 1456976204, |
242 | 'upload_date': '20160303', | |
0ae937a7 YCH |
243 | }, |
244 | 'params': { | |
245 | # The same video as https://twitter.com/i/videos/tweet/705235433198714880 | |
246 | # Test case of TwitterCardIE | |
247 | 'skip_download': True, | |
248 | }, | |
03879ff0 YCH |
249 | }, { |
250 | 'url': 'https://twitter.com/jaydingeer/status/700207533655363584', | |
03879ff0 YCH |
251 | 'info_dict': { |
252 | 'id': '700207533655363584', | |
253 | 'ext': 'mp4', | |
10a5091e | 254 | 'title': 'simon vetugo - BEAT PROD: @suhmeduh #Damndaniel', |
18ca61c5 | 255 | 'description': 'BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ', |
ec85ded8 | 256 | 'thumbnail': r're:^https?://.*\.jpg', |
10a5091e | 257 | 'uploader': 'simon vetugo', |
18ca61c5 | 258 | 'uploader_id': 'simonvertugo', |
3b65a6fb | 259 | 'duration': 30.0, |
18ca61c5 RA |
260 | 'timestamp': 1455777459, |
261 | 'upload_date': '20160218', | |
03879ff0 | 262 | }, |
395fd4b0 YCH |
263 | }, { |
264 | 'url': 'https://twitter.com/Filmdrunk/status/713801302971588609', | |
265 | 'md5': '89a15ed345d13b86e9a5a5e051fa308a', | |
266 | 'info_dict': { | |
267 | 'id': 'MIOxnrUteUd', | |
268 | 'ext': 'mp4', | |
18ca61c5 RA |
269 | 'title': 'Dr.Pepperの飲み方 #japanese #バカ #ドクペ #電動ガン', |
270 | 'uploader': 'TAKUMA', | |
271 | 'uploader_id': '1004126642786242560', | |
3615bfe1 | 272 | 'timestamp': 1402826626, |
395fd4b0 YCH |
273 | 'upload_date': '20140615', |
274 | }, | |
275 | 'add_ie': ['Vine'], | |
36b7d9db YCH |
276 | }, { |
277 | 'url': 'https://twitter.com/captainamerica/status/719944021058060289', | |
36b7d9db YCH |
278 | 'info_dict': { |
279 | 'id': '719944021058060289', | |
280 | 'ext': 'mp4', | |
281 | 'title': 'Captain America - @King0fNerd Are you sure you made the right choice? Find out in theaters.', | |
18ca61c5 RA |
282 | 'description': '@King0fNerd Are you sure you made the right choice? Find out in theaters. https://t.co/GpgYi9xMJI', |
283 | 'uploader_id': 'CaptainAmerica', | |
36b7d9db | 284 | 'uploader': 'Captain America', |
3b65a6fb | 285 | 'duration': 3.17, |
18ca61c5 RA |
286 | 'timestamp': 1460483005, |
287 | 'upload_date': '20160412', | |
36b7d9db | 288 | }, |
f0bc5a86 YCH |
289 | }, { |
290 | 'url': 'https://twitter.com/OPP_HSD/status/779210622571536384', | |
291 | 'info_dict': { | |
292 | 'id': '1zqKVVlkqLaKB', | |
293 | 'ext': 'mp4', | |
18ca61c5 | 294 | 'title': 'Sgt Kerry Schmidt - Ontario Provincial Police - Road rage, mischief, assault, rollover and fire in one occurrence', |
f0bc5a86 | 295 | 'upload_date': '20160923', |
18ca61c5 RA |
296 | 'uploader_id': '1PmKqpJdOJQoY', |
297 | 'uploader': 'Sgt Kerry Schmidt - Ontario Provincial Police', | |
f0bc5a86 YCH |
298 | 'timestamp': 1474613214, |
299 | }, | |
300 | 'add_ie': ['Periscope'], | |
2edfd745 YCH |
301 | }, { |
302 | # has mp4 formats via mobile API | |
303 | 'url': 'https://twitter.com/news_al3alm/status/852138619213144067', | |
304 | 'info_dict': { | |
305 | 'id': '852138619213144067', | |
306 | 'ext': 'mp4', | |
307 | 'title': 'عالم الأخبار - كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة', | |
18ca61c5 | 308 | 'description': 'كلمة تاريخية بجلسة الجناسي التاريخية.. النائب خالد مؤنس العتيبي للمعارضين : اتقوا الله .. الظلم ظلمات يوم القيامة https://t.co/xg6OhpyKfN', |
2edfd745 YCH |
309 | 'uploader': 'عالم الأخبار', |
310 | 'uploader_id': 'news_al3alm', | |
3b65a6fb | 311 | 'duration': 277.4, |
18ca61c5 RA |
312 | 'timestamp': 1492000653, |
313 | 'upload_date': '20170412', | |
2edfd745 | 314 | }, |
5c1452e8 GF |
315 | }, { |
316 | 'url': 'https://twitter.com/i/web/status/910031516746514432', | |
317 | 'info_dict': { | |
318 | 'id': '910031516746514432', | |
319 | 'ext': 'mp4', | |
320 | '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.', | |
321 | 'thumbnail': r're:^https?://.*\.jpg', | |
18ca61c5 | 322 | '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', |
5c1452e8 GF |
323 | 'uploader': 'Préfet de Guadeloupe', |
324 | 'uploader_id': 'Prefet971', | |
325 | 'duration': 47.48, | |
18ca61c5 RA |
326 | 'timestamp': 1505803395, |
327 | 'upload_date': '20170919', | |
5c1452e8 GF |
328 | }, |
329 | 'params': { | |
330 | 'skip_download': True, # requires ffmpeg | |
331 | }, | |
2593725a S |
332 | }, { |
333 | # card via api.twitter.com/1.1/videos/tweet/config | |
334 | 'url': 'https://twitter.com/LisPower1/status/1001551623938805763', | |
335 | 'info_dict': { | |
336 | 'id': '1001551623938805763', | |
337 | 'ext': 'mp4', | |
338 | 'title': 're:.*?Shep is on a roll today.*?', | |
339 | 'thumbnail': r're:^https?://.*\.jpg', | |
18ca61c5 | 340 | 'description': 'md5:37b9f2ff31720cef23b2bd42ee8a0f09', |
2593725a S |
341 | 'uploader': 'Lis Power', |
342 | 'uploader_id': 'LisPower1', | |
343 | 'duration': 111.278, | |
18ca61c5 RA |
344 | 'timestamp': 1527623489, |
345 | 'upload_date': '20180529', | |
2593725a S |
346 | }, |
347 | 'params': { | |
348 | 'skip_download': True, # requires ffmpeg | |
349 | }, | |
b7ef93f0 S |
350 | }, { |
351 | 'url': 'https://twitter.com/foobar/status/1087791357756956680', | |
352 | 'info_dict': { | |
353 | 'id': '1087791357756956680', | |
354 | 'ext': 'mp4', | |
355 | '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!', | |
356 | 'thumbnail': r're:^https?://.*\.jpg', | |
18ca61c5 | 357 | 'description': 'md5:6dfd341a3310fb97d80d2bf7145df976', |
b7ef93f0 S |
358 | 'uploader': 'Twitter', |
359 | 'uploader_id': 'Twitter', | |
360 | 'duration': 61.567, | |
18ca61c5 RA |
361 | 'timestamp': 1548184644, |
362 | 'upload_date': '20190122', | |
363 | }, | |
364 | }, { | |
365 | # not available in Periscope | |
366 | 'url': 'https://twitter.com/ViviEducation/status/1136534865145286656', | |
367 | 'info_dict': { | |
368 | 'id': '1vOGwqejwoWxB', | |
369 | 'ext': 'mp4', | |
370 | 'title': 'Vivi - Vivi founder @lior_rauchy announcing our new student feedback tool live at @EduTECH_AU #EduTECH2019', | |
371 | 'uploader': 'Vivi', | |
372 | 'uploader_id': '1eVjYOLGkGrQL', | |
b7ef93f0 | 373 | }, |
18ca61c5 RA |
374 | 'add_ie': ['TwitterBroadcast'], |
375 | }, { | |
376 | # Twitch Clip Embed | |
377 | 'url': 'https://twitter.com/GunB1g/status/1163218564784017422', | |
378 | 'only_matching': True, | |
10a5091e RA |
379 | }, { |
380 | # promo_video_website card | |
381 | 'url': 'https://twitter.com/GunB1g/status/1163218564784017422', | |
382 | 'only_matching': True, | |
cf5881fc | 383 | }] |
f57f84f6 | 384 | |
385 | def _real_extract(self, url): | |
18ca61c5 RA |
386 | twid = self._match_id(url) |
387 | status = self._call_api( | |
388 | 'statuses/show/%s.json' % twid, twid, { | |
389 | 'cards_platform': 'Web-12', | |
390 | 'include_cards': 1, | |
391 | 'include_reply_count': 1, | |
392 | 'include_user_entities': 0, | |
393 | 'tweet_mode': 'extended', | |
394 | }) | |
575036b4 | 395 | |
18ca61c5 | 396 | title = description = status['full_text'].replace('\n', ' ') |
575036b4 | 397 | # strip 'https -_t.co_BJYgOjSeGA' junk from filenames |
b703ebee | 398 | title = re.sub(r'\s+(https?://[^ ]+)', '', title) |
18ca61c5 RA |
399 | user = status.get('user') or {} |
400 | uploader = user.get('name') | |
401 | if uploader: | |
402 | title = '%s - %s' % (uploader, title) | |
403 | uploader_id = user.get('screen_name') | |
404 | ||
405 | tags = [] | |
406 | for hashtag in (try_get(status, lambda x: x['entities']['hashtags'], list) or []): | |
407 | hashtag_text = hashtag.get('text') | |
408 | if not hashtag_text: | |
409 | continue | |
410 | tags.append(hashtag_text) | |
575036b4 | 411 | |
cf5881fc | 412 | info = { |
18ca61c5 RA |
413 | 'id': twid, |
414 | 'title': title, | |
415 | 'description': description, | |
416 | 'uploader': uploader, | |
417 | 'timestamp': unified_timestamp(status.get('created_at')), | |
418 | 'uploader_id': uploader_id, | |
419 | 'uploader_url': 'https://twitter.com/' + uploader_id if uploader_id else None, | |
420 | 'like_count': int_or_none(status.get('favorite_count')), | |
421 | 'repost_count': int_or_none(status.get('retweet_count')), | |
422 | 'comment_count': int_or_none(status.get('reply_count')), | |
423 | 'age_limit': 18 if status.get('possibly_sensitive') else 0, | |
424 | 'tags': tags, | |
f57f84f6 | 425 | } |
cf5881fc | 426 | |
18ca61c5 RA |
427 | media = try_get(status, lambda x: x['extended_entities']['media'][0]) |
428 | if media and media.get('type') != 'photo': | |
429 | video_info = media.get('video_info') or {} | |
430 | ||
431 | formats = [] | |
432 | for variant in video_info.get('variants', []): | |
433 | formats.extend(self._extract_variant_formats(variant, twid)) | |
434 | self._sort_formats(formats) | |
435 | ||
436 | thumbnails = [] | |
437 | media_url = media.get('media_url_https') or media.get('media_url') | |
438 | if media_url: | |
439 | def add_thumbnail(name, size): | |
440 | thumbnails.append({ | |
441 | 'id': name, | |
442 | 'url': update_url_query(media_url, {'name': name}), | |
443 | 'width': int_or_none(size.get('w') or size.get('width')), | |
444 | 'height': int_or_none(size.get('h') or size.get('height')), | |
445 | }) | |
446 | for name, size in media.get('sizes', {}).items(): | |
447 | add_thumbnail(name, size) | |
448 | add_thumbnail('orig', media.get('original_info') or {}) | |
cf5881fc | 449 | |
0ae937a7 | 450 | info.update({ |
18ca61c5 RA |
451 | 'formats': formats, |
452 | 'thumbnails': thumbnails, | |
453 | 'duration': float_or_none(video_info.get('duration_millis'), 1000), | |
0ae937a7 | 454 | }) |
18ca61c5 RA |
455 | else: |
456 | card = status.get('card') | |
457 | if card: | |
458 | binding_values = card['binding_values'] | |
459 | ||
460 | def get_binding_value(k): | |
461 | o = binding_values.get(k) or {} | |
462 | return try_get(o, lambda x: x[x['type'].lower() + '_value']) | |
463 | ||
464 | card_name = card['name'].split(':')[-1] | |
10a5091e RA |
465 | if card_name in ('amplify', 'promo_video_website'): |
466 | is_amplify = card_name == 'amplify' | |
467 | vmap_url = get_binding_value('amplify_url_vmap') if is_amplify else get_binding_value('player_stream_url') | |
468 | content_id = get_binding_value('%s_content_id' % (card_name if is_amplify else 'player')) | |
469 | formats = self._extract_formats_from_vmap_url(vmap_url, content_id or twid) | |
18ca61c5 RA |
470 | self._sort_formats(formats) |
471 | ||
472 | thumbnails = [] | |
473 | for suffix in ('_small', '', '_large', '_x_large', '_original'): | |
474 | image = get_binding_value('player_image' + suffix) or {} | |
475 | image_url = image.get('url') | |
476 | if not image_url or '/player-placeholder' in image_url: | |
477 | continue | |
478 | thumbnails.append({ | |
479 | 'id': suffix[1:] if suffix else 'medium', | |
480 | 'url': image_url, | |
481 | 'width': int_or_none(image.get('width')), | |
482 | 'height': int_or_none(image.get('height')), | |
483 | }) | |
484 | ||
485 | info.update({ | |
486 | 'formats': formats, | |
487 | 'thumbnails': thumbnails, | |
488 | 'duration': int_or_none(get_binding_value( | |
489 | 'content_duration_seconds')), | |
490 | }) | |
491 | elif card_name == 'player': | |
492 | info.update({ | |
493 | '_type': 'url', | |
494 | 'url': get_binding_value('player_url'), | |
495 | }) | |
496 | elif card_name == 'periscope_broadcast': | |
497 | info.update({ | |
498 | '_type': 'url', | |
499 | 'url': get_binding_value('url') or get_binding_value('player_url'), | |
500 | 'ie_key': PeriscopeIE.ie_key(), | |
501 | }) | |
502 | elif card_name == 'broadcast': | |
503 | info.update({ | |
504 | '_type': 'url', | |
505 | 'url': get_binding_value('broadcast_url'), | |
506 | 'ie_key': TwitterBroadcastIE.ie_key(), | |
507 | }) | |
508 | else: | |
509 | raise ExtractorError('Unsupported Twitter Card.') | |
510 | else: | |
511 | expanded_url = try_get(status, lambda x: x['entities']['urls'][0]['expanded_url']) | |
512 | if not expanded_url: | |
513 | raise ExtractorError("There's no video in this tweet.") | |
514 | info.update({ | |
515 | '_type': 'url', | |
516 | 'url': expanded_url, | |
517 | }) | |
518 | return info | |
445d72b8 YCH |
519 | |
520 | ||
521 | class TwitterAmplifyIE(TwitterBaseIE): | |
522 | IE_NAME = 'twitter:amplify' | |
25042f73 | 523 | _VALID_URL = r'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})' |
445d72b8 YCH |
524 | |
525 | _TEST = { | |
526 | 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951', | |
527 | 'md5': '7df102d0b9fd7066b86f3159f8e81bf6', | |
528 | 'info_dict': { | |
529 | 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951', | |
530 | 'ext': 'mp4', | |
531 | 'title': 'Twitter Video', | |
bdbf4ba4 | 532 | 'thumbnail': 're:^https?://.*', |
445d72b8 YCH |
533 | }, |
534 | } | |
535 | ||
536 | def _real_extract(self, url): | |
537 | video_id = self._match_id(url) | |
538 | webpage = self._download_webpage(url, video_id) | |
539 | ||
540 | vmap_url = self._html_search_meta( | |
541 | 'twitter:amplify:vmap', webpage, 'vmap url') | |
9be31e77 | 542 | formats = self._extract_formats_from_vmap_url(vmap_url, video_id) |
445d72b8 | 543 | |
bdbf4ba4 YCH |
544 | thumbnails = [] |
545 | thumbnail = self._html_search_meta( | |
546 | 'twitter:image:src', webpage, 'thumbnail', fatal=False) | |
547 | ||
548 | def _find_dimension(target): | |
549 | w = int_or_none(self._html_search_meta( | |
550 | 'twitter:%s:width' % target, webpage, fatal=False)) | |
551 | h = int_or_none(self._html_search_meta( | |
552 | 'twitter:%s:height' % target, webpage, fatal=False)) | |
553 | return w, h | |
554 | ||
555 | if thumbnail: | |
556 | thumbnail_w, thumbnail_h = _find_dimension('image') | |
557 | thumbnails.append({ | |
558 | 'url': thumbnail, | |
559 | 'width': thumbnail_w, | |
560 | 'height': thumbnail_h, | |
561 | }) | |
562 | ||
563 | video_w, video_h = _find_dimension('player') | |
9be31e77 | 564 | formats[0].update({ |
bdbf4ba4 YCH |
565 | 'width': video_w, |
566 | 'height': video_h, | |
9be31e77 | 567 | }) |
bdbf4ba4 | 568 | |
445d72b8 YCH |
569 | return { |
570 | 'id': video_id, | |
571 | 'title': 'Twitter Video', | |
bdbf4ba4 YCH |
572 | 'formats': formats, |
573 | 'thumbnails': thumbnails, | |
445d72b8 | 574 | } |
18ca61c5 RA |
575 | |
576 | ||
577 | class TwitterBroadcastIE(TwitterBaseIE, PeriscopeBaseIE): | |
578 | IE_NAME = 'twitter:broadcast' | |
579 | _VALID_URL = TwitterBaseIE._BASE_REGEX + r'i/broadcasts/(?P<id>[0-9a-zA-Z]{13})' | |
580 | ||
7b0b53ea S |
581 | _TEST = { |
582 | # untitled Periscope video | |
583 | 'url': 'https://twitter.com/i/broadcasts/1yNGaQLWpejGj', | |
584 | 'info_dict': { | |
585 | 'id': '1yNGaQLWpejGj', | |
586 | 'ext': 'mp4', | |
587 | 'title': 'Andrea May Sahouri - Periscope Broadcast', | |
588 | 'uploader': 'Andrea May Sahouri', | |
589 | 'uploader_id': '1PXEdBZWpGwKe', | |
590 | }, | |
591 | } | |
592 | ||
18ca61c5 RA |
593 | def _real_extract(self, url): |
594 | broadcast_id = self._match_id(url) | |
595 | broadcast = self._call_api( | |
596 | 'broadcasts/show.json', broadcast_id, | |
597 | {'ids': broadcast_id})['broadcasts'][broadcast_id] | |
598 | info = self._parse_broadcast_data(broadcast, broadcast_id) | |
599 | media_key = broadcast['media_key'] | |
600 | source = self._call_api( | |
601 | 'live_video_stream/status/' + media_key, media_key)['source'] | |
602 | m3u8_url = source.get('noRedirectPlaybackUrl') or source['location'] | |
603 | if '/live_video_stream/geoblocked/' in m3u8_url: | |
604 | self.raise_geo_restricted() | |
605 | m3u8_id = compat_parse_qs(compat_urllib_parse_urlparse( | |
606 | m3u8_url).query).get('type', [None])[0] | |
607 | state, width, height = self._extract_common_format_info(broadcast) | |
608 | info['formats'] = self._extract_pscp_m3u8_formats( | |
609 | m3u8_url, broadcast_id, m3u8_id, state, width, height) | |
610 | return info | |
86b868c6 U |
611 | |
612 | ||
613 | class TwitterShortenerIE(TwitterBaseIE): | |
614 | IE_NAME = 'twitter:shortener' | |
a537ab1a U |
615 | _VALID_URL = r'https?://t.co/(?P<id>[^?]+)|tco:(?P<eid>[^?]+)' |
616 | _BASE_URL = 'https://t.co/' | |
86b868c6 U |
617 | |
618 | def _real_extract(self, url): | |
a537ab1a U |
619 | mobj = re.match(self._VALID_URL, url) |
620 | eid, id = mobj.group('eid', 'id') | |
621 | if eid: | |
622 | id = eid | |
623 | url = self._BASE_URL + id | |
624 | new_url = self._request_webpage(url, id, headers={'User-Agent': 'curl'}).geturl() | |
625 | __UNSAFE_LINK = "https://twitter.com/safety/unsafe_link_warning?unsafe_link=" | |
626 | if new_url.startswith(__UNSAFE_LINK): | |
627 | new_url = new_url.replace(__UNSAFE_LINK, "") | |
9e20a9c4 | 628 | return self.url_result(new_url) |