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