]>
Commit | Line | Data |
---|---|---|
04d02a9d | 1 | # coding: utf-8 |
ee1e1996 PH |
2 | from __future__ import unicode_literals |
3 | ||
3182f3e2 | 4 | import itertools |
79e93125 | 5 | import re |
f353cbdb | 6 | import random |
79e93125 PH |
7 | |
8 | from .common import InfoExtractor | |
1cc79574 | 9 | from ..compat import ( |
efe470e2 | 10 | compat_HTTPError, |
e704f87f | 11 | compat_parse_qs, |
240b9b7a | 12 | compat_str, |
15707c7e | 13 | compat_urllib_parse_urlencode, |
e704f87f | 14 | compat_urllib_parse_urlparse, |
03c635a4 | 15 | compat_urlparse, |
1cc79574 PH |
16 | ) |
17 | from ..utils import ( | |
efe470e2 | 18 | clean_html, |
79e93125 | 19 | ExtractorError, |
7a6e8a1b | 20 | int_or_none, |
778f9694 | 21 | js_to_json, |
8bbb4b56 | 22 | orderedSet, |
e704f87f | 23 | parse_duration, |
355d074f | 24 | parse_iso8601, |
264e77c4 | 25 | update_url_query, |
6e6bc8da | 26 | urlencode_postdata, |
79e93125 PH |
27 | ) |
28 | ||
29 | ||
c5db6bb3 | 30 | class TwitchBaseIE(InfoExtractor): |
240b9b7a | 31 | _VALID_URL_BASE = r'https?://(?:www\.)?twitch\.tv' |
c5db6bb3 | 32 | |
46fd0dd5 | 33 | _API_BASE = 'https://api.twitch.tv' |
9f4576a7 | 34 | _USHER_BASE = 'https://usher.ttvnw.net' |
fbd9f6ea | 35 | _LOGIN_URL = 'http://www.twitch.tv/login' |
95be29e1 | 36 | _CLIENT_ID = 'jzkbprff40iqj646a697cyrvl0zt2m6' |
499bfcbf | 37 | _NETRC_MACHINE = 'twitch' |
79e93125 | 38 | |
355d074f S |
39 | def _handle_error(self, response): |
40 | if not isinstance(response, dict): | |
41 | return | |
42 | error = response.get('error') | |
43 | if error: | |
44 | raise ExtractorError( | |
45 | '%s returned error: %s - %s' % (self.IE_NAME, error, response.get('message')), | |
46 | expected=True) | |
47 | ||
e3f6b569 | 48 | def _call_api(self, path, item_id, note): |
e3f6b569 | 49 | response = self._download_json( |
95be29e1 S |
50 | '%s/%s' % (self._API_BASE, path), item_id, note, |
51 | headers={'Client-ID': self._CLIENT_ID}) | |
355d074f S |
52 | self._handle_error(response) |
53 | return response | |
54 | ||
c5db6bb3 S |
55 | def _real_initialize(self): |
56 | self._login() | |
57 | ||
58 | def _login(self): | |
59 | (username, password) = self._get_login_info() | |
60 | if username is None: | |
61 | return | |
62 | ||
efe470e2 S |
63 | def fail(message): |
64 | raise ExtractorError( | |
65 | 'Unable to login. Twitch said: %s' % message, expected=True) | |
66 | ||
fbd9f6ea | 67 | login_page, handle = self._download_webpage_handle( |
c5db6bb3 S |
68 | self._LOGIN_URL, None, 'Downloading login page') |
69 | ||
efe470e2 S |
70 | # Some TOR nodes and public proxies are blocked completely |
71 | if 'blacklist_message' in login_page: | |
72 | fail(clean_html(login_page)) | |
73 | ||
f8da79f8 | 74 | login_form = self._hidden_inputs(login_page) |
c5db6bb3 | 75 | |
9296e92e | 76 | login_form.update({ |
fbd9f6ea S |
77 | 'username': username, |
78 | 'password': password, | |
9296e92e | 79 | }) |
c5db6bb3 | 80 | |
fbd9f6ea S |
81 | redirect_url = handle.geturl() |
82 | ||
03c635a4 S |
83 | post_url = self._search_regex( |
84 | r'<form[^>]+action=(["\'])(?P<url>.+?)\1', login_page, | |
fbd9f6ea | 85 | 'post url', default=redirect_url, group='url') |
03c635a4 S |
86 | |
87 | if not post_url.startswith('http'): | |
fbd9f6ea | 88 | post_url = compat_urlparse.urljoin(redirect_url, post_url) |
03c635a4 | 89 | |
efe470e2 | 90 | headers = {'Referer': redirect_url} |
c5db6bb3 | 91 | |
efe470e2 S |
92 | try: |
93 | response = self._download_json( | |
94 | post_url, None, 'Logging in as %s' % username, | |
95 | data=urlencode_postdata(login_form), | |
96 | headers=headers) | |
97 | except ExtractorError as e: | |
98 | if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400: | |
99 | response = self._parse_json( | |
100 | e.cause.read().decode('utf-8'), None) | |
101 | fail(response['message']) | |
102 | raise | |
103 | ||
104 | if response.get('redirect'): | |
105 | self._download_webpage( | |
106 | response['redirect'], None, 'Downloading login redirect page', | |
107 | headers=headers) | |
17b41a33 | 108 | |
d0e958c7 PH |
109 | def _prefer_source(self, formats): |
110 | try: | |
111 | source = next(f for f in formats if f['format_id'] == 'Source') | |
112 | source['preference'] = 10 | |
113 | except StopIteration: | |
114 | pass # No Source stream present | |
115 | self._sort_formats(formats) | |
116 | ||
c5db6bb3 S |
117 | |
118 | class TwitchItemBaseIE(TwitchBaseIE): | |
119 | def _download_info(self, item, item_id): | |
e3f6b569 S |
120 | return self._extract_info(self._call_api( |
121 | 'kraken/videos/%s%s' % (item, item_id), item_id, | |
c5db6bb3 | 122 | 'Downloading %s info JSON' % self._ITEM_TYPE)) |
a22524b0 | 123 | |
c5db6bb3 S |
124 | def _extract_media(self, item_id): |
125 | info = self._download_info(self._ITEM_SHORTCUT, item_id) | |
e3f6b569 S |
126 | response = self._call_api( |
127 | 'api/videos/%s%s' % (self._ITEM_SHORTCUT, item_id), item_id, | |
c5db6bb3 | 128 | 'Downloading %s playlist JSON' % self._ITEM_TYPE) |
355d074f S |
129 | entries = [] |
130 | chunks = response['chunks'] | |
131 | qualities = list(chunks.keys()) | |
132 | for num, fragment in enumerate(zip(*chunks.values()), start=1): | |
133 | formats = [] | |
134 | for fmt_num, fragment_fmt in enumerate(fragment): | |
135 | format_id = qualities[fmt_num] | |
136 | fmt = { | |
137 | 'url': fragment_fmt['url'], | |
138 | 'format_id': format_id, | |
139 | 'quality': 1 if format_id == 'live' else 0, | |
140 | } | |
141 | m = re.search(r'^(?P<height>\d+)[Pp]', format_id) | |
142 | if m: | |
143 | fmt['height'] = int(m.group('height')) | |
144 | formats.append(fmt) | |
145 | self._sort_formats(formats) | |
146 | entry = dict(info) | |
159444a6 | 147 | entry['id'] = '%s_%d' % (entry['id'], num) |
355d074f S |
148 | entry['title'] = '%s part %d' % (entry['title'], num) |
149 | entry['formats'] = formats | |
150 | entries.append(entry) | |
013bfdd8 | 151 | return self.playlist_result(entries, info['id'], info['title']) |
355d074f S |
152 | |
153 | def _extract_info(self, info): | |
154 | return { | |
155 | 'id': info['_id'], | |
369c12e0 | 156 | 'title': info.get('title') or 'Untitled Broadcast', |
7a6e8a1b S |
157 | 'description': info.get('description'), |
158 | 'duration': int_or_none(info.get('length')), | |
159 | 'thumbnail': info.get('preview'), | |
160 | 'uploader': info.get('channel', {}).get('display_name'), | |
161 | 'uploader_id': info.get('channel', {}).get('name'), | |
162 | 'timestamp': parse_iso8601(info.get('recorded_at')), | |
163 | 'view_count': int_or_none(info.get('views')), | |
355d074f S |
164 | } |
165 | ||
c5db6bb3 S |
166 | def _real_extract(self, url): |
167 | return self._extract_media(self._match_id(url)) | |
04d02a9d | 168 | |
04d02a9d | 169 | |
c5db6bb3 S |
170 | class TwitchVideoIE(TwitchItemBaseIE): |
171 | IE_NAME = 'twitch:video' | |
93f78707 | 172 | _VALID_URL = r'%s/[^/]+/b/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE |
c5db6bb3 S |
173 | _ITEM_TYPE = 'video' |
174 | _ITEM_SHORTCUT = 'a' | |
04d02a9d | 175 | |
c5db6bb3 S |
176 | _TEST = { |
177 | 'url': 'http://www.twitch.tv/riotgames/b/577357806', | |
178 | 'info_dict': { | |
179 | 'id': 'a577357806', | |
180 | 'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG', | |
181 | }, | |
182 | 'playlist_mincount': 12, | |
0db3a661 | 183 | 'skip': 'HTTP Error 404: Not Found', |
c5db6bb3 | 184 | } |
04d02a9d | 185 | |
04d02a9d | 186 | |
c5db6bb3 S |
187 | class TwitchChapterIE(TwitchItemBaseIE): |
188 | IE_NAME = 'twitch:chapter' | |
93f78707 | 189 | _VALID_URL = r'%s/[^/]+/c/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE |
c5db6bb3 S |
190 | _ITEM_TYPE = 'chapter' |
191 | _ITEM_SHORTCUT = 'c' | |
04d02a9d | 192 | |
78111136 | 193 | _TESTS = [{ |
c5db6bb3 S |
194 | 'url': 'http://www.twitch.tv/acracingleague/c/5285812', |
195 | 'info_dict': { | |
196 | 'id': 'c5285812', | |
197 | 'title': 'ACRL Off Season - Sports Cars @ Nordschleife', | |
198 | }, | |
199 | 'playlist_mincount': 3, | |
0db3a661 | 200 | 'skip': 'HTTP Error 404: Not Found', |
78111136 PH |
201 | }, { |
202 | 'url': 'http://www.twitch.tv/tsm_theoddone/c/2349361', | |
203 | 'only_matching': True, | |
204 | }] | |
c5db6bb3 S |
205 | |
206 | ||
207 | class TwitchVodIE(TwitchItemBaseIE): | |
208 | IE_NAME = 'twitch:vod' | |
3f1ce168 S |
209 | _VALID_URL = r'''(?x) |
210 | https?:// | |
211 | (?: | |
212 | (?:www\.)?twitch\.tv/[^/]+/v/| | |
213 | player\.twitch\.tv/\?.*?\bvideo=v | |
214 | ) | |
215 | (?P<id>\d+) | |
216 | ''' | |
c5db6bb3 S |
217 | _ITEM_TYPE = 'vod' |
218 | _ITEM_SHORTCUT = 'v' | |
219 | ||
9c724a98 | 220 | _TESTS = [{ |
e704f87f | 221 | 'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s', |
c5db6bb3 | 222 | 'info_dict': { |
ac0474f8 | 223 | 'id': 'v6528877', |
c5db6bb3 | 224 | 'ext': 'mp4', |
ac0474f8 | 225 | 'title': 'LCK Summer Split - Week 6 Day 1', |
ec85ded8 | 226 | 'thumbnail': r're:^https?://.*\.jpg$', |
ac0474f8 YCH |
227 | 'duration': 17208, |
228 | 'timestamp': 1435131709, | |
229 | 'upload_date': '20150624', | |
230 | 'uploader': 'Riot Games', | |
231 | 'uploader_id': 'riotgames', | |
c5db6bb3 | 232 | 'view_count': int, |
e704f87f | 233 | 'start_time': 310, |
c5db6bb3 S |
234 | }, |
235 | 'params': { | |
236 | # m3u8 download | |
237 | 'skip_download': True, | |
238 | }, | |
9c724a98 S |
239 | }, { |
240 | # Untitled broadcast (title is None) | |
241 | 'url': 'http://www.twitch.tv/belkao_o/v/11230755', | |
242 | 'info_dict': { | |
243 | 'id': 'v11230755', | |
244 | 'ext': 'mp4', | |
245 | 'title': 'Untitled Broadcast', | |
ec85ded8 | 246 | 'thumbnail': r're:^https?://.*\.jpg$', |
9c724a98 S |
247 | 'duration': 1638, |
248 | 'timestamp': 1439746708, | |
249 | 'upload_date': '20150816', | |
250 | 'uploader': 'BelkAO_o', | |
251 | 'uploader_id': 'belkao_o', | |
252 | 'view_count': int, | |
253 | }, | |
254 | 'params': { | |
255 | # m3u8 download | |
256 | 'skip_download': True, | |
257 | }, | |
9bd7bd0b | 258 | 'skip': 'HTTP Error 404: Not Found', |
3f1ce168 S |
259 | }, { |
260 | 'url': 'http://player.twitch.tv/?t=5m10s&video=v6528877', | |
261 | 'only_matching': True, | |
9c724a98 | 262 | }] |
04d02a9d | 263 | |
79e93125 | 264 | def _real_extract(self, url): |
c5db6bb3 | 265 | item_id = self._match_id(url) |
e5e99661 | 266 | |
c5db6bb3 | 267 | info = self._download_info(self._ITEM_SHORTCUT, item_id) |
e3f6b569 S |
268 | access_token = self._call_api( |
269 | 'api/vods/%s/access_token' % item_id, item_id, | |
350c9481 | 270 | 'Downloading %s access token' % self._ITEM_TYPE) |
e5e99661 | 271 | |
c5db6bb3 | 272 | formats = self._extract_m3u8_formats( |
350c9481 S |
273 | '%s/vod/%s?%s' % ( |
274 | self._USHER_BASE, item_id, | |
15707c7e | 275 | compat_urllib_parse_urlencode({ |
e5e99661 | 276 | 'allow_source': 'true', |
ac455055 | 277 | 'allow_audio_only': 'true', |
e5e99661 S |
278 | 'allow_spectre': 'true', |
279 | 'player': 'twitchweb', | |
280 | 'nauth': access_token['token'], | |
281 | 'nauthsig': access_token['sig'], | |
282 | })), | |
631d4c87 | 283 | item_id, 'mp4', entry_protocol='m3u8_native') |
e5e99661 | 284 | |
d0e958c7 | 285 | self._prefer_source(formats) |
c5db6bb3 | 286 | info['formats'] = formats |
e704f87f NH |
287 | |
288 | parsed_url = compat_urllib_parse_urlparse(url) | |
289 | query = compat_parse_qs(parsed_url.query) | |
290 | if 't' in query: | |
291 | info['start_time'] = parse_duration(query['t'][0]) | |
292 | ||
264e77c4 S |
293 | if info.get('timestamp') is not None: |
294 | info['subtitles'] = { | |
295 | 'rechat': [{ | |
296 | 'url': update_url_query( | |
297 | 'https://rechat.twitch.tv/rechat-messages', { | |
298 | 'video_id': 'v%s' % item_id, | |
299 | 'start': info['timestamp'], | |
300 | }), | |
301 | 'ext': 'json', | |
302 | }], | |
303 | } | |
304 | ||
c5db6bb3 S |
305 | return info |
306 | ||
307 | ||
308 | class TwitchPlaylistBaseIE(TwitchBaseIE): | |
e3f6b569 | 309 | _PLAYLIST_PATH = 'kraken/channels/%s/videos/?offset=%d&limit=%d' |
ececca6c | 310 | _PAGE_LIMIT = 100 |
c5db6bb3 S |
311 | |
312 | def _extract_playlist(self, channel_id): | |
e3f6b569 S |
313 | info = self._call_api( |
314 | 'kraken/channels/%s' % channel_id, | |
c5db6bb3 S |
315 | channel_id, 'Downloading channel info JSON') |
316 | channel_name = info.get('display_name') or info.get('name') | |
317 | entries = [] | |
318 | offset = 0 | |
319 | limit = self._PAGE_LIMIT | |
539a1641 S |
320 | broken_paging_detected = False |
321 | counter_override = None | |
c5db6bb3 | 322 | for counter in itertools.count(1): |
e3f6b569 S |
323 | response = self._call_api( |
324 | self._PLAYLIST_PATH % (channel_id, offset, limit), | |
539a1641 | 325 | channel_id, |
93753aad | 326 | 'Downloading %s JSON page %s' |
539a1641 | 327 | % (self._PLAYLIST_TYPE, counter_override or counter)) |
c2e64f71 S |
328 | page_entries = self._extract_playlist_page(response) |
329 | if not page_entries: | |
c5db6bb3 | 330 | break |
539a1641 S |
331 | total = int_or_none(response.get('_total')) |
332 | # Since the beginning of March 2016 twitch's paging mechanism | |
333 | # is completely broken on the twitch side. It simply ignores | |
334 | # a limit and returns the whole offset number of videos. | |
335 | # Working around by just requesting all videos at once. | |
dec2cae0 | 336 | # Upd: pagination bug was fixed by twitch on 15.03.2016. |
539a1641 S |
337 | if not broken_paging_detected and total and len(page_entries) > limit: |
338 | self.report_warning( | |
dec2cae0 | 339 | 'Twitch pagination is broken on twitch side, requesting all videos at once', |
539a1641 S |
340 | channel_id) |
341 | broken_paging_detected = True | |
a8276b26 | 342 | offset = total |
539a1641 S |
343 | counter_override = '(all at once)' |
344 | continue | |
c2e64f71 | 345 | entries.extend(page_entries) |
539a1641 S |
346 | if broken_paging_detected or total and len(page_entries) >= total: |
347 | break | |
c5db6bb3 | 348 | offset += limit |
c2e64f71 | 349 | return self.playlist_result( |
8bbb4b56 | 350 | [self.url_result(entry) for entry in orderedSet(entries)], |
c2e64f71 S |
351 | channel_id, channel_name) |
352 | ||
353 | def _extract_playlist_page(self, response): | |
354 | videos = response.get('videos') | |
355 | return [video['url'] for video in videos] if videos else [] | |
c5db6bb3 S |
356 | |
357 | def _real_extract(self, url): | |
358 | return self._extract_playlist(self._match_id(url)) | |
359 | ||
360 | ||
361 | class TwitchProfileIE(TwitchPlaylistBaseIE): | |
362 | IE_NAME = 'twitch:profile' | |
363 | _VALID_URL = r'%s/(?P<id>[^/]+)/profile/?(?:\#.*)?$' % TwitchBaseIE._VALID_URL_BASE | |
364 | _PLAYLIST_TYPE = 'profile' | |
365 | ||
366 | _TEST = { | |
367 | 'url': 'http://www.twitch.tv/vanillatv/profile', | |
368 | 'info_dict': { | |
369 | 'id': 'vanillatv', | |
370 | 'title': 'VanillaTV', | |
371 | }, | |
372 | 'playlist_mincount': 412, | |
373 | } | |
374 | ||
375 | ||
93753aad S |
376 | class TwitchVideosBaseIE(TwitchPlaylistBaseIE): |
377 | _VALID_URL_VIDEOS_BASE = r'%s/(?P<id>[^/]+)/videos' % TwitchBaseIE._VALID_URL_BASE | |
378 | _PLAYLIST_PATH = TwitchPlaylistBaseIE._PLAYLIST_PATH + '&broadcast_type=' | |
379 | ||
380 | ||
381 | class TwitchAllVideosIE(TwitchVideosBaseIE): | |
382 | IE_NAME = 'twitch:videos:all' | |
383 | _VALID_URL = r'%s/all' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE | |
384 | _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive,upload,highlight' | |
385 | _PLAYLIST_TYPE = 'all videos' | |
386 | ||
387 | _TEST = { | |
388 | 'url': 'https://www.twitch.tv/spamfish/videos/all', | |
389 | 'info_dict': { | |
390 | 'id': 'spamfish', | |
391 | 'title': 'Spamfish', | |
392 | }, | |
393 | 'playlist_mincount': 869, | |
394 | } | |
395 | ||
396 | ||
397 | class TwitchUploadsIE(TwitchVideosBaseIE): | |
398 | IE_NAME = 'twitch:videos:uploads' | |
399 | _VALID_URL = r'%s/uploads' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE | |
400 | _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'upload' | |
401 | _PLAYLIST_TYPE = 'uploads' | |
402 | ||
403 | _TEST = { | |
404 | 'url': 'https://www.twitch.tv/spamfish/videos/uploads', | |
405 | 'info_dict': { | |
406 | 'id': 'spamfish', | |
407 | 'title': 'Spamfish', | |
408 | }, | |
409 | 'playlist_mincount': 0, | |
410 | } | |
411 | ||
412 | ||
413 | class TwitchPastBroadcastsIE(TwitchVideosBaseIE): | |
414 | IE_NAME = 'twitch:videos:past-broadcasts' | |
415 | _VALID_URL = r'%s/past-broadcasts' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE | |
416 | _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive' | |
c5db6bb3 S |
417 | _PLAYLIST_TYPE = 'past broadcasts' |
418 | ||
419 | _TEST = { | |
93753aad S |
420 | 'url': 'https://www.twitch.tv/spamfish/videos/past-broadcasts', |
421 | 'info_dict': { | |
422 | 'id': 'spamfish', | |
423 | 'title': 'Spamfish', | |
424 | }, | |
425 | 'playlist_mincount': 0, | |
426 | } | |
427 | ||
428 | ||
429 | class TwitchHighlightsIE(TwitchVideosBaseIE): | |
430 | IE_NAME = 'twitch:videos:highlights' | |
431 | _VALID_URL = r'%s/highlights' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE | |
432 | _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'highlight' | |
433 | _PLAYLIST_TYPE = 'highlights' | |
434 | ||
435 | _TEST = { | |
436 | 'url': 'https://www.twitch.tv/spamfish/videos/highlights', | |
c5db6bb3 S |
437 | 'info_dict': { |
438 | 'id': 'spamfish', | |
439 | 'title': 'Spamfish', | |
440 | }, | |
93753aad | 441 | 'playlist_mincount': 805, |
c5db6bb3 | 442 | } |
240b9b7a S |
443 | |
444 | ||
445 | class TwitchStreamIE(TwitchBaseIE): | |
446 | IE_NAME = 'twitch:stream' | |
faa1f83a | 447 | _VALID_URL = r'%s/(?P<id>[^/#?]+)/?(?:\#.*)?$' % TwitchBaseIE._VALID_URL_BASE |
240b9b7a | 448 | |
faa1f83a | 449 | _TESTS = [{ |
240b9b7a S |
450 | 'url': 'http://www.twitch.tv/shroomztv', |
451 | 'info_dict': { | |
452 | 'id': '12772022048', | |
453 | 'display_id': 'shroomztv', | |
454 | 'ext': 'mp4', | |
455 | 'title': 're:^ShroomzTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', | |
456 | 'description': 'H1Z1 - lonewolfing with ShroomzTV | A3 Battle Royale later - @ShroomzTV', | |
457 | 'is_live': True, | |
458 | 'timestamp': 1421928037, | |
459 | 'upload_date': '20150122', | |
460 | 'uploader': 'ShroomzTV', | |
461 | 'uploader_id': 'shroomztv', | |
462 | 'view_count': int, | |
463 | }, | |
464 | 'params': { | |
465 | # m3u8 download | |
466 | 'skip_download': True, | |
467 | }, | |
faa1f83a S |
468 | }, { |
469 | 'url': 'http://www.twitch.tv/miracle_doto#profile-0', | |
470 | 'only_matching': True, | |
471 | }] | |
240b9b7a S |
472 | |
473 | def _real_extract(self, url): | |
474 | channel_id = self._match_id(url) | |
475 | ||
e3f6b569 | 476 | stream = self._call_api( |
9aa929d3 | 477 | 'kraken/streams/%s?stream_type=all' % channel_id, channel_id, |
240b9b7a S |
478 | 'Downloading stream JSON').get('stream') |
479 | ||
240b9b7a | 480 | if not stream: |
cc764a6d | 481 | raise ExtractorError('%s is offline' % channel_id, expected=True) |
240b9b7a | 482 | |
06966677 S |
483 | # Channel name may be typed if different case than the original channel name |
484 | # (e.g. http://www.twitch.tv/TWITCHPLAYSPOKEMON) that will lead to constructing | |
485 | # an invalid m3u8 URL. Working around by use of original channel name from stream | |
486 | # JSON and fallback to lowercase if it's not available. | |
1793d71d S |
487 | channel_id = stream.get('channel', {}).get('name') or channel_id.lower() |
488 | ||
e3f6b569 S |
489 | access_token = self._call_api( |
490 | 'api/channels/%s/access_token' % channel_id, channel_id, | |
240b9b7a S |
491 | 'Downloading channel access token') |
492 | ||
493 | query = { | |
494 | 'allow_source': 'true', | |
ac455055 | 495 | 'allow_audio_only': 'true', |
9aa929d3 | 496 | 'allow_spectre': 'true', |
f353cbdb | 497 | 'p': random.randint(1000000, 10000000), |
240b9b7a S |
498 | 'player': 'twitchweb', |
499 | 'segment_preference': '4', | |
d34e7949 S |
500 | 'sig': access_token['sig'].encode('utf-8'), |
501 | 'token': access_token['token'].encode('utf-8'), | |
240b9b7a | 502 | } |
240b9b7a S |
503 | formats = self._extract_m3u8_formats( |
504 | '%s/api/channel/hls/%s.m3u8?%s' | |
15707c7e | 505 | % (self._USHER_BASE, channel_id, compat_urllib_parse_urlencode(query)), |
240b9b7a | 506 | channel_id, 'mp4') |
d0e958c7 | 507 | self._prefer_source(formats) |
240b9b7a S |
508 | |
509 | view_count = stream.get('viewers') | |
510 | timestamp = parse_iso8601(stream.get('created_at')) | |
511 | ||
512 | channel = stream['channel'] | |
513 | title = self._live_title(channel.get('display_name') or channel.get('name')) | |
514 | description = channel.get('status') | |
515 | ||
516 | thumbnails = [] | |
517 | for thumbnail_key, thumbnail_url in stream['preview'].items(): | |
518 | m = re.search(r'(?P<width>\d+)x(?P<height>\d+)\.jpg$', thumbnail_key) | |
519 | if not m: | |
520 | continue | |
521 | thumbnails.append({ | |
522 | 'url': thumbnail_url, | |
523 | 'width': int(m.group('width')), | |
524 | 'height': int(m.group('height')), | |
525 | }) | |
526 | ||
527 | return { | |
528 | 'id': compat_str(stream['_id']), | |
529 | 'display_id': channel_id, | |
530 | 'title': title, | |
531 | 'description': description, | |
532 | 'thumbnails': thumbnails, | |
533 | 'uploader': channel.get('display_name'), | |
534 | 'uploader_id': channel.get('name'), | |
535 | 'timestamp': timestamp, | |
536 | 'view_count': view_count, | |
537 | 'formats': formats, | |
538 | 'is_live': True, | |
12d1fb5a | 539 | } |
778f9694 S |
540 | |
541 | ||
542 | class TwitchClipsIE(InfoExtractor): | |
543 | IE_NAME = 'twitch:clips' | |
544 | _VALID_URL = r'https?://clips\.twitch\.tv/(?:[^/]+/)*(?P<id>[^/?#&]+)' | |
545 | ||
74ba450a | 546 | _TESTS = [{ |
778f9694 S |
547 | 'url': 'https://clips.twitch.tv/ea/AggressiveCobraPoooound', |
548 | 'md5': '761769e1eafce0ffebfb4089cb3847cd', | |
549 | 'info_dict': { | |
550 | 'id': 'AggressiveCobraPoooound', | |
551 | 'ext': 'mp4', | |
552 | 'title': 'EA Play 2016 Live from the Novo Theatre', | |
ec85ded8 | 553 | 'thumbnail': r're:^https?://.*\.jpg', |
778f9694 S |
554 | 'creator': 'EA', |
555 | 'uploader': 'stereotype_', | |
556 | 'uploader_id': 'stereotype_', | |
557 | }, | |
74ba450a S |
558 | }, { |
559 | # multiple formats | |
560 | 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy', | |
561 | 'only_matching': True, | |
562 | }] | |
778f9694 S |
563 | |
564 | def _real_extract(self, url): | |
565 | video_id = self._match_id(url) | |
566 | ||
567 | webpage = self._download_webpage(url, video_id) | |
568 | ||
569 | clip = self._parse_json( | |
570 | self._search_regex( | |
571 | r'(?s)clipInfo\s*=\s*({.+?});', webpage, 'clip info'), | |
572 | video_id, transform_source=js_to_json) | |
573 | ||
74ba450a S |
574 | title = clip.get('channel_title') or self._og_search_title(webpage) |
575 | ||
576 | formats = [{ | |
577 | 'url': option['source'], | |
578 | 'format_id': option.get('quality'), | |
579 | 'height': int_or_none(option.get('quality')), | |
580 | } for option in clip.get('quality_options', []) if option.get('source')] | |
581 | ||
582 | if not formats: | |
583 | formats = [{ | |
584 | 'url': clip['clip_video_url'], | |
585 | }] | |
778f9694 | 586 | |
0cacae28 S |
587 | self._sort_formats(formats) |
588 | ||
778f9694 S |
589 | return { |
590 | 'id': video_id, | |
778f9694 S |
591 | 'title': title, |
592 | 'thumbnail': self._og_search_thumbnail(webpage), | |
593 | 'creator': clip.get('broadcaster_display_name') or clip.get('broadcaster_login'), | |
594 | 'uploader': clip.get('curator_login'), | |
595 | 'uploader_id': clip.get('curator_display_name'), | |
74ba450a | 596 | 'formats': formats, |
778f9694 | 597 | } |