2 from __future__ import unicode_literals
8 from .common import InfoExtractor
13 compat_urllib_parse_urlencode,
14 compat_urllib_parse_urlparse,
30 class TwitchBaseIE(InfoExtractor):
31 _VALID_URL_BASE = r'https?://(?:www\.)?twitch\.tv'
33 _API_BASE = 'https://api.twitch.tv'
34 _USHER_BASE = 'https://usher.ttvnw.net'
35 _LOGIN_URL = 'https://www.twitch.tv/login'
36 _CLIENT_ID = 'jzkbprff40iqj646a697cyrvl0zt2m6'
37 _NETRC_MACHINE = 'twitch'
39 def _handle_error(self, response):
40 if not isinstance(response, dict):
42 error = response.get('error')
45 '%s returned error: %s - %s' % (self.IE_NAME, error, response.get('message')),
48 def _call_api(self, path, item_id, note):
49 response = self._download_json(
50 '%s/%s' % (self._API_BASE, path), item_id, note,
51 headers={'Client-ID': self._CLIENT_ID})
52 self._handle_error(response)
55 def _real_initialize(self):
59 (username, password) = self._get_login_info()
65 'Unable to login. Twitch said: %s' % message, expected=True)
67 def post_login_form(page, urlh, note, data):
68 form = self._hidden_inputs(page)
71 page_url = urlh.geturl()
72 post_url = self._search_regex(
73 r'<form[^>]+action=(["\'])(?P<url>.+?)\1', page,
74 'post url', default=page_url, group='url')
75 post_url = urljoin(page_url, post_url)
77 headers = {'Referer': page_url}
80 response = self._download_json(
82 data=urlencode_postdata(form),
84 except ExtractorError as e:
85 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 400:
86 response = self._parse_json(
87 e.cause.read().decode('utf-8'), None)
88 fail(response['message'])
91 if response.get('redirect'):
92 redirect_url = urljoin(post_url, response['redirect'])
93 return self._download_webpage_handle(
94 redirect_url, None, 'Downloading login redirect page',
97 login_page, handle = self._download_webpage_handle(
98 self._LOGIN_URL, None, 'Downloading login page')
100 # Some TOR nodes and public proxies are blocked completely
101 if 'blacklist_message' in login_page:
102 fail(clean_html(login_page))
105 'username': username,
106 'password': password,
108 redirect_res = post_login_form(
109 login_page, handle, 'Logging in as %s' % username, login_data)
113 redirect_page, handle = redirect_res
115 if re.search(r'(?i)<form[^>]+id="two-factor-submit"', redirect_page) is not None:
116 # TODO: Add mechanism to request an SMS or phone call
117 tfa_token = self._get_tfa_info('two-factor authentication token')
119 'authy_token': tfa_token,
120 'remember_2fa': 'true',
122 post_login_form(redirect_page, handle, 'Submitting TFA token', tfa_data)
124 def _prefer_source(self, formats):
126 source = next(f for f in formats if f['format_id'] == 'Source')
127 source['preference'] = 10
128 except StopIteration:
129 pass # No Source stream present
130 self._sort_formats(formats)
133 class TwitchItemBaseIE(TwitchBaseIE):
134 def _download_info(self, item, item_id):
135 return self._extract_info(self._call_api(
136 'kraken/videos/%s%s' % (item, item_id), item_id,
137 'Downloading %s info JSON' % self._ITEM_TYPE))
139 def _extract_media(self, item_id):
140 info = self._download_info(self._ITEM_SHORTCUT, item_id)
141 response = self._call_api(
142 'api/videos/%s%s' % (self._ITEM_SHORTCUT, item_id), item_id,
143 'Downloading %s playlist JSON' % self._ITEM_TYPE)
145 chunks = response['chunks']
146 qualities = list(chunks.keys())
147 for num, fragment in enumerate(zip(*chunks.values()), start=1):
149 for fmt_num, fragment_fmt in enumerate(fragment):
150 format_id = qualities[fmt_num]
152 'url': fragment_fmt['url'],
153 'format_id': format_id,
154 'quality': 1 if format_id == 'live' else 0,
156 m = re.search(r'^(?P<height>\d+)[Pp]', format_id)
158 fmt['height'] = int(m.group('height'))
160 self._sort_formats(formats)
162 entry['id'] = '%s_%d' % (entry['id'], num)
163 entry['title'] = '%s part %d' % (entry['title'], num)
164 entry['formats'] = formats
165 entries.append(entry)
166 return self.playlist_result(entries, info['id'], info['title'])
168 def _extract_info(self, info):
171 'title': info.get('title') or 'Untitled Broadcast',
172 'description': info.get('description'),
173 'duration': int_or_none(info.get('length')),
174 'thumbnail': info.get('preview'),
175 'uploader': info.get('channel', {}).get('display_name'),
176 'uploader_id': info.get('channel', {}).get('name'),
177 'timestamp': parse_iso8601(info.get('recorded_at')),
178 'view_count': int_or_none(info.get('views')),
181 def _real_extract(self, url):
182 return self._extract_media(self._match_id(url))
185 class TwitchVideoIE(TwitchItemBaseIE):
186 IE_NAME = 'twitch:video'
187 _VALID_URL = r'%s/[^/]+/b/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
192 'url': 'http://www.twitch.tv/riotgames/b/577357806',
195 'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG',
197 'playlist_mincount': 12,
198 'skip': 'HTTP Error 404: Not Found',
202 class TwitchChapterIE(TwitchItemBaseIE):
203 IE_NAME = 'twitch:chapter'
204 _VALID_URL = r'%s/[^/]+/c/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
205 _ITEM_TYPE = 'chapter'
209 'url': 'http://www.twitch.tv/acracingleague/c/5285812',
212 'title': 'ACRL Off Season - Sports Cars @ Nordschleife',
214 'playlist_mincount': 3,
215 'skip': 'HTTP Error 404: Not Found',
217 'url': 'http://www.twitch.tv/tsm_theoddone/c/2349361',
218 'only_matching': True,
222 class TwitchVodIE(TwitchItemBaseIE):
223 IE_NAME = 'twitch:vod'
224 _VALID_URL = r'''(?x)
227 (?:www\.)?twitch\.tv/(?:[^/]+/v|videos)/|
228 player\.twitch\.tv/\?.*?\bvideo=v
236 'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s',
240 'title': 'LCK Summer Split - Week 6 Day 1',
241 'thumbnail': r're:^https?://.*\.jpg$',
243 'timestamp': 1435131709,
244 'upload_date': '20150624',
245 'uploader': 'Riot Games',
246 'uploader_id': 'riotgames',
252 'skip_download': True,
255 # Untitled broadcast (title is None)
256 'url': 'http://www.twitch.tv/belkao_o/v/11230755',
260 'title': 'Untitled Broadcast',
261 'thumbnail': r're:^https?://.*\.jpg$',
263 'timestamp': 1439746708,
264 'upload_date': '20150816',
265 'uploader': 'BelkAO_o',
266 'uploader_id': 'belkao_o',
271 'skip_download': True,
273 'skip': 'HTTP Error 404: Not Found',
275 'url': 'http://player.twitch.tv/?t=5m10s&video=v6528877',
276 'only_matching': True,
278 'url': 'https://www.twitch.tv/videos/6528877',
279 'only_matching': True,
282 def _real_extract(self, url):
283 item_id = self._match_id(url)
285 info = self._download_info(self._ITEM_SHORTCUT, item_id)
286 access_token = self._call_api(
287 'api/vods/%s/access_token' % item_id, item_id,
288 'Downloading %s access token' % self._ITEM_TYPE)
290 formats = self._extract_m3u8_formats(
292 self._USHER_BASE, item_id,
293 compat_urllib_parse_urlencode({
294 'allow_source': 'true',
295 'allow_audio_only': 'true',
296 'allow_spectre': 'true',
297 'player': 'twitchweb',
298 'nauth': access_token['token'],
299 'nauthsig': access_token['sig'],
301 item_id, 'mp4', entry_protocol='m3u8_native')
303 self._prefer_source(formats)
304 info['formats'] = formats
306 parsed_url = compat_urllib_parse_urlparse(url)
307 query = compat_parse_qs(parsed_url.query)
309 info['start_time'] = parse_duration(query['t'][0])
311 if info.get('timestamp') is not None:
312 info['subtitles'] = {
314 'url': update_url_query(
315 'https://rechat.twitch.tv/rechat-messages', {
316 'video_id': 'v%s' % item_id,
317 'start': info['timestamp'],
326 class TwitchPlaylistBaseIE(TwitchBaseIE):
327 _PLAYLIST_PATH = 'kraken/channels/%s/videos/?offset=%d&limit=%d'
330 def _extract_playlist(self, channel_id):
331 info = self._call_api(
332 'kraken/channels/%s' % channel_id,
333 channel_id, 'Downloading channel info JSON')
334 channel_name = info.get('display_name') or info.get('name')
337 limit = self._PAGE_LIMIT
338 broken_paging_detected = False
339 counter_override = None
340 for counter in itertools.count(1):
341 response = self._call_api(
342 self._PLAYLIST_PATH % (channel_id, offset, limit),
344 'Downloading %s JSON page %s'
345 % (self._PLAYLIST_TYPE, counter_override or counter))
346 page_entries = self._extract_playlist_page(response)
349 total = int_or_none(response.get('_total'))
350 # Since the beginning of March 2016 twitch's paging mechanism
351 # is completely broken on the twitch side. It simply ignores
352 # a limit and returns the whole offset number of videos.
353 # Working around by just requesting all videos at once.
354 # Upd: pagination bug was fixed by twitch on 15.03.2016.
355 if not broken_paging_detected and total and len(page_entries) > limit:
357 'Twitch pagination is broken on twitch side, requesting all videos at once',
359 broken_paging_detected = True
361 counter_override = '(all at once)'
363 entries.extend(page_entries)
364 if broken_paging_detected or total and len(page_entries) >= total:
367 return self.playlist_result(
368 [self.url_result(entry) for entry in orderedSet(entries)],
369 channel_id, channel_name)
371 def _extract_playlist_page(self, response):
372 videos = response.get('videos')
373 return [video['url'] for video in videos] if videos else []
375 def _real_extract(self, url):
376 return self._extract_playlist(self._match_id(url))
379 class TwitchProfileIE(TwitchPlaylistBaseIE):
380 IE_NAME = 'twitch:profile'
381 _VALID_URL = r'%s/(?P<id>[^/]+)/profile/?(?:\#.*)?$' % TwitchBaseIE._VALID_URL_BASE
382 _PLAYLIST_TYPE = 'profile'
385 'url': 'http://www.twitch.tv/vanillatv/profile',
388 'title': 'VanillaTV',
390 'playlist_mincount': 412,
394 class TwitchVideosBaseIE(TwitchPlaylistBaseIE):
395 _VALID_URL_VIDEOS_BASE = r'%s/(?P<id>[^/]+)/videos' % TwitchBaseIE._VALID_URL_BASE
396 _PLAYLIST_PATH = TwitchPlaylistBaseIE._PLAYLIST_PATH + '&broadcast_type='
399 class TwitchAllVideosIE(TwitchVideosBaseIE):
400 IE_NAME = 'twitch:videos:all'
401 _VALID_URL = r'%s/all' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
402 _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive,upload,highlight'
403 _PLAYLIST_TYPE = 'all videos'
406 'url': 'https://www.twitch.tv/spamfish/videos/all',
411 'playlist_mincount': 869,
415 class TwitchUploadsIE(TwitchVideosBaseIE):
416 IE_NAME = 'twitch:videos:uploads'
417 _VALID_URL = r'%s/uploads' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
418 _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'upload'
419 _PLAYLIST_TYPE = 'uploads'
422 'url': 'https://www.twitch.tv/spamfish/videos/uploads',
427 'playlist_mincount': 0,
431 class TwitchPastBroadcastsIE(TwitchVideosBaseIE):
432 IE_NAME = 'twitch:videos:past-broadcasts'
433 _VALID_URL = r'%s/past-broadcasts' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
434 _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'archive'
435 _PLAYLIST_TYPE = 'past broadcasts'
438 'url': 'https://www.twitch.tv/spamfish/videos/past-broadcasts',
443 'playlist_mincount': 0,
447 class TwitchHighlightsIE(TwitchVideosBaseIE):
448 IE_NAME = 'twitch:videos:highlights'
449 _VALID_URL = r'%s/highlights' % TwitchVideosBaseIE._VALID_URL_VIDEOS_BASE
450 _PLAYLIST_PATH = TwitchVideosBaseIE._PLAYLIST_PATH + 'highlight'
451 _PLAYLIST_TYPE = 'highlights'
454 'url': 'https://www.twitch.tv/spamfish/videos/highlights',
459 'playlist_mincount': 805,
463 class TwitchStreamIE(TwitchBaseIE):
464 IE_NAME = 'twitch:stream'
465 _VALID_URL = r'''(?x)
468 (?:www\.)?twitch\.tv/|
469 player\.twitch\.tv/\?.*?\bchannel=
475 'url': 'http://www.twitch.tv/shroomztv',
478 'display_id': 'shroomztv',
480 'title': 're:^ShroomzTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
481 'description': 'H1Z1 - lonewolfing with ShroomzTV | A3 Battle Royale later - @ShroomzTV',
483 'timestamp': 1421928037,
484 'upload_date': '20150122',
485 'uploader': 'ShroomzTV',
486 'uploader_id': 'shroomztv',
491 'skip_download': True,
494 'url': 'http://www.twitch.tv/miracle_doto#profile-0',
495 'only_matching': True,
497 'url': 'https://player.twitch.tv/?channel=lotsofs',
498 'only_matching': True,
502 def suitable(cls, url):
504 if any(ie.suitable(url) for ie in (
511 TwitchPastBroadcastsIE,
513 else super(TwitchStreamIE, cls).suitable(url))
515 def _real_extract(self, url):
516 channel_id = self._match_id(url)
518 stream = self._call_api(
519 'kraken/streams/%s?stream_type=all' % channel_id, channel_id,
520 'Downloading stream JSON').get('stream')
523 raise ExtractorError('%s is offline' % channel_id, expected=True)
525 # Channel name may be typed if different case than the original channel name
526 # (e.g. http://www.twitch.tv/TWITCHPLAYSPOKEMON) that will lead to constructing
527 # an invalid m3u8 URL. Working around by use of original channel name from stream
528 # JSON and fallback to lowercase if it's not available.
529 channel_id = stream.get('channel', {}).get('name') or channel_id.lower()
531 access_token = self._call_api(
532 'api/channels/%s/access_token' % channel_id, channel_id,
533 'Downloading channel access token')
536 'allow_source': 'true',
537 'allow_audio_only': 'true',
538 'allow_spectre': 'true',
539 'p': random.randint(1000000, 10000000),
540 'player': 'twitchweb',
541 'segment_preference': '4',
542 'sig': access_token['sig'].encode('utf-8'),
543 'token': access_token['token'].encode('utf-8'),
545 formats = self._extract_m3u8_formats(
546 '%s/api/channel/hls/%s.m3u8?%s'
547 % (self._USHER_BASE, channel_id, compat_urllib_parse_urlencode(query)),
549 self._prefer_source(formats)
551 view_count = stream.get('viewers')
552 timestamp = parse_iso8601(stream.get('created_at'))
554 channel = stream['channel']
555 title = self._live_title(channel.get('display_name') or channel.get('name'))
556 description = channel.get('status')
559 for thumbnail_key, thumbnail_url in stream['preview'].items():
560 m = re.search(r'(?P<width>\d+)x(?P<height>\d+)\.jpg$', thumbnail_key)
564 'url': thumbnail_url,
565 'width': int(m.group('width')),
566 'height': int(m.group('height')),
570 'id': compat_str(stream['_id']),
571 'display_id': channel_id,
573 'description': description,
574 'thumbnails': thumbnails,
575 'uploader': channel.get('display_name'),
576 'uploader_id': channel.get('name'),
577 'timestamp': timestamp,
578 'view_count': view_count,
584 class TwitchClipsIE(InfoExtractor):
585 IE_NAME = 'twitch:clips'
586 _VALID_URL = r'https?://clips\.twitch\.tv/(?:[^/]+/)*(?P<id>[^/?#&]+)'
589 'url': 'https://clips.twitch.tv/ea/AggressiveCobraPoooound',
590 'md5': '761769e1eafce0ffebfb4089cb3847cd',
592 'id': 'AggressiveCobraPoooound',
594 'title': 'EA Play 2016 Live from the Novo Theatre',
595 'thumbnail': r're:^https?://.*\.jpg',
597 'uploader': 'stereotype_',
598 'uploader_id': 'stereotype_',
602 'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy',
603 'only_matching': True,
606 def _real_extract(self, url):
607 video_id = self._match_id(url)
609 webpage = self._download_webpage(url, video_id)
611 clip = self._parse_json(
613 r'(?s)clipInfo\s*=\s*({.+?});', webpage, 'clip info'),
614 video_id, transform_source=js_to_json)
616 title = clip.get('channel_title') or self._og_search_title(webpage)
619 'url': option['source'],
620 'format_id': option.get('quality'),
621 'height': int_or_none(option.get('quality')),
622 } for option in clip.get('quality_options', []) if option.get('source')]
626 'url': clip['clip_video_url'],
629 self._sort_formats(formats)
634 'thumbnail': self._og_search_thumbnail(webpage),
635 'creator': clip.get('broadcaster_display_name') or clip.get('broadcaster_login'),
636 'uploader': clip.get('curator_login'),
637 'uploader_id': clip.get('curator_display_name'),