]>
Commit | Line | Data |
---|---|---|
04d02a9d | 1 | # coding: utf-8 |
ee1e1996 PH |
2 | from __future__ import unicode_literals |
3 | ||
841b6838 | 4 | import collections |
3182f3e2 | 5 | import itertools |
48afc6ca | 6 | import json |
841b6838 S |
7 | import random |
8 | import re | |
79e93125 PH |
9 | |
10 | from .common import InfoExtractor | |
1cc79574 | 11 | from ..compat import ( |
e704f87f | 12 | compat_parse_qs, |
240b9b7a | 13 | compat_str, |
15707c7e | 14 | compat_urllib_parse_urlencode, |
e704f87f | 15 | compat_urllib_parse_urlparse, |
1cc79574 PH |
16 | ) |
17 | from ..utils import ( | |
efe470e2 | 18 | clean_html, |
30a074c2 | 19 | dict_get, |
79e93125 | 20 | ExtractorError, |
841b6838 | 21 | float_or_none, |
7a6e8a1b | 22 | int_or_none, |
e704f87f | 23 | parse_duration, |
355d074f | 24 | parse_iso8601, |
4dfbf869 | 25 | parse_qs, |
a0455d0f | 26 | qualities, |
639f80c1 | 27 | str_or_none, |
28 | traverse_obj, | |
49fa7de3 S |
29 | try_get, |
30 | unified_timestamp, | |
264e77c4 | 31 | update_url_query, |
3052a30d | 32 | url_or_none, |
c64c03be | 33 | urljoin, |
79e93125 PH |
34 | ) |
35 | ||
36 | ||
c5db6bb3 | 37 | class TwitchBaseIE(InfoExtractor): |
fce79626 | 38 | _VALID_URL_BASE = r'https?://(?:(?:www|go|m)\.)?twitch\.tv' |
c5db6bb3 | 39 | |
46fd0dd5 | 40 | _API_BASE = 'https://api.twitch.tv' |
9f4576a7 | 41 | _USHER_BASE = 'https://usher.ttvnw.net' |
48afc6ca TB |
42 | _LOGIN_FORM_URL = 'https://www.twitch.tv/login' |
43 | _LOGIN_POST_URL = 'https://passport.twitch.tv/login' | |
cb1c3a3c | 44 | _CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko' |
499bfcbf | 45 | _NETRC_MACHINE = 'twitch' |
79e93125 | 46 | |
00dd0cd5 | 47 | _OPERATION_HASHES = { |
48 | 'CollectionSideBar': '27111f1b382effad0b6def325caef1909c733fe6a4fbabf54f8d491ef2cf2f14', | |
49 | 'FilterableVideoTower_Videos': 'a937f1d22e269e39a03b509f65a7490f9fc247d7f83d6ac1421523e3b68042cb', | |
50 | 'ClipsCards__User': 'b73ad2bfaecfd30a9e6c28fada15bd97032c83ec77a0440766a56fe0bd632777', | |
51 | 'ChannelCollectionsContent': '07e3691a1bad77a36aba590c351180439a40baefc1c275356f40fc7082419a84', | |
52 | 'StreamMetadata': '1c719a40e481453e5c48d9bb585d971b8b372f8ebb105b17076722264dfa5b3e', | |
53 | 'ComscoreStreamingQuery': 'e1edae8122517d013405f237ffcc124515dc6ded82480a88daef69c83b53ac01', | |
14eb1ee1 | 54 | 'VideoAccessToken_Clip': '36b89d2507fce29e5ca551df756d27c1cfe079e2609642b4390aa4c35796eb11', |
00dd0cd5 | 55 | 'VideoPreviewOverlay': '3006e77e51b128d838fa4e835723ca4dc9a05c5efd4466c1085215c6e437e65c', |
56 | 'VideoMetadata': '226edb3e692509f727fd56821f5653c05740242c82b0388883e0c0e75dcbf687', | |
639f80c1 | 57 | 'VideoPlayer_ChapterSelectButtonVideo': '8d2793384aac3773beab5e59bd5d6f585aedb923d292800119e03d40cd0f9b41', |
00dd0cd5 | 58 | } |
355d074f | 59 | |
52efa4b3 | 60 | def _perform_login(self, username, password): |
efe470e2 S |
61 | def fail(message): |
62 | raise ExtractorError( | |
63 | 'Unable to login. Twitch said: %s' % message, expected=True) | |
64 | ||
5316566e | 65 | def login_step(page, urlh, note, data): |
c64c03be XDG |
66 | form = self._hidden_inputs(page) |
67 | form.update(data) | |
68 | ||
69 | page_url = urlh.geturl() | |
70 | post_url = self._search_regex( | |
71 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', page, | |
48afc6ca | 72 | 'post url', default=self._LOGIN_POST_URL, group='url') |
c64c03be XDG |
73 | post_url = urljoin(page_url, post_url) |
74 | ||
48afc6ca TB |
75 | headers = { |
76 | 'Referer': page_url, | |
30a074c2 | 77 | 'Origin': 'https://www.twitch.tv', |
cb1c3a3c | 78 | 'Content-Type': 'text/plain;charset=UTF-8', |
48afc6ca | 79 | } |
c64c03be | 80 | |
cb1c3a3c S |
81 | response = self._download_json( |
82 | post_url, None, note, data=json.dumps(form).encode(), | |
83 | headers=headers, expected_status=400) | |
30a074c2 | 84 | error = dict_get(response, ('error', 'error_description', 'error_code')) |
cb1c3a3c S |
85 | if error: |
86 | fail(error) | |
c64c03be | 87 | |
1370dba5 S |
88 | if 'Authenticated successfully' in response.get('message', ''): |
89 | return None, None | |
90 | ||
91 | redirect_url = urljoin( | |
92 | post_url, | |
93 | response.get('redirect') or response['redirect_path']) | |
5316566e S |
94 | return self._download_webpage_handle( |
95 | redirect_url, None, 'Downloading login redirect page', | |
96 | headers=headers) | |
c64c03be | 97 | |
fbd9f6ea | 98 | login_page, handle = self._download_webpage_handle( |
48afc6ca | 99 | self._LOGIN_FORM_URL, None, 'Downloading login page') |
c5db6bb3 | 100 | |
efe470e2 S |
101 | # Some TOR nodes and public proxies are blocked completely |
102 | if 'blacklist_message' in login_page: | |
103 | fail(clean_html(login_page)) | |
104 | ||
5316566e | 105 | redirect_page, handle = login_step( |
e4d95865 | 106 | login_page, handle, 'Logging in', { |
5316566e S |
107 | 'username': username, |
108 | 'password': password, | |
48afc6ca | 109 | 'client_id': self._CLIENT_ID, |
fe646a2f | 110 | }) |
c64c03be | 111 | |
1370dba5 S |
112 | # Successful login |
113 | if not redirect_page: | |
114 | return | |
115 | ||
c64c03be XDG |
116 | if re.search(r'(?i)<form[^>]+id="two-factor-submit"', redirect_page) is not None: |
117 | # TODO: Add mechanism to request an SMS or phone call | |
118 | tfa_token = self._get_tfa_info('two-factor authentication token') | |
5316566e | 119 | login_step(redirect_page, handle, 'Submitting TFA token', { |
c64c03be XDG |
120 | 'authy_token': tfa_token, |
121 | 'remember_2fa': 'true', | |
5316566e | 122 | }) |
17b41a33 | 123 | |
d0e958c7 PH |
124 | def _prefer_source(self, formats): |
125 | try: | |
126 | source = next(f for f in formats if f['format_id'] == 'Source') | |
60e67c5b | 127 | source['quality'] = 10 |
d0e958c7 | 128 | except StopIteration: |
985637cb S |
129 | for f in formats: |
130 | if '/chunked/' in f['url']: | |
131 | f.update({ | |
60e67c5b | 132 | 'quality': 10, |
985637cb S |
133 | 'format_note': 'Source', |
134 | }) | |
d0e958c7 PH |
135 | self._sort_formats(formats) |
136 | ||
00dd0cd5 | 137 | def _download_base_gql(self, video_id, ops, note, fatal=True): |
30a074c2 | 138 | headers = { |
139 | 'Content-Type': 'text/plain;charset=UTF-8', | |
140 | 'Client-ID': self._CLIENT_ID, | |
141 | } | |
142 | gql_auth = self._get_cookies('https://gql.twitch.tv').get('auth-token') | |
143 | if gql_auth: | |
144 | headers['Authorization'] = 'OAuth ' + gql_auth.value | |
00dd0cd5 | 145 | return self._download_json( |
146 | 'https://gql.twitch.tv/gql', video_id, note, | |
147 | data=json.dumps(ops).encode(), | |
30a074c2 | 148 | headers=headers, fatal=fatal) |
c5db6bb3 | 149 | |
00dd0cd5 | 150 | def _download_gql(self, video_id, ops, note, fatal=True): |
151 | for op in ops: | |
152 | op['extensions'] = { | |
153 | 'persistedQuery': { | |
154 | 'version': 1, | |
155 | 'sha256Hash': self._OPERATION_HASHES[op['operationName']], | |
156 | } | |
157 | } | |
158 | return self._download_base_gql(video_id, ops, note) | |
159 | ||
160 | def _download_access_token(self, video_id, token_kind, param_name): | |
161 | method = '%sPlaybackAccessToken' % token_kind | |
162 | ops = { | |
163 | 'query': '''{ | |
164 | %s( | |
165 | %s: "%s", | |
166 | params: { | |
167 | platform: "web", | |
168 | playerBackend: "mediaplayer", | |
169 | playerType: "site" | |
170 | } | |
171 | ) | |
172 | { | |
173 | value | |
174 | signature | |
175 | } | |
176 | }''' % (method, param_name, video_id), | |
177 | } | |
178 | return self._download_base_gql( | |
179 | video_id, ops, | |
180 | 'Downloading %s access token GraphQL' % token_kind)['data'][method] | |
04d02a9d | 181 | |
04d02a9d | 182 | |
841b6838 | 183 | class TwitchVodIE(TwitchBaseIE): |
c5db6bb3 | 184 | IE_NAME = 'twitch:vod' |
3f1ce168 S |
185 | _VALID_URL = r'''(?x) |
186 | https?:// | |
187 | (?: | |
644921b3 | 188 | (?:(?:www|go|m)\.)?twitch\.tv/(?:[^/]+/v(?:ideo)?|videos)/| |
bc48773e | 189 | player\.twitch\.tv/\?.*?\bvideo=v? |
3f1ce168 S |
190 | ) |
191 | (?P<id>\d+) | |
192 | ''' | |
c5db6bb3 | 193 | |
9c724a98 | 194 | _TESTS = [{ |
e704f87f | 195 | 'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s', |
c5db6bb3 | 196 | 'info_dict': { |
ac0474f8 | 197 | 'id': 'v6528877', |
c5db6bb3 | 198 | 'ext': 'mp4', |
ac0474f8 | 199 | 'title': 'LCK Summer Split - Week 6 Day 1', |
ec85ded8 | 200 | 'thumbnail': r're:^https?://.*\.jpg$', |
ac0474f8 | 201 | 'duration': 17208, |
00dd0cd5 | 202 | 'timestamp': 1435131734, |
ac0474f8 YCH |
203 | 'upload_date': '20150624', |
204 | 'uploader': 'Riot Games', | |
205 | 'uploader_id': 'riotgames', | |
c5db6bb3 | 206 | 'view_count': int, |
e704f87f | 207 | 'start_time': 310, |
c5db6bb3 S |
208 | }, |
209 | 'params': { | |
210 | # m3u8 download | |
211 | 'skip_download': True, | |
212 | }, | |
9c724a98 S |
213 | }, { |
214 | # Untitled broadcast (title is None) | |
215 | 'url': 'http://www.twitch.tv/belkao_o/v/11230755', | |
216 | 'info_dict': { | |
217 | 'id': 'v11230755', | |
218 | 'ext': 'mp4', | |
219 | 'title': 'Untitled Broadcast', | |
ec85ded8 | 220 | 'thumbnail': r're:^https?://.*\.jpg$', |
9c724a98 S |
221 | 'duration': 1638, |
222 | 'timestamp': 1439746708, | |
223 | 'upload_date': '20150816', | |
224 | 'uploader': 'BelkAO_o', | |
225 | 'uploader_id': 'belkao_o', | |
226 | 'view_count': int, | |
227 | }, | |
228 | 'params': { | |
229 | # m3u8 download | |
230 | 'skip_download': True, | |
231 | }, | |
9bd7bd0b | 232 | 'skip': 'HTTP Error 404: Not Found', |
3f1ce168 S |
233 | }, { |
234 | 'url': 'http://player.twitch.tv/?t=5m10s&video=v6528877', | |
235 | 'only_matching': True, | |
0b23c222 S |
236 | }, { |
237 | 'url': 'https://www.twitch.tv/videos/6528877', | |
238 | 'only_matching': True, | |
fce79626 S |
239 | }, { |
240 | 'url': 'https://m.twitch.tv/beagsandjam/v/247478721', | |
241 | 'only_matching': True, | |
644921b3 S |
242 | }, { |
243 | 'url': 'https://www.twitch.tv/northernlion/video/291940395', | |
244 | 'only_matching': True, | |
bc48773e M |
245 | }, { |
246 | 'url': 'https://player.twitch.tv/?video=480452374', | |
247 | 'only_matching': True, | |
639f80c1 | 248 | }, { |
249 | 'url': 'https://www.twitch.tv/videos/635475444', | |
250 | 'info_dict': { | |
251 | 'id': 'v635475444', | |
252 | 'ext': 'mp4', | |
253 | 'title': 'Riot Games', | |
254 | 'duration': 11643, | |
255 | 'uploader': 'Riot Games', | |
256 | 'uploader_id': 'riotgames', | |
257 | 'timestamp': 1590770569, | |
258 | 'upload_date': '20200529', | |
259 | 'chapters': [ | |
260 | { | |
261 | 'start_time': 0, | |
262 | 'end_time': 573, | |
263 | 'title': 'League of Legends' | |
264 | }, | |
265 | { | |
266 | 'start_time': 573, | |
267 | 'end_time': 3922, | |
268 | 'title': 'Legends of Runeterra' | |
269 | }, | |
270 | { | |
271 | 'start_time': 3922, | |
272 | 'end_time': 11643, | |
273 | 'title': 'Art' | |
274 | } | |
275 | ], | |
276 | }, | |
277 | 'params': { | |
278 | 'skip_download': True | |
279 | } | |
9c724a98 | 280 | }] |
04d02a9d | 281 | |
841b6838 | 282 | def _download_info(self, item_id): |
00dd0cd5 | 283 | data = self._download_gql( |
284 | item_id, [{ | |
285 | 'operationName': 'VideoMetadata', | |
286 | 'variables': { | |
287 | 'channelLogin': '', | |
288 | 'videoID': item_id, | |
289 | }, | |
639f80c1 | 290 | }, { |
291 | 'operationName': 'VideoPlayer_ChapterSelectButtonVideo', | |
292 | 'variables': { | |
293 | 'includePrivate': False, | |
294 | 'videoID': item_id, | |
295 | }, | |
00dd0cd5 | 296 | }], |
639f80c1 | 297 | 'Downloading stream metadata GraphQL') |
298 | ||
299 | video = traverse_obj(data, (0, 'data', 'video')) | |
300 | video['moments'] = traverse_obj(data, (1, 'data', 'video', 'moments', 'edges', ..., 'node')) | |
301 | ||
00dd0cd5 | 302 | if video is None: |
303 | raise ExtractorError( | |
304 | 'Video %s does not exist' % item_id, expected=True) | |
305 | return self._extract_info_gql(video, item_id) | |
841b6838 | 306 | |
639f80c1 | 307 | def _extract_info(self, info): |
841b6838 S |
308 | status = info.get('status') |
309 | if status == 'recording': | |
310 | is_live = True | |
311 | elif status == 'recorded': | |
312 | is_live = False | |
313 | else: | |
314 | is_live = None | |
315 | _QUALITIES = ('small', 'medium', 'large') | |
316 | quality_key = qualities(_QUALITIES) | |
317 | thumbnails = [] | |
318 | preview = info.get('preview') | |
319 | if isinstance(preview, dict): | |
320 | for thumbnail_id, thumbnail_url in preview.items(): | |
321 | thumbnail_url = url_or_none(thumbnail_url) | |
322 | if not thumbnail_url: | |
323 | continue | |
324 | if thumbnail_id not in _QUALITIES: | |
325 | continue | |
326 | thumbnails.append({ | |
327 | 'url': thumbnail_url, | |
328 | 'preference': quality_key(thumbnail_id), | |
329 | }) | |
330 | return { | |
331 | 'id': info['_id'], | |
332 | 'title': info.get('title') or 'Untitled Broadcast', | |
333 | 'description': info.get('description'), | |
334 | 'duration': int_or_none(info.get('length')), | |
335 | 'thumbnails': thumbnails, | |
336 | 'uploader': info.get('channel', {}).get('display_name'), | |
337 | 'uploader_id': info.get('channel', {}).get('name'), | |
338 | 'timestamp': parse_iso8601(info.get('recorded_at')), | |
339 | 'view_count': int_or_none(info.get('views')), | |
340 | 'is_live': is_live, | |
a33c0d9c | 341 | 'was_live': True, |
841b6838 S |
342 | } |
343 | ||
639f80c1 | 344 | def _extract_moments(self, info, item_id): |
345 | for moment in info.get('moments') or []: | |
346 | start_time = int_or_none(moment.get('positionMilliseconds'), 1000) | |
347 | duration = int_or_none(moment.get('durationMilliseconds'), 1000) | |
348 | name = str_or_none(moment.get('description')) | |
349 | ||
350 | if start_time is None or duration is None: | |
351 | self.report_warning(f'Important chapter information missing for chapter {name}', item_id) | |
352 | continue | |
353 | yield { | |
354 | 'start_time': start_time, | |
355 | 'end_time': start_time + duration, | |
356 | 'title': name, | |
357 | } | |
358 | ||
359 | def _extract_info_gql(self, info, item_id): | |
00dd0cd5 | 360 | vod_id = info.get('id') or item_id |
361 | # id backward compatibility for download archives | |
362 | if vod_id[0] != 'v': | |
363 | vod_id = 'v%s' % vod_id | |
364 | thumbnail = url_or_none(info.get('previewThumbnailURL')) | |
a33c0d9c | 365 | is_live = None |
00dd0cd5 | 366 | if thumbnail: |
a33c0d9c D |
367 | if thumbnail.endswith('/404_processing_{width}x{height}.png'): |
368 | is_live, thumbnail = True, None | |
369 | else: | |
370 | is_live = False | |
371 | for p in ('width', 'height'): | |
372 | thumbnail = thumbnail.replace('{%s}' % p, '0') | |
639f80c1 | 373 | |
00dd0cd5 | 374 | return { |
375 | 'id': vod_id, | |
376 | 'title': info.get('title') or 'Untitled Broadcast', | |
377 | 'description': info.get('description'), | |
378 | 'duration': int_or_none(info.get('lengthSeconds')), | |
379 | 'thumbnail': thumbnail, | |
380 | 'uploader': try_get(info, lambda x: x['owner']['displayName'], compat_str), | |
381 | 'uploader_id': try_get(info, lambda x: x['owner']['login'], compat_str), | |
382 | 'timestamp': unified_timestamp(info.get('publishedAt')), | |
383 | 'view_count': int_or_none(info.get('viewCount')), | |
639f80c1 | 384 | 'chapters': list(self._extract_moments(info, item_id)), |
a33c0d9c D |
385 | 'is_live': is_live, |
386 | 'was_live': True, | |
00dd0cd5 | 387 | } |
388 | ||
79e93125 | 389 | def _real_extract(self, url): |
841b6838 | 390 | vod_id = self._match_id(url) |
e5e99661 | 391 | |
841b6838 | 392 | info = self._download_info(vod_id) |
00dd0cd5 | 393 | access_token = self._download_access_token(vod_id, 'video', 'id') |
e5e99661 | 394 | |
c5db6bb3 | 395 | formats = self._extract_m3u8_formats( |
2da4316e | 396 | '%s/vod/%s.m3u8?%s' % ( |
841b6838 | 397 | self._USHER_BASE, vod_id, |
15707c7e | 398 | compat_urllib_parse_urlencode({ |
e5e99661 | 399 | 'allow_source': 'true', |
ac455055 | 400 | 'allow_audio_only': 'true', |
e5e99661 S |
401 | 'allow_spectre': 'true', |
402 | 'player': 'twitchweb', | |
1d31b7ca | 403 | 'playlist_include_framerate': 'true', |
00dd0cd5 | 404 | 'nauth': access_token['value'], |
405 | 'nauthsig': access_token['signature'], | |
e5e99661 | 406 | })), |
841b6838 | 407 | vod_id, 'mp4', entry_protocol='m3u8_native') |
e5e99661 | 408 | |
d0e958c7 | 409 | self._prefer_source(formats) |
c5db6bb3 | 410 | info['formats'] = formats |
e704f87f NH |
411 | |
412 | parsed_url = compat_urllib_parse_urlparse(url) | |
413 | query = compat_parse_qs(parsed_url.query) | |
414 | if 't' in query: | |
415 | info['start_time'] = parse_duration(query['t'][0]) | |
416 | ||
264e77c4 S |
417 | if info.get('timestamp') is not None: |
418 | info['subtitles'] = { | |
419 | 'rechat': [{ | |
420 | 'url': update_url_query( | |
841b6838 | 421 | 'https://api.twitch.tv/v5/videos/%s/comments' % vod_id, { |
ce112a8c | 422 | 'client_id': self._CLIENT_ID, |
264e77c4 S |
423 | }), |
424 | 'ext': 'json', | |
425 | }], | |
426 | } | |
427 | ||
c5db6bb3 S |
428 | return info |
429 | ||
430 | ||
841b6838 S |
431 | def _make_video_result(node): |
432 | assert isinstance(node, dict) | |
433 | video_id = node.get('id') | |
434 | if not video_id: | |
435 | return | |
436 | return { | |
437 | '_type': 'url_transparent', | |
438 | 'ie_key': TwitchVodIE.ie_key(), | |
c76eb41b | 439 | 'id': 'v' + video_id, |
841b6838 S |
440 | 'url': 'https://www.twitch.tv/videos/%s' % video_id, |
441 | 'title': node.get('title'), | |
442 | 'thumbnail': node.get('previewThumbnailURL'), | |
443 | 'duration': float_or_none(node.get('lengthSeconds')), | |
444 | 'view_count': int_or_none(node.get('viewCount')), | |
445 | } | |
446 | ||
447 | ||
00dd0cd5 | 448 | class TwitchCollectionIE(TwitchBaseIE): |
841b6838 S |
449 | _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/collections/(?P<id>[^/]+)' |
450 | ||
451 | _TESTS = [{ | |
452 | 'url': 'https://www.twitch.tv/collections/wlDCoH0zEBZZbQ', | |
453 | 'info_dict': { | |
454 | 'id': 'wlDCoH0zEBZZbQ', | |
455 | 'title': 'Overthrow Nook, capitalism for children', | |
456 | }, | |
457 | 'playlist_mincount': 13, | |
458 | }] | |
459 | ||
460 | _OPERATION_NAME = 'CollectionSideBar' | |
841b6838 S |
461 | |
462 | def _real_extract(self, url): | |
463 | collection_id = self._match_id(url) | |
464 | collection = self._download_gql( | |
defc820b S |
465 | collection_id, [{ |
466 | 'operationName': self._OPERATION_NAME, | |
467 | 'variables': {'collectionID': collection_id}, | |
468 | }], | |
469 | 'Downloading collection GraphQL')[0]['data']['collection'] | |
841b6838 | 470 | title = collection.get('title') |
c5db6bb3 | 471 | entries = [] |
841b6838 S |
472 | for edge in collection['items']['edges']: |
473 | if not isinstance(edge, dict): | |
474 | continue | |
475 | node = edge.get('node') | |
476 | if not isinstance(node, dict): | |
477 | continue | |
478 | video = _make_video_result(node) | |
479 | if video: | |
480 | entries.append(video) | |
481 | return self.playlist_result( | |
482 | entries, playlist_id=collection_id, playlist_title=title) | |
483 | ||
484 | ||
00dd0cd5 | 485 | class TwitchPlaylistBaseIE(TwitchBaseIE): |
486 | _PAGE_LIMIT = 100 | |
487 | ||
841b6838 S |
488 | def _entries(self, channel_name, *args): |
489 | cursor = None | |
490 | variables_common = self._make_variables(channel_name, *args) | |
491 | entries_key = '%ss' % self._ENTRY_KIND | |
492 | for page_num in itertools.count(1): | |
493 | variables = variables_common.copy() | |
494 | variables['limit'] = self._PAGE_LIMIT | |
495 | if cursor: | |
496 | variables['cursor'] = cursor | |
497 | page = self._download_gql( | |
defc820b S |
498 | channel_name, [{ |
499 | 'operationName': self._OPERATION_NAME, | |
500 | 'variables': variables, | |
501 | }], | |
841b6838 S |
502 | 'Downloading %ss GraphQL page %s' % (self._NODE_KIND, page_num), |
503 | fatal=False) | |
504 | if not page: | |
505 | break | |
506 | edges = try_get( | |
defc820b | 507 | page, lambda x: x[0]['data']['user'][entries_key]['edges'], list) |
841b6838 S |
508 | if not edges: |
509 | break | |
510 | for edge in edges: | |
511 | if not isinstance(edge, dict): | |
512 | continue | |
513 | if edge.get('__typename') != self._EDGE_KIND: | |
514 | continue | |
515 | node = edge.get('node') | |
516 | if not isinstance(node, dict): | |
517 | continue | |
518 | if node.get('__typename') != self._NODE_KIND: | |
519 | continue | |
520 | entry = self._extract_entry(node) | |
521 | if entry: | |
522 | cursor = edge.get('cursor') | |
523 | yield entry | |
524 | if not cursor or not isinstance(cursor, compat_str): | |
525 | break | |
526 | ||
c5db6bb3 | 527 | |
841b6838 S |
528 | class TwitchVideosIE(TwitchPlaylistBaseIE): |
529 | _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/(?P<id>[^/]+)/(?:videos|profile)' | |
c5db6bb3 | 530 | |
fce79626 | 531 | _TESTS = [{ |
841b6838 S |
532 | # All Videos sorted by Date |
533 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=all', | |
c5db6bb3 | 534 | 'info_dict': { |
841b6838 S |
535 | 'id': 'spamfish', |
536 | 'title': 'spamfish - All Videos sorted by Date', | |
c5db6bb3 | 537 | }, |
841b6838 | 538 | 'playlist_mincount': 924, |
fce79626 | 539 | }, { |
841b6838 S |
540 | # All Videos sorted by Popular |
541 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=all&sort=views', | |
93753aad S |
542 | 'info_dict': { |
543 | 'id': 'spamfish', | |
841b6838 | 544 | 'title': 'spamfish - All Videos sorted by Popular', |
93753aad | 545 | }, |
841b6838 | 546 | 'playlist_mincount': 931, |
fce79626 | 547 | }, { |
841b6838 S |
548 | # Past Broadcasts sorted by Date |
549 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=archives', | |
550 | 'info_dict': { | |
551 | 'id': 'spamfish', | |
552 | 'title': 'spamfish - Past Broadcasts sorted by Date', | |
553 | }, | |
554 | 'playlist_mincount': 27, | |
555 | }, { | |
556 | # Highlights sorted by Date | |
557 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=highlights', | |
558 | 'info_dict': { | |
559 | 'id': 'spamfish', | |
560 | 'title': 'spamfish - Highlights sorted by Date', | |
561 | }, | |
562 | 'playlist_mincount': 901, | |
563 | }, { | |
564 | # Uploads sorted by Date | |
565 | 'url': 'https://www.twitch.tv/esl_csgo/videos?filter=uploads&sort=time', | |
566 | 'info_dict': { | |
567 | 'id': 'esl_csgo', | |
568 | 'title': 'esl_csgo - Uploads sorted by Date', | |
569 | }, | |
570 | 'playlist_mincount': 5, | |
571 | }, { | |
572 | # Past Premieres sorted by Date | |
573 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=past_premieres', | |
93753aad S |
574 | 'info_dict': { |
575 | 'id': 'spamfish', | |
841b6838 | 576 | 'title': 'spamfish - Past Premieres sorted by Date', |
93753aad | 577 | }, |
841b6838 | 578 | 'playlist_mincount': 1, |
fce79626 | 579 | }, { |
841b6838 S |
580 | 'url': 'https://www.twitch.tv/spamfish/videos/all', |
581 | 'only_matching': True, | |
582 | }, { | |
583 | 'url': 'https://m.twitch.tv/spamfish/videos/all', | |
584 | 'only_matching': True, | |
585 | }, { | |
586 | 'url': 'https://www.twitch.tv/spamfish/videos', | |
fce79626 S |
587 | 'only_matching': True, |
588 | }] | |
93753aad | 589 | |
841b6838 S |
590 | Broadcast = collections.namedtuple('Broadcast', ['type', 'label']) |
591 | ||
592 | _DEFAULT_BROADCAST = Broadcast(None, 'All Videos') | |
593 | _BROADCASTS = { | |
594 | 'archives': Broadcast('ARCHIVE', 'Past Broadcasts'), | |
595 | 'highlights': Broadcast('HIGHLIGHT', 'Highlights'), | |
596 | 'uploads': Broadcast('UPLOAD', 'Uploads'), | |
597 | 'past_premieres': Broadcast('PAST_PREMIERE', 'Past Premieres'), | |
598 | 'all': _DEFAULT_BROADCAST, | |
599 | } | |
600 | ||
601 | _DEFAULT_SORTED_BY = 'Date' | |
602 | _SORTED_BY = { | |
603 | 'time': _DEFAULT_SORTED_BY, | |
604 | 'views': 'Popular', | |
605 | } | |
606 | ||
841b6838 S |
607 | _OPERATION_NAME = 'FilterableVideoTower_Videos' |
608 | _ENTRY_KIND = 'video' | |
609 | _EDGE_KIND = 'VideoEdge' | |
610 | _NODE_KIND = 'Video' | |
611 | ||
612 | @classmethod | |
613 | def suitable(cls, url): | |
614 | return (False | |
615 | if any(ie.suitable(url) for ie in ( | |
616 | TwitchVideosClipsIE, | |
617 | TwitchVideosCollectionsIE)) | |
618 | else super(TwitchVideosIE, cls).suitable(url)) | |
619 | ||
620 | @staticmethod | |
621 | def _make_variables(channel_name, broadcast_type, sort): | |
622 | return { | |
623 | 'channelOwnerLogin': channel_name, | |
624 | 'broadcastType': broadcast_type, | |
625 | 'videoSort': sort.upper(), | |
626 | } | |
627 | ||
628 | @staticmethod | |
629 | def _extract_entry(node): | |
630 | return _make_video_result(node) | |
93753aad | 631 | |
841b6838 S |
632 | def _real_extract(self, url): |
633 | channel_name = self._match_id(url) | |
4dfbf869 | 634 | qs = parse_qs(url) |
841b6838 S |
635 | filter = qs.get('filter', ['all'])[0] |
636 | sort = qs.get('sort', ['time'])[0] | |
637 | broadcast = self._BROADCASTS.get(filter, self._DEFAULT_BROADCAST) | |
638 | return self.playlist_result( | |
639 | self._entries(channel_name, broadcast.type, sort), | |
640 | playlist_id=channel_name, | |
641 | playlist_title='%s - %s sorted by %s' | |
642 | % (channel_name, broadcast.label, | |
643 | self._SORTED_BY.get(sort, self._DEFAULT_SORTED_BY))) | |
644 | ||
645 | ||
646 | class TwitchVideosClipsIE(TwitchPlaylistBaseIE): | |
647 | _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/(?P<id>[^/]+)/(?:clips|videos/*?\?.*?\bfilter=clips)' | |
c5db6bb3 | 648 | |
fce79626 | 649 | _TESTS = [{ |
841b6838 S |
650 | # Clips |
651 | 'url': 'https://www.twitch.tv/vanillatv/clips?filter=clips&range=all', | |
93753aad | 652 | 'info_dict': { |
841b6838 S |
653 | 'id': 'vanillatv', |
654 | 'title': 'vanillatv - Clips Top All', | |
93753aad | 655 | }, |
841b6838 | 656 | 'playlist_mincount': 1, |
fce79626 | 657 | }, { |
841b6838 | 658 | 'url': 'https://www.twitch.tv/dota2ruhub/videos?filter=clips&range=7d', |
fce79626 S |
659 | 'only_matching': True, |
660 | }] | |
93753aad | 661 | |
841b6838 S |
662 | Clip = collections.namedtuple('Clip', ['filter', 'label']) |
663 | ||
664 | _DEFAULT_CLIP = Clip('LAST_WEEK', 'Top 7D') | |
665 | _RANGE = { | |
666 | '24hr': Clip('LAST_DAY', 'Top 24H'), | |
667 | '7d': _DEFAULT_CLIP, | |
668 | '30d': Clip('LAST_MONTH', 'Top 30D'), | |
669 | 'all': Clip('ALL_TIME', 'Top All'), | |
670 | } | |
671 | ||
672 | # NB: values other than 20 result in skipped videos | |
673 | _PAGE_LIMIT = 20 | |
674 | ||
841b6838 S |
675 | _OPERATION_NAME = 'ClipsCards__User' |
676 | _ENTRY_KIND = 'clip' | |
677 | _EDGE_KIND = 'ClipEdge' | |
678 | _NODE_KIND = 'Clip' | |
679 | ||
680 | @staticmethod | |
681 | def _make_variables(channel_name, filter): | |
682 | return { | |
683 | 'login': channel_name, | |
684 | 'criteria': { | |
685 | 'filter': filter, | |
686 | }, | |
687 | } | |
688 | ||
689 | @staticmethod | |
690 | def _extract_entry(node): | |
691 | assert isinstance(node, dict) | |
692 | clip_url = url_or_none(node.get('url')) | |
693 | if not clip_url: | |
694 | return | |
695 | return { | |
696 | '_type': 'url_transparent', | |
697 | 'ie_key': TwitchClipsIE.ie_key(), | |
698 | 'id': node.get('id'), | |
699 | 'url': clip_url, | |
700 | 'title': node.get('title'), | |
701 | 'thumbnail': node.get('thumbnailURL'), | |
702 | 'duration': float_or_none(node.get('durationSeconds')), | |
703 | 'timestamp': unified_timestamp(node.get('createdAt')), | |
704 | 'view_count': int_or_none(node.get('viewCount')), | |
705 | 'language': node.get('language'), | |
706 | } | |
707 | ||
708 | def _real_extract(self, url): | |
709 | channel_name = self._match_id(url) | |
4dfbf869 | 710 | qs = parse_qs(url) |
841b6838 S |
711 | range = qs.get('range', ['7d'])[0] |
712 | clip = self._RANGE.get(range, self._DEFAULT_CLIP) | |
713 | return self.playlist_result( | |
714 | self._entries(channel_name, clip.filter), | |
715 | playlist_id=channel_name, | |
716 | playlist_title='%s - Clips %s' % (channel_name, clip.label)) | |
717 | ||
93753aad | 718 | |
841b6838 S |
719 | class TwitchVideosCollectionsIE(TwitchPlaylistBaseIE): |
720 | _VALID_URL = r'https?://(?:(?:www|go|m)\.)?twitch\.tv/(?P<id>[^/]+)/videos/*?\?.*?\bfilter=collections' | |
93753aad | 721 | |
fce79626 | 722 | _TESTS = [{ |
841b6838 S |
723 | # Collections |
724 | 'url': 'https://www.twitch.tv/spamfish/videos?filter=collections', | |
c5db6bb3 S |
725 | 'info_dict': { |
726 | 'id': 'spamfish', | |
841b6838 | 727 | 'title': 'spamfish - Collections', |
c5db6bb3 | 728 | }, |
841b6838 | 729 | 'playlist_mincount': 3, |
fce79626 | 730 | }] |
240b9b7a | 731 | |
841b6838 S |
732 | _OPERATION_NAME = 'ChannelCollectionsContent' |
733 | _ENTRY_KIND = 'collection' | |
734 | _EDGE_KIND = 'CollectionsItemEdge' | |
735 | _NODE_KIND = 'Collection' | |
736 | ||
737 | @staticmethod | |
738 | def _make_variables(channel_name): | |
739 | return { | |
740 | 'ownerLogin': channel_name, | |
741 | } | |
742 | ||
743 | @staticmethod | |
744 | def _extract_entry(node): | |
745 | assert isinstance(node, dict) | |
746 | collection_id = node.get('id') | |
747 | if not collection_id: | |
748 | return | |
749 | return { | |
750 | '_type': 'url_transparent', | |
751 | 'ie_key': TwitchCollectionIE.ie_key(), | |
752 | 'id': collection_id, | |
753 | 'url': 'https://www.twitch.tv/collections/%s' % collection_id, | |
754 | 'title': node.get('title'), | |
755 | 'thumbnail': node.get('thumbnailURL'), | |
756 | 'duration': float_or_none(node.get('lengthSeconds')), | |
757 | 'timestamp': unified_timestamp(node.get('updatedAt')), | |
758 | 'view_count': int_or_none(node.get('viewCount')), | |
759 | } | |
760 | ||
761 | def _real_extract(self, url): | |
762 | channel_name = self._match_id(url) | |
763 | return self.playlist_result( | |
764 | self._entries(channel_name), playlist_id=channel_name, | |
765 | playlist_title='%s - Collections' % channel_name) | |
766 | ||
240b9b7a | 767 | |
00dd0cd5 | 768 | class TwitchStreamIE(TwitchBaseIE): |
240b9b7a | 769 | IE_NAME = 'twitch:stream' |
9db8f6c5 S |
770 | _VALID_URL = r'''(?x) |
771 | https?:// | |
772 | (?: | |
fce79626 | 773 | (?:(?:www|go|m)\.)?twitch\.tv/| |
9db8f6c5 S |
774 | player\.twitch\.tv/\?.*?\bchannel= |
775 | ) | |
776 | (?P<id>[^/#?]+) | |
777 | ''' | |
240b9b7a | 778 | |
faa1f83a | 779 | _TESTS = [{ |
240b9b7a S |
780 | 'url': 'http://www.twitch.tv/shroomztv', |
781 | 'info_dict': { | |
782 | 'id': '12772022048', | |
783 | 'display_id': 'shroomztv', | |
784 | 'ext': 'mp4', | |
785 | 'title': 're:^ShroomzTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
786 | 'description': 'H1Z1 - lonewolfing with ShroomzTV | A3 Battle Royale later - @ShroomzTV', | |
787 | 'is_live': True, | |
788 | 'timestamp': 1421928037, | |
789 | 'upload_date': '20150122', | |
790 | 'uploader': 'ShroomzTV', | |
791 | 'uploader_id': 'shroomztv', | |
792 | 'view_count': int, | |
793 | }, | |
794 | 'params': { | |
795 | # m3u8 download | |
796 | 'skip_download': True, | |
797 | }, | |
faa1f83a S |
798 | }, { |
799 | 'url': 'http://www.twitch.tv/miracle_doto#profile-0', | |
800 | 'only_matching': True, | |
9db8f6c5 S |
801 | }, { |
802 | 'url': 'https://player.twitch.tv/?channel=lotsofs', | |
803 | 'only_matching': True, | |
b763e1d6 S |
804 | }, { |
805 | 'url': 'https://go.twitch.tv/food', | |
806 | 'only_matching': True, | |
fce79626 S |
807 | }, { |
808 | 'url': 'https://m.twitch.tv/food', | |
809 | 'only_matching': True, | |
faa1f83a | 810 | }] |
240b9b7a | 811 | |
9db8f6c5 S |
812 | @classmethod |
813 | def suitable(cls, url): | |
814 | return (False | |
815 | if any(ie.suitable(url) for ie in ( | |
9db8f6c5 | 816 | TwitchVodIE, |
841b6838 S |
817 | TwitchCollectionIE, |
818 | TwitchVideosIE, | |
819 | TwitchVideosClipsIE, | |
820 | TwitchVideosCollectionsIE, | |
db348e88 | 821 | TwitchClipsIE)) |
9db8f6c5 S |
822 | else super(TwitchStreamIE, cls).suitable(url)) |
823 | ||
240b9b7a | 824 | def _real_extract(self, url): |
defc820b S |
825 | channel_name = self._match_id(url).lower() |
826 | ||
827 | gql = self._download_gql( | |
828 | channel_name, [{ | |
829 | 'operationName': 'StreamMetadata', | |
830 | 'variables': {'channelLogin': channel_name}, | |
831 | }, { | |
832 | 'operationName': 'ComscoreStreamingQuery', | |
833 | 'variables': { | |
834 | 'channel': channel_name, | |
835 | 'clipSlug': '', | |
836 | 'isClip': False, | |
837 | 'isLive': True, | |
838 | 'isVodOrCollection': False, | |
839 | 'vodID': '', | |
840 | }, | |
841 | }, { | |
842 | 'operationName': 'VideoPreviewOverlay', | |
843 | 'variables': {'login': channel_name}, | |
844 | }], | |
845 | 'Downloading stream GraphQL') | |
846 | ||
847 | user = gql[0]['data']['user'] | |
848 | ||
849 | if not user: | |
850 | raise ExtractorError( | |
851 | '%s does not exist' % channel_name, expected=True) | |
240b9b7a | 852 | |
defc820b | 853 | stream = user['stream'] |
240b9b7a | 854 | |
240b9b7a | 855 | if not stream: |
defc820b | 856 | raise ExtractorError('%s is offline' % channel_name, expected=True) |
240b9b7a | 857 | |
00dd0cd5 | 858 | access_token = self._download_access_token( |
859 | channel_name, 'stream', 'channelName') | |
860 | token = access_token['value'] | |
240b9b7a | 861 | |
defc820b | 862 | stream_id = stream.get('id') or channel_name |
240b9b7a S |
863 | query = { |
864 | 'allow_source': 'true', | |
ac455055 | 865 | 'allow_audio_only': 'true', |
9aa929d3 | 866 | 'allow_spectre': 'true', |
f353cbdb | 867 | 'p': random.randint(1000000, 10000000), |
240b9b7a | 868 | 'player': 'twitchweb', |
1d31b7ca | 869 | 'playlist_include_framerate': 'true', |
240b9b7a | 870 | 'segment_preference': '4', |
00dd0cd5 | 871 | 'sig': access_token['signature'].encode('utf-8'), |
ce3735df | 872 | 'token': token.encode('utf-8'), |
240b9b7a | 873 | } |
240b9b7a | 874 | formats = self._extract_m3u8_formats( |
defc820b S |
875 | '%s/api/channel/hls/%s.m3u8' % (self._USHER_BASE, channel_name), |
876 | stream_id, 'mp4', query=query) | |
d0e958c7 | 877 | self._prefer_source(formats) |
240b9b7a S |
878 | |
879 | view_count = stream.get('viewers') | |
defc820b | 880 | timestamp = unified_timestamp(stream.get('createdAt')) |
240b9b7a | 881 | |
defc820b S |
882 | sq_user = try_get(gql, lambda x: x[1]['data']['user'], dict) or {} |
883 | uploader = sq_user.get('displayName') | |
884 | description = try_get( | |
885 | sq_user, lambda x: x['broadcastSettings']['title'], compat_str) | |
240b9b7a | 886 | |
defc820b S |
887 | thumbnail = url_or_none(try_get( |
888 | gql, lambda x: x[2]['data']['user']['stream']['previewImageURL'], | |
889 | compat_str)) | |
890 | ||
891 | title = uploader or channel_name | |
892 | stream_type = stream.get('type') | |
893 | if stream_type in ['rerun', 'live']: | |
894 | title += ' (%s)' % stream_type | |
240b9b7a S |
895 | |
896 | return { | |
defc820b | 897 | 'id': stream_id, |
ce3735df | 898 | 'display_id': channel_name, |
39ca3b5c | 899 | 'title': title, |
240b9b7a | 900 | 'description': description, |
defc820b S |
901 | 'thumbnail': thumbnail, |
902 | 'uploader': uploader, | |
903 | 'uploader_id': channel_name, | |
240b9b7a S |
904 | 'timestamp': timestamp, |
905 | 'view_count': view_count, | |
906 | 'formats': formats, | |
defc820b | 907 | 'is_live': stream_type == 'live', |
12d1fb5a | 908 | } |
778f9694 S |
909 | |
910 | ||
49fa7de3 | 911 | class TwitchClipsIE(TwitchBaseIE): |
778f9694 | 912 | IE_NAME = 'twitch:clips' |
c9595ee7 FS |
913 | _VALID_URL = r'''(?x) |
914 | https?:// | |
915 | (?: | |
916 | clips\.twitch\.tv/(?:embed\?.*?\bclip=|(?:[^/]+/)*)| | |
917 | (?:(?:www|go|m)\.)?twitch\.tv/[^/]+/clip/ | |
918 | ) | |
919 | (?P<id>[^/?#&]+) | |
920 | ''' | |
778f9694 | 921 | |
74ba450a | 922 | _TESTS = [{ |
49fa7de3 | 923 | 'url': 'https://clips.twitch.tv/FaintLightGullWholeWheat', |
778f9694 S |
924 | 'md5': '761769e1eafce0ffebfb4089cb3847cd', |
925 | 'info_dict': { | |
49fa7de3 | 926 | 'id': '42850523', |
678da2f2 | 927 | 'display_id': 'FaintLightGullWholeWheat', |
778f9694 S |
928 | 'ext': 'mp4', |
929 | 'title': 'EA Play 2016 Live from the Novo Theatre', | |
ec85ded8 | 930 | 'thumbnail': r're:^https?://.*\.jpg', |
49fa7de3 S |
931 | 'timestamp': 1465767393, |
932 | 'upload_date': '20160612', | |
778f9694 S |
933 | 'creator': 'EA', |
934 | 'uploader': 'stereotype_', | |
49fa7de3 | 935 | 'uploader_id': '43566419', |
778f9694 | 936 | }, |
74ba450a S |
937 | }, { |
938 | # multiple formats | |
939 | 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy', | |
940 | 'only_matching': True, | |
db348e88 S |
941 | }, { |
942 | 'url': 'https://www.twitch.tv/sergeynixon/clip/StormyThankfulSproutFutureMan', | |
943 | 'only_matching': True, | |
0b16b3c2 RA |
944 | }, { |
945 | 'url': 'https://clips.twitch.tv/embed?clip=InquisitiveBreakableYogurtJebaited', | |
946 | 'only_matching': True, | |
c9595ee7 FS |
947 | }, { |
948 | 'url': 'https://m.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank', | |
949 | 'only_matching': True, | |
950 | }, { | |
951 | 'url': 'https://go.twitch.tv/rossbroadcast/clip/ConfidentBraveHumanChefFrank', | |
952 | 'only_matching': True, | |
74ba450a | 953 | }] |
778f9694 S |
954 | |
955 | def _real_extract(self, url): | |
956 | video_id = self._match_id(url) | |
957 | ||
14eb1ee1 | 958 | clip = self._download_gql( |
959 | video_id, [{ | |
960 | 'operationName': 'VideoAccessToken_Clip', | |
961 | 'variables': { | |
962 | 'slug': video_id, | |
963 | }, | |
964 | }], | |
965 | 'Downloading clip access token GraphQL')[0]['data']['clip'] | |
966 | ||
967 | if not clip: | |
968 | raise ExtractorError( | |
969 | 'This clip is no longer available', expected=True) | |
970 | ||
971 | access_query = { | |
972 | 'sig': clip['playbackAccessToken']['signature'], | |
973 | 'token': clip['playbackAccessToken']['value'], | |
974 | } | |
975 | ||
976 | data = self._download_base_gql( | |
00dd0cd5 | 977 | video_id, { |
232ed8e6 RA |
978 | 'query': '''{ |
979 | clip(slug: "%s") { | |
980 | broadcaster { | |
981 | displayName | |
982 | } | |
983 | createdAt | |
984 | curator { | |
985 | displayName | |
986 | id | |
987 | } | |
988 | durationSeconds | |
989 | id | |
990 | tiny: thumbnailURL(width: 86, height: 45) | |
991 | small: thumbnailURL(width: 260, height: 147) | |
992 | medium: thumbnailURL(width: 480, height: 272) | |
993 | title | |
994 | videoQualities { | |
995 | frameRate | |
996 | quality | |
997 | sourceURL | |
998 | } | |
999 | viewCount | |
1000 | } | |
14eb1ee1 | 1001 | }''' % video_id}, 'Downloading clip GraphQL', fatal=False) |
232ed8e6 | 1002 | |
14eb1ee1 | 1003 | if data: |
1004 | clip = try_get(data, lambda x: x['data']['clip'], dict) or clip | |
778f9694 | 1005 | |
49fa7de3 | 1006 | formats = [] |
232ed8e6 | 1007 | for option in clip.get('videoQualities', []): |
49fa7de3 S |
1008 | if not isinstance(option, dict): |
1009 | continue | |
232ed8e6 | 1010 | source = url_or_none(option.get('sourceURL')) |
3052a30d | 1011 | if not source: |
49fa7de3 S |
1012 | continue |
1013 | formats.append({ | |
14eb1ee1 | 1014 | 'url': update_url_query(source, access_query), |
49fa7de3 S |
1015 | 'format_id': option.get('quality'), |
1016 | 'height': int_or_none(option.get('quality')), | |
232ed8e6 | 1017 | 'fps': int_or_none(option.get('frameRate')), |
49fa7de3 | 1018 | }) |
07acdc5a S |
1019 | self._sort_formats(formats) |
1020 | ||
232ed8e6 RA |
1021 | thumbnails = [] |
1022 | for thumbnail_id in ('tiny', 'small', 'medium'): | |
1023 | thumbnail_url = clip.get(thumbnail_id) | |
1024 | if not thumbnail_url: | |
1025 | continue | |
1026 | thumb = { | |
1027 | 'id': thumbnail_id, | |
1028 | 'url': thumbnail_url, | |
1029 | } | |
1030 | mobj = re.search(r'-(\d+)x(\d+)\.', thumbnail_url) | |
1031 | if mobj: | |
1032 | thumb.update({ | |
1033 | 'height': int(mobj.group(2)), | |
1034 | 'width': int(mobj.group(1)), | |
1035 | }) | |
1036 | thumbnails.append(thumb) | |
1037 | ||
1038 | return { | |
1039 | 'id': clip.get('id') or video_id, | |
678da2f2 | 1040 | 'display_id': video_id, |
232ed8e6 | 1041 | 'title': clip.get('title') or video_id, |
49fa7de3 | 1042 | 'formats': formats, |
232ed8e6 | 1043 | 'duration': int_or_none(clip.get('durationSeconds')), |
be8cd3cb | 1044 | 'view_count': int_or_none(clip.get('viewCount')), |
232ed8e6 RA |
1045 | 'timestamp': unified_timestamp(clip.get('createdAt')), |
1046 | 'thumbnails': thumbnails, | |
1047 | 'creator': try_get(clip, lambda x: x['broadcaster']['displayName'], compat_str), | |
1048 | 'uploader': try_get(clip, lambda x: x['curator']['displayName'], compat_str), | |
1049 | 'uploader_id': try_get(clip, lambda x: x['curator']['id'], compat_str), | |
49fa7de3 | 1050 | } |