X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=youtube_dl%2Fextractor%2Ftwitch.py;h=b11a1d5610d0dffe0d98df7d7b05d4228552dfb7;hb=31424c126fb48acc49cd539bad6aa7193c7583d6;hp=42d38b224ffbd3feb8c2cd78e90c0a580ecab828;hpb=46fd0dd5a5a8d98b76830636a3dcd241ac7bcc7f;p=youtube-dl diff --git a/youtube_dl/extractor/twitch.py b/youtube_dl/extractor/twitch.py index 42d38b224..b11a1d561 100644 --- a/youtube_dl/extractor/twitch.py +++ b/youtube_dl/extractor/twitch.py @@ -1,9 +1,14 @@ +# coding: utf-8 from __future__ import unicode_literals import itertools import re from .common import InfoExtractor +from ..compat import ( + compat_urllib_parse, + compat_urllib_request, +) from ..utils import ( ExtractorError, parse_iso8601, @@ -17,6 +22,7 @@ class TwitchIE(InfoExtractor): _VALID_URL = r"""(?x)^(?:http://)?(?:www\.)?twitch\.tv/ (?: (?P[^/]+)| + (?:(?:[^/]+)/v/(?P[^/]+))| (?:(?:[^/]+)/b/(?P[^/]+))| (?:(?:[^/]+)/c/(?P[^/]+)) ) @@ -24,18 +30,29 @@ class TwitchIE(InfoExtractor): """ _PAGE_LIMIT = 100 _API_BASE = 'https://api.twitch.tv' - _TEST = { - 'url': 'http://www.twitch.tv/thegamedevhub/b/296128360', - 'md5': 'ecaa8a790c22a40770901460af191c9a', + _LOGIN_URL = 'https://secure.twitch.tv/user/login' + _TESTS = [{ + 'url': 'http://www.twitch.tv/riotgames/b/577357806', 'info_dict': { - 'id': '296128360', - 'ext': 'flv', - 'upload_date': '20110927', - 'uploader_id': 25114803, - 'uploader': 'thegamedevhub', - 'title': 'Beginner Series - Scripting With Python Pt.1' - } - } + 'id': 'a577357806', + 'title': 'Worlds Semifinals - Star Horn Royal Club vs. OMG', + }, + 'playlist_mincount': 12, + }, { + 'url': 'http://www.twitch.tv/acracingleague/c/5285812', + 'info_dict': { + 'id': 'c5285812', + 'title': 'ACRL Off Season - Sports Cars @ Nordschleife', + }, + 'playlist_mincount': 3, + }, { + 'url': 'http://www.twitch.tv/vanillatv', + 'info_dict': { + 'id': 'vanillatv', + 'title': 'VanillaTV', + }, + 'playlist_mincount': 412, + }] def _handle_error(self, response): if not isinstance(response, dict): @@ -54,11 +71,24 @@ class TwitchIE(InfoExtractor): def _extract_media(self, item, item_id): ITEMS = { 'a': 'video', + 'v': 'vod', 'c': 'chapter', } info = self._extract_info(self._download_json( '%s/kraken/videos/%s%s' % (self._API_BASE, item, item_id), item_id, 'Downloading %s info JSON' % ITEMS[item])) + + if item == 'v': + access_token = self._download_json( + '%s/api/vods/%s/access_token' % (self._API_BASE, item_id), item_id, + 'Downloading %s access token' % ITEMS[item]) + formats = self._extract_m3u8_formats( + 'http://usher.twitch.tv/vod/%s?nauth=%s&nauthsig=%s' + % (item_id, access_token['token'], access_token['sig']), + item_id, 'mp4') + info['formats'] = formats + return info + response = self._download_json( '%s/api/videos/%s%s' % (self._API_BASE, item, item_id), item_id, 'Downloading %s playlist JSON' % ITEMS[item]) @@ -80,10 +110,11 @@ class TwitchIE(InfoExtractor): formats.append(fmt) self._sort_formats(formats) entry = dict(info) + entry['id'] = '%s_%d' % (entry['id'], num) entry['title'] = '%s part %d' % (entry['title'], num) entry['formats'] = formats entries.append(entry) - return entries + return self.playlist_result(entries, info['id'], info['title']) def _extract_info(self, info): return { @@ -98,6 +129,44 @@ class TwitchIE(InfoExtractor): 'view_count': info['views'], } + def _real_initialize(self): + self._login() + + def _login(self): + (username, password) = self._get_login_info() + if username is None: + return + + login_page = self._download_webpage( + self._LOGIN_URL, None, 'Downloading login page') + + authenticity_token = self._search_regex( + r']*>(?P[^<]+)", response) + if m: + raise ExtractorError( + 'Unable to login: %s' % m.group('msg').strip(), expected=True) + def _real_extract(self, url): mobj = re.match(self._VALID_URL, url) if mobj.group('chapterid'): @@ -154,6 +223,8 @@ class TwitchIE(InfoExtractor): """ elif mobj.group('videoid'): return self._extract_media('a', mobj.group('videoid')) + elif mobj.group('vodid'): + return self._extract_media('v', mobj.group('vodid')) elif mobj.group('channelid'): channel_id = mobj.group('channelid') info = self._download_json(