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