Merge branch 'weibo' of https://github.com/sprhawk/youtube-dl into sprhawk-weibo
[youtube-dl] / youtube_dl / extractor / twitch.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import itertools
5 import re
6 import random
7
8 from .common import InfoExtractor
9 from ..compat import (
10     compat_HTTPError,
11     compat_parse_qs,
12     compat_str,
13     compat_urllib_parse_urlencode,
14     compat_urllib_parse_urlparse,
15 )
16 from ..utils import (
17     clean_html,
18     ExtractorError,
19     int_or_none,
20     js_to_json,
21     orderedSet,
22     parse_duration,
23     parse_iso8601,
24     update_url_query,
25     urlencode_postdata,
26     urljoin,
27 )
28
29
30 class TwitchBaseIE(InfoExtractor):
31     _VALID_URL_BASE = r'https?://(?:(?:www|go)\.)?twitch\.tv'
32
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'
38
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
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)
53         return response
54
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
63         def fail(message):
64             raise ExtractorError(
65                 'Unable to login. Twitch said: %s' % message, expected=True)
66
67         def login_step(page, urlh, note, data):
68             form = self._hidden_inputs(page)
69             form.update(data)
70
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)
76
77             headers = {'Referer': page_url}
78
79             try:
80                 response = self._download_json(
81                     post_url, None, note,
82                     data=urlencode_postdata(form),
83                     headers=headers)
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'])
89                 raise
90
91             redirect_url = urljoin(post_url, response['redirect'])
92             return self._download_webpage_handle(
93                 redirect_url, None, 'Downloading login redirect page',
94                 headers=headers)
95
96         login_page, handle = self._download_webpage_handle(
97             self._LOGIN_URL, None, 'Downloading login page')
98
99         # Some TOR nodes and public proxies are blocked completely
100         if 'blacklist_message' in login_page:
101             fail(clean_html(login_page))
102
103         redirect_page, handle = login_step(
104             login_page, handle, 'Logging in', {
105                 'username': username,
106                 'password': password,
107             })
108
109         if re.search(r'(?i)<form[^>]+id="two-factor-submit"', redirect_page) is not None:
110             # TODO: Add mechanism to request an SMS or phone call
111             tfa_token = self._get_tfa_info('two-factor authentication token')
112             login_step(redirect_page, handle, 'Submitting TFA token', {
113                 'authy_token': tfa_token,
114                 'remember_2fa': 'true',
115             })
116
117     def _prefer_source(self, formats):
118         try:
119             source = next(f for f in formats if f['format_id'] == 'Source')
120             source['preference'] = 10
121         except StopIteration:
122             pass  # No Source stream present
123         self._sort_formats(formats)
124
125
126 class TwitchItemBaseIE(TwitchBaseIE):
127     def _download_info(self, item, item_id):
128         return self._extract_info(self._call_api(
129             'kraken/videos/%s%s' % (item, item_id), item_id,
130             'Downloading %s info JSON' % self._ITEM_TYPE))
131
132     def _extract_media(self, item_id):
133         info = self._download_info(self._ITEM_SHORTCUT, item_id)
134         response = self._call_api(
135             'api/videos/%s%s' % (self._ITEM_SHORTCUT, item_id), item_id,
136             'Downloading %s playlist JSON' % self._ITEM_TYPE)
137         entries = []
138         chunks = response['chunks']
139         qualities = list(chunks.keys())
140         for num, fragment in enumerate(zip(*chunks.values()), start=1):
141             formats = []
142             for fmt_num, fragment_fmt in enumerate(fragment):
143                 format_id = qualities[fmt_num]
144                 fmt = {
145                     'url': fragment_fmt['url'],
146                     'format_id': format_id,
147                     'quality': 1 if format_id == 'live' else 0,
148                 }
149                 m = re.search(r'^(?P<height>\d+)[Pp]', format_id)
150                 if m:
151                     fmt['height'] = int(m.group('height'))
152                 formats.append(fmt)
153             self._sort_formats(formats)
154             entry = dict(info)
155             entry['id'] = '%s_%d' % (entry['id'], num)
156             entry['title'] = '%s part %d' % (entry['title'], num)
157             entry['formats'] = formats
158             entries.append(entry)
159         return self.playlist_result(entries, info['id'], info['title'])
160
161     def _extract_info(self, info):
162         return {
163             'id': info['_id'],
164             'title': info.get('title') or 'Untitled Broadcast',
165             'description': info.get('description'),
166             'duration': int_or_none(info.get('length')),
167             'thumbnail': info.get('preview'),
168             'uploader': info.get('channel', {}).get('display_name'),
169             'uploader_id': info.get('channel', {}).get('name'),
170             'timestamp': parse_iso8601(info.get('recorded_at')),
171             'view_count': int_or_none(info.get('views')),
172         }
173
174     def _real_extract(self, url):
175         return self._extract_media(self._match_id(url))
176
177
178 class TwitchVideoIE(TwitchItemBaseIE):
179     IE_NAME = 'twitch:video'
180     _VALID_URL = r'%s/[^/]+/b/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
181     _ITEM_TYPE = 'video'
182     _ITEM_SHORTCUT = 'a'
183
184     _TEST = {
185         'url': 'http://www.twitch.tv/riotgames/b/577357806',
186         'info_dict': {
187             'id': 'a577357806',
188             'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG',
189         },
190         'playlist_mincount': 12,
191         'skip': 'HTTP Error 404: Not Found',
192     }
193
194
195 class TwitchChapterIE(TwitchItemBaseIE):
196     IE_NAME = 'twitch:chapter'
197     _VALID_URL = r'%s/[^/]+/c/(?P<id>\d+)' % TwitchBaseIE._VALID_URL_BASE
198     _ITEM_TYPE = 'chapter'
199     _ITEM_SHORTCUT = 'c'
200
201     _TESTS = [{
202         'url': 'http://www.twitch.tv/acracingleague/c/5285812',
203         'info_dict': {
204             'id': 'c5285812',
205             'title': 'ACRL Off Season - Sports Cars @ Nordschleife',
206         },
207         'playlist_mincount': 3,
208         'skip': 'HTTP Error 404: Not Found',
209     }, {
210         'url': 'http://www.twitch.tv/tsm_theoddone/c/2349361',
211         'only_matching': True,
212     }]
213
214
215 class TwitchVodIE(TwitchItemBaseIE):
216     IE_NAME = 'twitch:vod'
217     _VALID_URL = r'''(?x)
218                     https?://
219                         (?:
220                             (?:(?:www|go)\.)?twitch\.tv/(?:[^/]+/v|videos)/|
221                             player\.twitch\.tv/\?.*?\bvideo=v
222                         )
223                         (?P<id>\d+)
224                     '''
225     _ITEM_TYPE = 'vod'
226     _ITEM_SHORTCUT = 'v'
227
228     _TESTS = [{
229         'url': 'http://www.twitch.tv/riotgames/v/6528877?t=5m10s',
230         'info_dict': {
231             'id': 'v6528877',
232             'ext': 'mp4',
233             'title': 'LCK Summer Split - Week 6 Day 1',
234             'thumbnail': r're:^https?://.*\.jpg$',
235             'duration': 17208,
236             'timestamp': 1435131709,
237             'upload_date': '20150624',
238             'uploader': 'Riot Games',
239             'uploader_id': 'riotgames',
240             'view_count': int,
241             'start_time': 310,
242         },
243         'params': {
244             # m3u8 download
245             'skip_download': True,
246         },
247     }, {
248         # Untitled broadcast (title is None)
249         'url': 'http://www.twitch.tv/belkao_o/v/11230755',
250         'info_dict': {
251             'id': 'v11230755',
252             'ext': 'mp4',
253             'title': 'Untitled Broadcast',
254             'thumbnail': r're:^https?://.*\.jpg$',
255             'duration': 1638,
256             'timestamp': 1439746708,
257             'upload_date': '20150816',
258             'uploader': 'BelkAO_o',
259             'uploader_id': 'belkao_o',
260             'view_count': int,
261         },
262         'params': {
263             # m3u8 download
264             'skip_download': True,
265         },
266         'skip': 'HTTP Error 404: Not Found',
267     }, {
268         'url': 'http://player.twitch.tv/?t=5m10s&video=v6528877',
269         'only_matching': True,
270     }, {
271         'url': 'https://www.twitch.tv/videos/6528877',
272         'only_matching': True,
273     }]
274
275     def _real_extract(self, url):
276         item_id = self._match_id(url)
277
278         info = self._download_info(self._ITEM_SHORTCUT, item_id)
279         access_token = self._call_api(
280             'api/vods/%s/access_token' % item_id, item_id,
281             'Downloading %s access token' % self._ITEM_TYPE)
282
283         formats = self._extract_m3u8_formats(
284             '%s/vod/%s?%s' % (
285                 self._USHER_BASE, item_id,
286                 compat_urllib_parse_urlencode({
287                     'allow_source': 'true',
288                     'allow_audio_only': 'true',
289                     'allow_spectre': 'true',
290                     'player': 'twitchweb',
291                     'nauth': access_token['token'],
292                     'nauthsig': access_token['sig'],
293                 })),
294             item_id, 'mp4', entry_protocol='m3u8_native')
295
296         self._prefer_source(formats)
297         info['formats'] = formats
298
299         parsed_url = compat_urllib_parse_urlparse(url)
300         query = compat_parse_qs(parsed_url.query)
301         if 't' in query:
302             info['start_time'] = parse_duration(query['t'][0])
303
304         if info.get('timestamp') is not None:
305             info['subtitles'] = {
306                 'rechat': [{
307                     'url': update_url_query(
308                         'https://rechat.twitch.tv/rechat-messages', {
309                             'video_id': 'v%s' % item_id,
310                             'start': info['timestamp'],
311                         }),
312                     'ext': 'json',
313                 }],
314             }
315
316         return info
317
318
319 class TwitchPlaylistBaseIE(TwitchBaseIE):
320     _PLAYLIST_PATH = 'kraken/channels/%s/videos/?offset=%d&limit=%d'
321     _PAGE_LIMIT = 100
322
323     def _extract_playlist(self, channel_id):
324         info = self._call_api(
325             'kraken/channels/%s' % channel_id,
326             channel_id, 'Downloading channel info JSON')
327         channel_name = info.get('display_name') or info.get('name')
328         entries = []
329         offset = 0
330         limit = self._PAGE_LIMIT
331         broken_paging_detected = False
332         counter_override = None
333         for counter in itertools.count(1):
334             response = self._call_api(
335                 self._PLAYLIST_PATH % (channel_id, offset, limit),
336                 channel_id,
337                 'Downloading %s JSON page %s'
338                 % (self._PLAYLIST_TYPE, counter_override or counter))
339             page_entries = self._extract_playlist_page(response)
340             if not page_entries:
341                 break
342             total = int_or_none(response.get('_total'))
343             # Since the beginning of March 2016 twitch's paging mechanism
344             # is completely broken on the twitch side. It simply ignores
345             # a limit and returns the whole offset number of videos.
346             # Working around by just requesting all videos at once.
347             # Upd: pagination bug was fixed by twitch on 15.03.2016.
348             if not broken_paging_detected and total and len(page_entries) > limit:
349                 self.report_warning(
350                     'Twitch pagination is broken on twitch side, requesting all videos at once',
351                     channel_id)
352                 broken_paging_detected = True
353                 offset = total
354                 counter_override = '(all at once)'
355                 continue
356             entries.extend(page_entries)
357             if broken_paging_detected or total and len(page_entries) >= total:
358                 break
359             offset += limit
360         return self.playlist_result(
361             [self._make_url_result(entry) for entry in orderedSet(entries)],
362             channel_id, channel_name)
363
364     def _make_url_result(self, url):
365         try:
366             video_id = 'v%s' % TwitchVodIE._match_id(url)
367             return self.url_result(url, TwitchVodIE.ie_key(), video_id=video_id)
368         except AssertionError:
369             return self.url_result(url)
370
371     def _extract_playlist_page(self, response):
372         videos = response.get('videos')
373         return [video['url'] for video in videos] if videos else []
374
375     def _real_extract(self, url):
376         return self._extract_playlist(self._match_id(url))
377
378
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'
383
384     _TEST = {
385         'url': 'http://www.twitch.tv/vanillatv/profile',
386         'info_dict': {
387             'id': 'vanillatv',
388             'title': 'VanillaTV',
389         },
390         'playlist_mincount': 412,
391     }
392
393
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='
397
398
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'
404
405     _TEST = {
406         'url': 'https://www.twitch.tv/spamfish/videos/all',
407         'info_dict': {
408             'id': 'spamfish',
409             'title': 'Spamfish',
410         },
411         'playlist_mincount': 869,
412     }
413
414
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'
420
421     _TEST = {
422         'url': 'https://www.twitch.tv/spamfish/videos/uploads',
423         'info_dict': {
424             'id': 'spamfish',
425             'title': 'Spamfish',
426         },
427         'playlist_mincount': 0,
428     }
429
430
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'
436
437     _TEST = {
438         'url': 'https://www.twitch.tv/spamfish/videos/past-broadcasts',
439         'info_dict': {
440             'id': 'spamfish',
441             'title': 'Spamfish',
442         },
443         'playlist_mincount': 0,
444     }
445
446
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'
452
453     _TEST = {
454         'url': 'https://www.twitch.tv/spamfish/videos/highlights',
455         'info_dict': {
456             'id': 'spamfish',
457             'title': 'Spamfish',
458         },
459         'playlist_mincount': 805,
460     }
461
462
463 class TwitchStreamIE(TwitchBaseIE):
464     IE_NAME = 'twitch:stream'
465     _VALID_URL = r'''(?x)
466                     https?://
467                         (?:
468                             (?:(?:www|go)\.)?twitch\.tv/|
469                             player\.twitch\.tv/\?.*?\bchannel=
470                         )
471                         (?P<id>[^/#?]+)
472                     '''
473
474     _TESTS = [{
475         'url': 'http://www.twitch.tv/shroomztv',
476         'info_dict': {
477             'id': '12772022048',
478             'display_id': 'shroomztv',
479             'ext': 'mp4',
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',
482             'is_live': True,
483             'timestamp': 1421928037,
484             'upload_date': '20150122',
485             'uploader': 'ShroomzTV',
486             'uploader_id': 'shroomztv',
487             'view_count': int,
488         },
489         'params': {
490             # m3u8 download
491             'skip_download': True,
492         },
493     }, {
494         'url': 'http://www.twitch.tv/miracle_doto#profile-0',
495         'only_matching': True,
496     }, {
497         'url': 'https://player.twitch.tv/?channel=lotsofs',
498         'only_matching': True,
499     }, {
500         'url': 'https://go.twitch.tv/food',
501         'only_matching': True,
502     }]
503
504     @classmethod
505     def suitable(cls, url):
506         return (False
507                 if any(ie.suitable(url) for ie in (
508                     TwitchVideoIE,
509                     TwitchChapterIE,
510                     TwitchVodIE,
511                     TwitchProfileIE,
512                     TwitchAllVideosIE,
513                     TwitchUploadsIE,
514                     TwitchPastBroadcastsIE,
515                     TwitchHighlightsIE))
516                 else super(TwitchStreamIE, cls).suitable(url))
517
518     def _real_extract(self, url):
519         channel_id = self._match_id(url)
520
521         stream = self._call_api(
522             'kraken/streams/%s?stream_type=all' % channel_id, channel_id,
523             'Downloading stream JSON').get('stream')
524
525         if not stream:
526             raise ExtractorError('%s is offline' % channel_id, expected=True)
527
528         # Channel name may be typed if different case than the original channel name
529         # (e.g. http://www.twitch.tv/TWITCHPLAYSPOKEMON) that will lead to constructing
530         # an invalid m3u8 URL. Working around by use of original channel name from stream
531         # JSON and fallback to lowercase if it's not available.
532         channel_id = stream.get('channel', {}).get('name') or channel_id.lower()
533
534         access_token = self._call_api(
535             'api/channels/%s/access_token' % channel_id, channel_id,
536             'Downloading channel access token')
537
538         query = {
539             'allow_source': 'true',
540             'allow_audio_only': 'true',
541             'allow_spectre': 'true',
542             'p': random.randint(1000000, 10000000),
543             'player': 'twitchweb',
544             'segment_preference': '4',
545             'sig': access_token['sig'].encode('utf-8'),
546             'token': access_token['token'].encode('utf-8'),
547         }
548         formats = self._extract_m3u8_formats(
549             '%s/api/channel/hls/%s.m3u8?%s'
550             % (self._USHER_BASE, channel_id, compat_urllib_parse_urlencode(query)),
551             channel_id, 'mp4')
552         self._prefer_source(formats)
553
554         view_count = stream.get('viewers')
555         timestamp = parse_iso8601(stream.get('created_at'))
556
557         channel = stream['channel']
558         title = self._live_title(channel.get('display_name') or channel.get('name'))
559         description = channel.get('status')
560
561         thumbnails = []
562         for thumbnail_key, thumbnail_url in stream['preview'].items():
563             m = re.search(r'(?P<width>\d+)x(?P<height>\d+)\.jpg$', thumbnail_key)
564             if not m:
565                 continue
566             thumbnails.append({
567                 'url': thumbnail_url,
568                 'width': int(m.group('width')),
569                 'height': int(m.group('height')),
570             })
571
572         return {
573             'id': compat_str(stream['_id']),
574             'display_id': channel_id,
575             'title': title,
576             'description': description,
577             'thumbnails': thumbnails,
578             'uploader': channel.get('display_name'),
579             'uploader_id': channel.get('name'),
580             'timestamp': timestamp,
581             'view_count': view_count,
582             'formats': formats,
583             'is_live': True,
584         }
585
586
587 class TwitchClipsIE(InfoExtractor):
588     IE_NAME = 'twitch:clips'
589     _VALID_URL = r'https?://clips\.twitch\.tv/(?:[^/]+/)*(?P<id>[^/?#&]+)'
590
591     _TESTS = [{
592         'url': 'https://clips.twitch.tv/ea/AggressiveCobraPoooound',
593         'md5': '761769e1eafce0ffebfb4089cb3847cd',
594         'info_dict': {
595             'id': 'AggressiveCobraPoooound',
596             'ext': 'mp4',
597             'title': 'EA Play 2016 Live from the Novo Theatre',
598             'thumbnail': r're:^https?://.*\.jpg',
599             'creator': 'EA',
600             'uploader': 'stereotype_',
601             'uploader_id': 'stereotype_',
602         },
603     }, {
604         # multiple formats
605         'url': 'https://clips.twitch.tv/rflegendary/UninterestedBeeDAESuppy',
606         'only_matching': True,
607     }]
608
609     def _real_extract(self, url):
610         video_id = self._match_id(url)
611
612         webpage = self._download_webpage(url, video_id)
613
614         clip = self._parse_json(
615             self._search_regex(
616                 r'(?s)clipInfo\s*=\s*({.+?});', webpage, 'clip info'),
617             video_id, transform_source=js_to_json)
618
619         title = clip.get('title') or clip.get('channel_title') or self._og_search_title(webpage)
620
621         formats = [{
622             'url': option['source'],
623             'format_id': option.get('quality'),
624             'height': int_or_none(option.get('quality')),
625         } for option in clip.get('quality_options', []) if option.get('source')]
626
627         if not formats:
628             formats = [{
629                 'url': clip['clip_video_url'],
630             }]
631
632         self._sort_formats(formats)
633
634         return {
635             'id': video_id,
636             'title': title,
637             'thumbnail': self._og_search_thumbnail(webpage),
638             'creator': clip.get('broadcaster_display_name') or clip.get('broadcaster_login'),
639             'uploader': clip.get('curator_login'),
640             'uploader_id': clip.get('curator_display_name'),
641             'formats': formats,
642         }