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