]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/twitch.py
36aa1ad6ec578859d90c947dee9c39213dcfda59
[youtubedl] / youtube_dl / extractor / twitch.py
1 from __future__ import unicode_literals
2
3 import itertools
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 parse_iso8601,
10 )
11
12
13 class TwitchIE(InfoExtractor):
14 # TODO: One broadcast may be split into multiple videos. The key
15 # 'broadcast_id' is the same for all parts, and 'broadcast_part'
16 # starts at 1 and increases. Can we treat all parts as one video?
17 _VALID_URL = r"""(?x)^(?:http://)?(?:www\.)?twitch\.tv/
18 (?:
19 (?P<channelid>[^/]+)|
20 (?:(?:[^/]+)/b/(?P<videoid>[^/]+))|
21 (?:(?:[^/]+)/c/(?P<chapterid>[^/]+))
22 )
23 /?(?:\#.*)?$
24 """
25 _PAGE_LIMIT = 100
26 _API_BASE = 'https://api.twitch.tv'
27 _TESTS = [{
28 'url': 'http://www.twitch.tv/riotgames/b/577357806',
29 'info_dict': {
30 'id': 'a577357806',
31 'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG',
32 },
33 'playlist_mincount': 12,
34 }, {
35 'url': 'http://www.twitch.tv/acracingleague/c/5285812',
36 'info_dict': {
37 'id': 'c5285812',
38 'title': 'ACRL Off Season - Sports Cars @ Nordschleife',
39 },
40 'playlist_mincount': 3,
41 }, {
42 'url': 'http://www.twitch.tv/vanillatv',
43 'info_dict': {
44 'id': 'vanillatv',
45 'title': 'VanillaTV',
46 },
47 'playlist_mincount': 412,
48 }]
49
50 def _handle_error(self, response):
51 if not isinstance(response, dict):
52 return
53 error = response.get('error')
54 if error:
55 raise ExtractorError(
56 '%s returned error: %s - %s' % (self.IE_NAME, error, response.get('message')),
57 expected=True)
58
59 def _download_json(self, url, video_id, note='Downloading JSON metadata'):
60 response = super(TwitchIE, self)._download_json(url, video_id, note)
61 self._handle_error(response)
62 return response
63
64 def _extract_media(self, item, item_id):
65 ITEMS = {
66 'a': 'video',
67 'c': 'chapter',
68 }
69 info = self._extract_info(self._download_json(
70 '%s/kraken/videos/%s%s' % (self._API_BASE, item, item_id), item_id,
71 'Downloading %s info JSON' % ITEMS[item]))
72 response = self._download_json(
73 '%s/api/videos/%s%s' % (self._API_BASE, item, item_id), item_id,
74 'Downloading %s playlist JSON' % ITEMS[item])
75 entries = []
76 chunks = response['chunks']
77 qualities = list(chunks.keys())
78 for num, fragment in enumerate(zip(*chunks.values()), start=1):
79 formats = []
80 for fmt_num, fragment_fmt in enumerate(fragment):
81 format_id = qualities[fmt_num]
82 fmt = {
83 'url': fragment_fmt['url'],
84 'format_id': format_id,
85 'quality': 1 if format_id == 'live' else 0,
86 }
87 m = re.search(r'^(?P<height>\d+)[Pp]', format_id)
88 if m:
89 fmt['height'] = int(m.group('height'))
90 formats.append(fmt)
91 self._sort_formats(formats)
92 entry = dict(info)
93 entry['id'] = '%s_%d' % (entry['id'], num)
94 entry['title'] = '%s part %d' % (entry['title'], num)
95 entry['formats'] = formats
96 entries.append(entry)
97 return self.playlist_result(entries, info['id'], info['title'])
98
99 def _extract_info(self, info):
100 return {
101 'id': info['_id'],
102 'title': info['title'],
103 'description': info['description'],
104 'duration': info['length'],
105 'thumbnail': info['preview'],
106 'uploader': info['channel']['display_name'],
107 'uploader_id': info['channel']['name'],
108 'timestamp': parse_iso8601(info['recorded_at']),
109 'view_count': info['views'],
110 }
111
112 def _real_extract(self, url):
113 mobj = re.match(self._VALID_URL, url)
114 if mobj.group('chapterid'):
115 return self._extract_media('c', mobj.group('chapterid'))
116
117 """
118 webpage = self._download_webpage(url, chapter_id)
119 m = re.search(r'PP\.archive_id = "([0-9]+)";', webpage)
120 if not m:
121 raise ExtractorError('Cannot find archive of a chapter')
122 archive_id = m.group(1)
123
124 api = api_base + '/broadcast/by_chapter/%s.xml' % chapter_id
125 doc = self._download_xml(
126 api, chapter_id,
127 note='Downloading chapter information',
128 errnote='Chapter information download failed')
129 for a in doc.findall('.//archive'):
130 if archive_id == a.find('./id').text:
131 break
132 else:
133 raise ExtractorError('Could not find chapter in chapter information')
134
135 video_url = a.find('./video_file_url').text
136 video_ext = video_url.rpartition('.')[2] or 'flv'
137
138 chapter_api_url = 'https://api.twitch.tv/kraken/videos/c' + chapter_id
139 chapter_info = self._download_json(
140 chapter_api_url, 'c' + chapter_id,
141 note='Downloading chapter metadata',
142 errnote='Download of chapter metadata failed')
143
144 bracket_start = int(doc.find('.//bracket_start').text)
145 bracket_end = int(doc.find('.//bracket_end').text)
146
147 # TODO determine start (and probably fix up file)
148 # youtube-dl -v http://www.twitch.tv/firmbelief/c/1757457
149 #video_url += '?start=' + TODO:start_timestamp
150 # bracket_start is 13290, but we want 51670615
151 self._downloader.report_warning('Chapter detected, but we can just download the whole file. '
152 'Chapter starts at %s and ends at %s' % (formatSeconds(bracket_start), formatSeconds(bracket_end)))
153
154 info = {
155 'id': 'c' + chapter_id,
156 'url': video_url,
157 'ext': video_ext,
158 'title': chapter_info['title'],
159 'thumbnail': chapter_info['preview'],
160 'description': chapter_info['description'],
161 'uploader': chapter_info['channel']['display_name'],
162 'uploader_id': chapter_info['channel']['name'],
163 }
164 return info
165 """
166 elif mobj.group('videoid'):
167 return self._extract_media('a', mobj.group('videoid'))
168 elif mobj.group('channelid'):
169 channel_id = mobj.group('channelid')
170 info = self._download_json(
171 '%s/kraken/channels/%s' % (self._API_BASE, channel_id),
172 channel_id, 'Downloading channel info JSON')
173 channel_name = info.get('display_name') or info.get('name')
174 entries = []
175 offset = 0
176 limit = self._PAGE_LIMIT
177 for counter in itertools.count(1):
178 response = self._download_json(
179 '%s/kraken/channels/%s/videos/?offset=%d&limit=%d'
180 % (self._API_BASE, channel_id, offset, limit),
181 channel_id, 'Downloading channel videos JSON page %d' % counter)
182 videos = response['videos']
183 if not videos:
184 break
185 entries.extend([self.url_result(video['url'], 'Twitch') for video in videos])
186 offset += limit
187 return self.playlist_result(entries, channel_id, channel_name)