X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=youtube_dl%2Fextractor%2Ftwitch.py;h=b11a1d5610d0dffe0d98df7d7b05d4228552dfb7;hb=bc694039e47cc871c98abacdf1c0a2e5a257a8a4;hp=36aa1ad6ec578859d90c947dee9c39213dcfda59;hpb=159444a6688172696185404cbfea02945982e968;p=youtube-dl diff --git a/youtube_dl/extractor/twitch.py b/youtube_dl/extractor/twitch.py index 36aa1ad6e..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,6 +30,7 @@ class TwitchIE(InfoExtractor): """ _PAGE_LIMIT = 100 _API_BASE = 'https://api.twitch.tv' + _LOGIN_URL = 'https://secure.twitch.tv/user/login' _TESTS = [{ 'url': 'http://www.twitch.tv/riotgames/b/577357806', 'info_dict': { @@ -64,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]) @@ -109,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'): @@ -165,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(