2013-03-02 21:26:28 +01:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2013-04-27 13:17:00 +02:00
|
|
|
|
|
|
|
# pylint has issues with urlparse: "some types could not be inferred"
|
|
|
|
# pylint: disable=E1103
|
|
|
|
|
2013-03-01 23:39:42 +01:00
|
|
|
from __future__ import absolute_import
|
2013-02-12 19:43:37 +01:00
|
|
|
import re
|
2014-02-08 17:09:14 +01:00
|
|
|
import json
|
2016-08-20 16:32:12 +02:00
|
|
|
import copy
|
2018-11-18 12:47:19 +01:00
|
|
|
import logging
|
2018-01-30 22:07:21 +01:00
|
|
|
from urllib.parse import urlparse, quote_plus
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2013-04-21 12:44:31 +02:00
|
|
|
from svtplay_dl.service import Service
|
2015-10-04 14:37:16 +02:00
|
|
|
from svtplay_dl.fetcher.hls import hlsparse
|
2016-08-20 16:32:12 +02:00
|
|
|
from svtplay_dl.fetcher.http import HTTP
|
2015-09-06 14:37:40 +02:00
|
|
|
from svtplay_dl.error import ServiceError
|
2015-05-23 19:18:04 +02:00
|
|
|
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2015-08-24 18:40:59 +02:00
|
|
|
class TwitchException(Exception):
|
2014-02-08 17:09:14 +01:00
|
|
|
pass
|
|
|
|
|
2015-05-23 19:18:04 +02:00
|
|
|
|
2015-08-24 18:40:59 +02:00
|
|
|
class TwitchUrlException(TwitchException):
|
2014-02-05 19:52:29 +01:00
|
|
|
"""
|
|
|
|
Used to indicate an invalid URL for a given media_type. E.g.:
|
|
|
|
|
2015-08-24 18:40:59 +02:00
|
|
|
TwitchUrlException('video', 'http://twitch.tv/example')
|
2014-02-05 19:52:29 +01:00
|
|
|
"""
|
|
|
|
def __init__(self, media_type, url):
|
2015-08-24 18:40:59 +02:00
|
|
|
super(TwitchUrlException, self).__init__(
|
2017-10-09 22:35:13 +02:00
|
|
|
"'{0}' is not recognized as a {1} URL".format(url, media_type)
|
2014-02-05 19:52:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-08-24 18:40:59 +02:00
|
|
|
class Twitch(Service):
|
|
|
|
# Twitch uses language subdomains, e.g. en.www.twitch.tv. They
|
2014-01-01 15:50:47 +01:00
|
|
|
# are usually two characters, but may have a country suffix as well (e.g.
|
|
|
|
# zh-tw, zh-cn and pt-br.
|
|
|
|
supported_domains_re = [
|
2016-08-20 16:32:12 +02:00
|
|
|
r'^(?:(?:[a-z]{2}-)?[a-z]{2}\.)?(www\.|clips\.)?twitch\.tv$',
|
2015-08-24 18:40:59 +02:00
|
|
|
]
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-02-08 17:09:14 +01:00
|
|
|
api_base_url = 'https://api.twitch.tv'
|
|
|
|
hls_base_url = 'http://usher.justin.tv/api/channel/hls'
|
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
def get(self):
|
2014-02-05 19:52:29 +01:00
|
|
|
urlp = urlparse(self.url)
|
|
|
|
|
2015-05-23 19:18:04 +02:00
|
|
|
match = re.match(r'/(\w+)/([bcv])/(\d+)', urlp.path)
|
|
|
|
if not match:
|
2016-08-20 16:32:12 +02:00
|
|
|
if re.search("clips.twitch.tv", urlp.netloc):
|
2018-05-22 00:02:20 +02:00
|
|
|
data = self._get_clips()
|
2016-08-20 16:32:12 +02:00
|
|
|
else:
|
2018-05-22 00:02:20 +02:00
|
|
|
data = self._get_channel(urlp)
|
2015-05-23 19:18:04 +02:00
|
|
|
else:
|
|
|
|
if match.group(2) in ["b", "c"]:
|
2015-10-20 00:04:29 +02:00
|
|
|
yield ServiceError("This twitch video type is unsupported")
|
2015-05-23 19:18:04 +02:00
|
|
|
return
|
2018-05-22 00:02:20 +02:00
|
|
|
data = self._get_archive(match.group(3))
|
2015-05-24 12:37:16 +02:00
|
|
|
try:
|
|
|
|
for i in data:
|
|
|
|
yield i
|
2016-04-03 19:06:45 +02:00
|
|
|
except TwitchUrlException:
|
2015-10-20 00:04:29 +02:00
|
|
|
yield ServiceError("This twitch video type is unsupported")
|
2015-05-24 12:37:16 +02:00
|
|
|
return
|
2014-02-05 19:52:29 +01:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
def _get_static_video(self, videoid):
|
2015-05-23 19:18:04 +02:00
|
|
|
access = self._get_access_token(videoid)
|
2014-03-09 15:01:04 +01:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
data = self.http.request("get", "https://api.twitch.tv/kraken/videos/v{0}".format(videoid))
|
|
|
|
if data.status_code == 404:
|
|
|
|
yield ServiceError("Can't find the video")
|
|
|
|
return
|
|
|
|
info = json.loads(data.text)
|
|
|
|
self.output["title"] = "twitch-{0}".format(info["channel"]["name"])
|
2018-05-27 16:04:39 +02:00
|
|
|
self.output["episodename"] = info["title"]
|
2015-05-24 13:59:11 +02:00
|
|
|
|
2015-05-23 19:18:04 +02:00
|
|
|
if "token" not in access:
|
2015-08-24 18:40:59 +02:00
|
|
|
raise TwitchUrlException('video', self.url)
|
2015-05-23 19:18:04 +02:00
|
|
|
nauth = quote_plus(str(access["token"]))
|
|
|
|
authsig = access["sig"]
|
2014-03-09 15:01:04 +01:00
|
|
|
|
2017-10-09 22:35:13 +02:00
|
|
|
url = "http://usher.twitch.tv/vod/{0}?nauth={1}&nauthsig={2}".format(videoid, nauth, authsig)
|
2014-03-09 15:01:04 +01:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
streams = hlsparse(copy.copy(self.config), self.http.request("get", url), url, output=self.output)
|
2015-05-23 19:18:04 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
2015-10-04 14:37:16 +02:00
|
|
|
yield streams[n]
|
2014-03-09 15:01:04 +01:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
def _get_archive(self, vid):
|
2015-05-23 19:18:04 +02:00
|
|
|
try:
|
2018-05-22 00:02:20 +02:00
|
|
|
for n in self._get_static_video(vid):
|
2015-05-23 19:18:04 +02:00
|
|
|
yield n
|
2015-08-24 18:40:59 +02:00
|
|
|
except TwitchUrlException as e:
|
2018-11-18 12:47:19 +01:00
|
|
|
logging.error(str(e))
|
2014-03-09 15:01:04 +01:00
|
|
|
|
2015-05-23 19:18:04 +02:00
|
|
|
def _get_access_token(self, channel, vtype="vods"):
|
2014-02-08 17:09:14 +01:00
|
|
|
"""
|
|
|
|
Get a Twitch access token. It's a three element dict:
|
|
|
|
|
|
|
|
* mobile_restricted
|
|
|
|
* sig
|
|
|
|
* token
|
|
|
|
|
|
|
|
`sig` is a hexadecimal string, and `token` is a JSON blob, with
|
|
|
|
information about access expiration. `mobile_restricted` is not
|
|
|
|
important, but is a boolean.
|
|
|
|
|
|
|
|
Both `sig` and `token` should be added to the HLS URI, and the
|
|
|
|
token should, of course, be URI encoded.
|
|
|
|
"""
|
2017-10-09 22:35:13 +02:00
|
|
|
return self._ajax_get('/api/{0}/{1}/access_token'.format(vtype, channel))
|
2014-02-08 17:09:14 +01:00
|
|
|
|
|
|
|
def _ajax_get(self, method):
|
2017-10-09 22:35:13 +02:00
|
|
|
url = "{0}/{1}".format(self.api_base_url, method)
|
2014-02-08 17:09:14 +01:00
|
|
|
|
|
|
|
# Logic found in Twitch's global.js. Prepend /kraken/ to url
|
|
|
|
# path unless the API method already is absolute.
|
|
|
|
if method[0] != '/':
|
2017-10-09 22:35:13 +02:00
|
|
|
method = '/kraken/{0}'.format(method)
|
2014-02-08 17:09:14 +01:00
|
|
|
|
2016-05-02 22:26:30 +02:00
|
|
|
payload = self.http.request("get", url)
|
2015-09-07 19:00:40 +02:00
|
|
|
return json.loads(payload.text)
|
2014-02-08 17:09:14 +01:00
|
|
|
|
|
|
|
def _get_hls_url(self, channel):
|
2015-05-23 19:18:04 +02:00
|
|
|
access = self._get_access_token(channel, "channels")
|
2014-02-08 17:09:14 +01:00
|
|
|
|
2017-10-09 22:35:13 +02:00
|
|
|
query = "token={0}&sig={1}&allow_source=true&allow_spectre=true".format(quote_plus(access['token']), access['sig'])
|
|
|
|
return "{0}/{1}.m3u8?{2}".format(self.hls_base_url, channel, query)
|
2014-02-08 17:09:14 +01:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
def _get_channel(self, urlp):
|
2014-02-05 19:52:29 +01:00
|
|
|
match = re.match(r'/(\w+)', urlp.path)
|
2014-02-08 17:09:14 +01:00
|
|
|
|
2014-02-05 19:52:29 +01:00
|
|
|
if not match:
|
2015-08-24 18:40:59 +02:00
|
|
|
raise TwitchUrlException('channel', urlp.geturl())
|
2014-02-05 19:52:29 +01:00
|
|
|
|
2014-02-08 17:09:14 +01:00
|
|
|
channel = match.group(1)
|
2018-05-22 00:02:20 +02:00
|
|
|
|
|
|
|
self.output["title"] = channel
|
2015-05-24 13:59:11 +02:00
|
|
|
|
2014-02-08 17:09:14 +01:00
|
|
|
hls_url = self._get_hls_url(channel)
|
|
|
|
urlp = urlparse(hls_url)
|
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
self.config.set("live", True)
|
2015-09-06 14:37:40 +02:00
|
|
|
data = self.http.request("get", hls_url)
|
|
|
|
if data.status_code == 404:
|
|
|
|
yield ServiceError("Stream is not online.")
|
|
|
|
return
|
2018-05-22 00:02:20 +02:00
|
|
|
streams = hlsparse(self.output, data, hls_url, output=self.output)
|
2014-04-21 21:55:39 +02:00
|
|
|
for n in list(streams.keys()):
|
2015-10-04 14:37:16 +02:00
|
|
|
yield streams[n]
|
2016-08-20 16:32:12 +02:00
|
|
|
|
2018-05-22 00:02:20 +02:00
|
|
|
def _get_clips(self):
|
2018-10-28 23:16:00 +01:00
|
|
|
match = re.search(r"quality_options: (\[[^\]]+\])", self.get_urldata())
|
2016-08-20 16:32:12 +02:00
|
|
|
if not match:
|
|
|
|
yield ServiceError("Can't find the video clip")
|
|
|
|
return
|
2018-10-28 23:16:00 +01:00
|
|
|
name = re.search(r'slug: "([^"]+)"', self.get_urldata()).group(1)
|
2018-05-22 00:02:20 +02:00
|
|
|
brodcaster = re.search('broadcaster_login: "([^"]+)"', self.get_urldata()).group(1)
|
|
|
|
self.output["title"] = "twitch-{0}".format(brodcaster)
|
2018-05-27 16:04:39 +02:00
|
|
|
self.output["episodename"] = name
|
2016-08-20 16:32:12 +02:00
|
|
|
|
|
|
|
dataj = json.loads(match.group(1))
|
|
|
|
for i in dataj:
|
2018-05-22 00:02:20 +02:00
|
|
|
yield HTTP(copy.copy(self.config), i["source"], i["quality"], output=self.output)
|