1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-28 14:14:15 +01:00
svtplay-dl/lib/svtplay_dl/service/twitch.py

184 lines
6.2 KiB
Python
Raw Normal View History

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 -*-
# pylint has issues with urlparse: "some types could not be inferred"
# pylint: disable=E1103
from __future__ import absolute_import
import re
2014-02-08 17:09:14 +01:00
import json
import os
import copy
2015-05-23 19:18:04 +02:00
from svtplay_dl.utils.urllib import urlparse, quote_plus
from svtplay_dl.service import Service
from svtplay_dl.utils import filenamify
from svtplay_dl.log import log
from svtplay_dl.fetcher.hls import hlsparse
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
class TwitchException(Exception):
2014-02-08 17:09:14 +01:00
pass
2015-05-23 19:18:04 +02:00
class TwitchUrlException(TwitchException):
"""
Used to indicate an invalid URL for a given media_type. E.g.:
TwitchUrlException('video', 'http://twitch.tv/example')
"""
def __init__(self, media_type, url):
super(TwitchUrlException, self).__init__(
"'%s' is not recognized as a %s URL" % (url, media_type)
)
class Twitch(Service):
# Twitch uses language subdomains, e.g. en.www.twitch.tv. They
# are usually two characters, but may have a country suffix as well (e.g.
# zh-tw, zh-cn and pt-br.
supported_domains_re = [
r'^(?:(?:[a-z]{2}-)?[a-z]{2}\.)?(www\.|clips\.)?twitch\.tv$',
]
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'
def get(self):
urlp = urlparse(self.url)
2016-05-14 22:54:30 +02:00
if self.exclude():
2015-09-06 23:04:48 +02:00
yield ServiceError("Excluding video")
return
2015-05-23 19:18:04 +02:00
match = re.match(r'/(\w+)/([bcv])/(\d+)', urlp.path)
if not match:
if re.search("clips.twitch.tv", urlp.netloc):
data = self._get_clips(self.options)
else:
data = self._get_channel(self.options, 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
data = self._get_archive(self.options, match.group(3))
2015-05-24 12:37:16 +02:00
try:
for i in data:
yield i
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
2015-05-23 19:18:04 +02:00
def _get_static_video(self, options, videoid):
access = self._get_access_token(videoid)
2015-05-24 13:59:11 +02:00
if options.output_auto:
2015-09-06 14:37:40 +02:00
data = self.http.request("get", "https://api.twitch.tv/kraken/videos/v%s" % videoid)
if data.status_code == 404:
yield ServiceError("Can't find the video")
return
info = json.loads(data.text)
name = "twitch-%s-%s" % (info["channel"]["name"], filenamify(info["title"]))
directory = os.path.dirname(options.output)
if os.path.isdir(directory):
name = os.path.join(directory, name)
options.output = name
2015-05-24 13:59:11 +02:00
2015-05-23 19:18:04 +02:00
if "token" not in access:
raise TwitchUrlException('video', self.url)
2015-05-23 19:18:04 +02:00
nauth = quote_plus(str(access["token"]))
authsig = access["sig"]
2015-05-23 19:18:04 +02:00
url = "http://usher.twitch.tv/vod/%s?nauth=%s&nauthsig=%s" % (
videoid, nauth, authsig)
streams = hlsparse(options, self.http.request("get", url), url)
2015-05-23 19:18:04 +02:00
if streams:
for n in list(streams.keys()):
yield streams[n]
2015-05-23 19:18:04 +02:00
def _get_archive(self, options, vid):
try:
for n in self._get_static_video(options, vid):
yield n
except TwitchUrlException as e:
2015-05-23 19:18:04 +02:00
log.error(str(e))
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.
"""
2015-05-23 19:18:04 +02:00
return self._ajax_get('/api/%s/%s/access_token' % (vtype, channel))
2014-02-08 17:09:14 +01:00
def _ajax_get(self, method):
url = "%s/%s" % (self.api_base_url, method)
# Logic found in Twitch's global.js. Prepend /kraken/ to url
# path unless the API method already is absolute.
if method[0] != '/':
method = '/kraken/%s' % method
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
query = "token=%s&sig=%s&allow_source=true&allow_spectre=true" % (quote_plus(access['token']), access['sig'])
2014-02-08 17:09:14 +01:00
return "%s/%s.m3u8?%s" % (self.hls_base_url, channel, query)
2015-05-23 19:18:04 +02:00
def _get_channel(self, options, urlp):
match = re.match(r'/(\w+)', urlp.path)
2014-02-08 17:09:14 +01:00
if not match:
raise TwitchUrlException('channel', urlp.geturl())
2014-02-08 17:09:14 +01:00
channel = match.group(1)
2015-05-24 13:59:11 +02:00
if options.output_auto:
options.output = "twitch-%s" % channel
2014-02-08 17:09:14 +01:00
hls_url = self._get_hls_url(channel)
urlp = urlparse(hls_url)
options.live = True
2014-02-08 17:09:14 +01:00
if not options.output:
options.output = channel
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
streams = hlsparse(options, data, hls_url)
2014-04-21 21:55:39 +02:00
for n in list(streams.keys()):
yield streams[n]
def _get_clips(self, options):
match = re.search("quality_options: (\[[^\]]+\])", self.get_urldata())
if not match:
yield ServiceError("Can't find the video clip")
return
if options.output_auto:
name = re.search('slug: "([^"]+)"', self.get_urldata()).group(1)
brodcaster = re.search('broadcaster_login: "([^"]+)"', self.get_urldata()).group(1)
name = "twitch-%s-%s" % (brodcaster, name)
directory = os.path.dirname(options.output)
if os.path.isdir(directory):
name = os.path.join(directory, name)
options.output = name
dataj = json.loads(match.group(1))
for i in dataj:
yield HTTP(copy.copy(options), i["source"], i["quality"])