X-Git-Url: https://git.rapsys.eu/youtubedl/blobdiff_plain/0865c28fb29a6481cd837cf8c1ef0cd134c6ef8e..09e2f88f88353cd91b2157a8dfb28df7799cd25b:/youtube_dl/extractor/twitch.py
diff --git a/youtube_dl/extractor/twitch.py b/youtube_dl/extractor/twitch.py
index 4b0ce54..eadc48c 100644
--- a/youtube_dl/extractor/twitch.py
+++ b/youtube_dl/extractor/twitch.py
@@ -1,28 +1,47 @@
# coding: utf-8
from __future__ import unicode_literals
+import collections
import itertools
-import re
+import json
import random
+import re
from .common import InfoExtractor
from ..compat import (
+ compat_kwargs,
+ compat_parse_qs,
compat_str,
- compat_urllib_parse,
- compat_urllib_request,
+ compat_urlparse,
+ compat_urllib_parse_urlencode,
+ compat_urllib_parse_urlparse,
)
from ..utils import (
+ clean_html,
ExtractorError,
+ float_or_none,
+ int_or_none,
+ parse_duration,
parse_iso8601,
+ qualities,
+ str_or_none,
+ try_get,
+ unified_timestamp,
+ update_url_query,
+ url_or_none,
+ urljoin,
)
class TwitchBaseIE(InfoExtractor):
- _VALID_URL_BASE = r'https?://(?:www\.)?twitch\.tv'
+ _VALID_URL_BASE = r'https?://(?:(?:www|go|m)\.)?twitch\.tv'
_API_BASE = 'https://api.twitch.tv'
- _USHER_BASE = 'http://usher.twitch.tv'
- _LOGIN_URL = 'https://secure.twitch.tv/user/login'
+ _USHER_BASE = 'https://usher.ttvnw.net'
+ _LOGIN_FORM_URL = 'https://www.twitch.tv/login'
+ _LOGIN_POST_URL = 'https://passport.twitch.tv/login'
+ _CLIENT_ID = 'kimne78kx3ncx6brgo4mv6wki5h1ko'
+ _NETRC_MACHINE = 'twitch'
def _handle_error(self, response):
if not isinstance(response, dict):
@@ -33,16 +52,19 @@ class TwitchBaseIE(InfoExtractor):
'%s returned error: %s - %s' % (self.IE_NAME, error, response.get('message')),
expected=True)
- def _download_json(self, url, video_id, note='Downloading JSON metadata'):
- headers = {
- 'Referer': 'http://api.twitch.tv/crossdomain/receiver.html?v=2',
- 'X-Requested-With': 'XMLHttpRequest',
- }
- for cookie in self._downloader.cookiejar:
- if cookie.name == 'api_token':
- headers['Twitch-Api-Token'] = cookie.value
- request = compat_urllib_request.Request(url, headers=headers)
- response = super(TwitchBaseIE, self)._download_json(request, video_id, note)
+ def _call_api(self, path, item_id, *args, **kwargs):
+ headers = kwargs.get('headers', {}).copy()
+ headers.update({
+ 'Accept': 'application/vnd.twitchtv.v5+json; charset=UTF-8',
+ 'Client-ID': self._CLIENT_ID,
+ })
+ kwargs.update({
+ 'headers': headers,
+ 'expected_status': (400, 410),
+ })
+ response = self._download_json(
+ '%s/%s' % (self._API_BASE, path), item_id,
+ *args, **compat_kwargs(kwargs))
self._handle_error(response)
return response
@@ -50,262 +72,663 @@ class TwitchBaseIE(InfoExtractor):
self._login()
def _login(self):
- (username, password) = self._get_login_info()
+ 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:
+ def fail(message):
raise ExtractorError(
- 'Unable to login: %s' % m.group('msg').strip(), expected=True)
+ 'Unable to login. Twitch said: %s' % message, expected=True)
+ def login_step(page, urlh, note, data):
+ form = self._hidden_inputs(page)
+ form.update(data)
-class TwitchItemBaseIE(TwitchBaseIE):
- def _download_info(self, item, item_id):
- return self._extract_info(self._download_json(
- '%s/kraken/videos/%s%s' % (self._API_BASE, item, item_id), item_id,
- 'Downloading %s info JSON' % self._ITEM_TYPE))
-
- def _extract_media(self, item_id):
- info = self._download_info(self._ITEM_SHORTCUT, item_id)
- response = self._download_json(
- '%s/api/videos/%s%s' % (self._API_BASE, self._ITEM_SHORTCUT, item_id), item_id,
- 'Downloading %s playlist JSON' % self._ITEM_TYPE)
- entries = []
- chunks = response['chunks']
- qualities = list(chunks.keys())
- for num, fragment in enumerate(zip(*chunks.values()), start=1):
- formats = []
- for fmt_num, fragment_fmt in enumerate(fragment):
- format_id = qualities[fmt_num]
- fmt = {
- 'url': fragment_fmt['url'],
- 'format_id': format_id,
- 'quality': 1 if format_id == 'live' else 0,
- }
- m = re.search(r'^(?P\d+)[Pp]', format_id)
- if m:
- fmt['height'] = int(m.group('height'))
- 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 self.playlist_result(entries, info['id'], info['title'])
-
- def _extract_info(self, info):
- return {
- 'id': info['_id'],
- 'title': info['title'],
- 'description': info['description'],
- 'duration': info['length'],
- 'thumbnail': info['preview'],
- 'uploader': info['channel']['display_name'],
- 'uploader_id': info['channel']['name'],
- 'timestamp': parse_iso8601(info['recorded_at']),
- 'view_count': info['views'],
- }
-
- def _real_extract(self, url):
- return self._extract_media(self._match_id(url))
+ page_url = urlh.geturl()
+ post_url = self._search_regex(
+ r'