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-03-01 23:39:42 +01:00
|
|
|
from __future__ import absolute_import
|
2013-02-12 19:43:37 +01:00
|
|
|
import sys
|
|
|
|
import re
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
2013-04-21 13:42:33 +02:00
|
|
|
from svtplay_dl.utils.urllib import urlparse, parse_qs
|
2014-01-19 14:26:48 +01:00
|
|
|
from svtplay_dl.service import Service, OpenGraphThumbMixin
|
2013-12-30 01:35:08 +01:00
|
|
|
from svtplay_dl.utils import get_http_data, select_quality, subtitle_smi, is_py2_old
|
2013-03-17 19:55:19 +01:00
|
|
|
from svtplay_dl.log import log
|
|
|
|
from svtplay_dl.fetcher.rtmp import download_rtmp
|
|
|
|
from svtplay_dl.fetcher.hds import download_hds
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2014-01-19 14:26:48 +01:00
|
|
|
class Tv4play(Service, OpenGraphThumbMixin):
|
2014-01-01 14:57:17 +01:00
|
|
|
supported_domains = ['tv4play.se', 'tv4.se']
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-01-11 23:02:47 +01:00
|
|
|
def __init__(self, url):
|
|
|
|
Service.__init__(self, url)
|
|
|
|
self.subtitle = None
|
|
|
|
|
2014-01-06 23:14:06 +01:00
|
|
|
def get(self, options):
|
|
|
|
parse = urlparse(self.url)
|
|
|
|
if "tv4play.se" in self.url:
|
2013-01-17 00:21:47 +01:00
|
|
|
try:
|
|
|
|
vid = parse_qs(parse[4])["video_id"][0]
|
|
|
|
except KeyError:
|
|
|
|
log.error("Can't find video file")
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
2014-01-06 23:14:06 +01:00
|
|
|
match = re.search(r"-(\d+)$", self.url)
|
2013-01-17 00:21:47 +01:00
|
|
|
if match:
|
|
|
|
vid = match.group(1)
|
|
|
|
else:
|
2014-01-06 23:14:06 +01:00
|
|
|
data = get_http_data(self.url)
|
2013-05-05 12:57:42 +02:00
|
|
|
match = re.search(r"\"vid\":\"(\d+)\",", data)
|
2013-03-02 22:14:10 +01:00
|
|
|
if match:
|
|
|
|
vid = match.group(1)
|
|
|
|
else:
|
|
|
|
log.error("Can't find video file")
|
|
|
|
sys.exit(2)
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
url = "http://premium.tv4play.se/api/web/asset/%s/play" % vid
|
|
|
|
data = get_http_data(url)
|
|
|
|
xml = ET.XML(data)
|
|
|
|
ss = xml.find("items")
|
2013-12-30 01:35:08 +01:00
|
|
|
if is_py2_old:
|
2013-01-17 00:21:47 +01:00
|
|
|
sa = list(ss.getiterator("item"))
|
|
|
|
else:
|
|
|
|
sa = list(ss.iter("item"))
|
|
|
|
|
|
|
|
if xml.find("live").text:
|
|
|
|
if xml.find("live").text != "false":
|
|
|
|
options.live = True
|
|
|
|
|
|
|
|
streams = {}
|
|
|
|
|
|
|
|
for i in sa:
|
2013-11-17 13:32:33 +01:00
|
|
|
if i.find("mediaFormat").text == "mp4":
|
2013-01-17 00:21:47 +01:00
|
|
|
stream = {}
|
|
|
|
stream["uri"] = i.find("base").text
|
|
|
|
stream["path"] = i.find("url").text
|
|
|
|
streams[int(i.find("bitrate").text)] = stream
|
2013-03-10 13:55:34 +01:00
|
|
|
elif i.find("mediaFormat").text == "smi":
|
2014-01-11 23:02:47 +01:00
|
|
|
self.subtitle = i.find("url").text
|
|
|
|
|
2014-01-15 19:21:05 +01:00
|
|
|
if len(streams) == 0:
|
|
|
|
log.error("Can't find any streams")
|
|
|
|
sys.exit(2)
|
|
|
|
elif len(streams) == 1:
|
2013-01-17 00:21:47 +01:00
|
|
|
test = streams[list(streams.keys())[0]]
|
|
|
|
else:
|
|
|
|
test = select_quality(options, streams)
|
|
|
|
|
2013-04-21 12:29:16 +02:00
|
|
|
## This is how we construct an swf uri, if we'll ever need one
|
2013-05-11 17:48:39 +02:00
|
|
|
swf = "http://www.tv4play.se/flash/tv4playflashlets.swf"
|
|
|
|
options.other = "-W %s -y %s" % (swf, test["path"])
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
if test["uri"][0:4] == "rtmp":
|
|
|
|
download_rtmp(options, test["uri"])
|
|
|
|
elif test["uri"][len(test["uri"])-3:len(test["uri"])] == "f4m":
|
2013-05-05 12:57:42 +02:00
|
|
|
match = re.search(r"\/se\/secure\/", test["uri"])
|
2013-01-17 00:21:47 +01:00
|
|
|
if match:
|
|
|
|
log.error("This stream is encrypted. Use --hls option")
|
|
|
|
sys.exit(2)
|
|
|
|
manifest = "%s?hdcore=2.8.0&g=hejsan" % test["path"]
|
2013-04-21 12:29:16 +02:00
|
|
|
download_hds(options, manifest)
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2014-01-11 23:02:47 +01:00
|
|
|
|
|
|
|
def get_subtitle(self, options):
|
|
|
|
if self.subtitle:
|
|
|
|
data = get_http_data(self.subtitle)
|
|
|
|
subtitle_smi(options, data)
|