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 -*-
|
2017-10-17 19:56:20 +02:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2013-02-12 19:43:37 +01:00
|
|
|
import re
|
|
|
|
import xml.etree.ElementTree as ET
|
2014-04-03 19:52:51 +02:00
|
|
|
import json
|
2014-06-07 20:43:40 +02:00
|
|
|
import copy
|
2018-01-15 00:37:18 +01:00
|
|
|
from datetime import datetime, timedelta
|
2018-01-30 22:07:21 +01:00
|
|
|
from urllib.parse import urlparse, parse_qs, quote_plus
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2014-01-19 14:26:48 +01:00
|
|
|
from svtplay_dl.service import Service, OpenGraphThumbMixin
|
2015-10-04 14:37:16 +02:00
|
|
|
from svtplay_dl.fetcher.hls import hlsparse
|
2014-04-21 18:24:01 +02:00
|
|
|
from svtplay_dl.fetcher.rtmp import RTMP
|
2014-04-27 13:24:53 +02:00
|
|
|
from svtplay_dl.fetcher.hds import hdsparse
|
2014-08-31 01:20:36 +02:00
|
|
|
from svtplay_dl.subtitle import subtitle
|
2015-09-06 14:19:10 +02:00
|
|
|
from svtplay_dl.error import ServiceError
|
2013-02-12 19:43:37 +01:00
|
|
|
|
2015-09-15 20:10:32 +02: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
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
def get(self):
|
2018-01-15 00:37:18 +01:00
|
|
|
parse = urlparse(self.url)
|
|
|
|
if parse.path[:8] == "/kanaler":
|
|
|
|
|
2018-03-24 18:43:05 +01:00
|
|
|
end_time_stamp = (datetime.utcnow() - timedelta(minutes=1, seconds=20)).replace(microsecond=0)
|
2018-01-15 22:16:07 +01:00
|
|
|
start_time_stamp = end_time_stamp - timedelta(minutes=1)
|
2018-01-15 00:37:18 +01:00
|
|
|
|
2018-01-30 20:11:37 +01:00
|
|
|
url = "https://bbr-l2v.akamaized.net/live/{0}/master.m3u8?in={1}&out={2}?".format(parse.path[9:],
|
|
|
|
start_time_stamp.isoformat(),
|
|
|
|
end_time_stamp.isoformat())
|
2018-01-15 22:16:07 +01:00
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
self.config.set("live", True)
|
2018-01-15 22:16:07 +01:00
|
|
|
self.options.hls_time_stamp = True
|
2018-05-21 00:56:22 +02:00
|
|
|
streams = hlsparse(self.config, self.http.request("get", url), url, output=self.output)
|
2018-05-08 22:48:55 +02:00
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2018-01-15 00:37:18 +01:00
|
|
|
return
|
|
|
|
|
2015-08-30 00:06:20 +02:00
|
|
|
data = self.get_urldata()
|
2015-01-20 13:10:35 +01:00
|
|
|
|
|
|
|
vid = findvid(self.url, data)
|
2017-10-07 16:01:56 +02:00
|
|
|
if not vid:
|
2017-10-07 15:01:26 +02:00
|
|
|
yield ServiceError("Can't find video id for {0}.".format(self.url))
|
2015-01-20 13:10:35 +01:00
|
|
|
return
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2017-10-07 15:01:26 +02:00
|
|
|
url = "http://prima.tv4play.se/api/web/asset/{0}/play".format(vid)
|
2015-08-31 19:45:15 +02:00
|
|
|
data = self.http.request("get", url, cookies=self.cookies)
|
2015-08-30 10:20:47 +02:00
|
|
|
if data.status_code == 401:
|
2015-08-30 00:06:20 +02:00
|
|
|
xml = ET.XML(data.content)
|
2014-09-07 22:51:44 +02:00
|
|
|
code = xml.find("code").text
|
|
|
|
if code == "SESSION_NOT_AUTHENTICATED":
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't access premium content")
|
2014-09-28 19:34:56 +02:00
|
|
|
elif code == "ASSET_PLAYBACK_INVALID_GEO_LOCATION":
|
2015-11-23 00:01:53 +01:00
|
|
|
yield ServiceError("Can't download this video because of geoblock.")
|
2014-09-07 22:51:44 +02:00
|
|
|
else:
|
2017-10-07 15:06:39 +02:00
|
|
|
yield ServiceError("Can't find any info for that video.")
|
2014-09-07 22:51:44 +02:00
|
|
|
return
|
2015-08-30 11:27:31 +02:00
|
|
|
if data.status_code == 404:
|
2017-10-07 15:06:39 +02:00
|
|
|
yield ServiceError("Can't find the video api.")
|
2015-08-30 11:27:31 +02:00
|
|
|
return
|
2015-08-30 00:06:20 +02:00
|
|
|
xml = ET.XML(data.content)
|
2013-01-17 00:21:47 +01:00
|
|
|
ss = xml.find("items")
|
2018-01-13 20:27:40 +01:00
|
|
|
sa = list(ss.iter("item"))
|
2013-01-17 00:21:47 +01:00
|
|
|
|
|
|
|
if xml.find("live").text:
|
2018-05-13 13:06:45 +02:00
|
|
|
self.config.set("live", (xml.find("live").text != "false"))
|
2014-06-03 16:25:31 +02:00
|
|
|
if xml.find("drmProtected").text == "true":
|
2017-10-07 15:06:39 +02:00
|
|
|
yield ServiceError("We can't download DRM protected content from this site.")
|
2014-11-25 21:46:33 +01:00
|
|
|
return
|
2016-03-27 13:02:49 +02:00
|
|
|
if xml.find("playbackStatus").text == "NOT_STARTED":
|
2017-10-07 15:06:39 +02:00
|
|
|
yield ServiceError("Can't download something that is not started.")
|
2016-03-27 13:02:49 +02:00
|
|
|
return
|
2014-08-27 22:58:37 +02:00
|
|
|
|
2018-05-21 22:13:08 +02:00
|
|
|
basename = self._autoname(vid)
|
|
|
|
if not basename:
|
|
|
|
yield ServiceError("Cant find vid id for autonaming.")
|
|
|
|
return
|
2014-08-27 22:58:37 +02:00
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
for i in sa:
|
2013-11-17 13:32:33 +01:00
|
|
|
if i.find("mediaFormat").text == "mp4":
|
2014-06-23 21:22:26 +02:00
|
|
|
base = urlparse(i.find("base").text)
|
|
|
|
parse = urlparse(i.find("url").text)
|
2014-12-20 23:00:06 +01:00
|
|
|
if "rtmp" in base.scheme:
|
2014-04-21 18:24:01 +02:00
|
|
|
swf = "http://www.tv4play.se/flash/tv4playflashlets.swf"
|
2018-05-21 22:13:30 +02:00
|
|
|
yield RTMP(copy.copy(self.config), i.find("base").text, i.find("bitrate").text, output=self.output,
|
|
|
|
other="-W {0} -y {1}".format(swf, i.find("url").text))
|
2018-01-30 20:11:37 +01:00
|
|
|
elif parse.path[len(parse.path) - 3:len(parse.path)] == "f4m":
|
2018-05-08 22:46:11 +02:00
|
|
|
streams = hdsparse(self.config, self.http.request("get", i.find("url").text,
|
2018-05-21 00:56:22 +02:00
|
|
|
params={"hdcore": "3.7.0"}), i.find("url").text, output=self.output)
|
2018-05-08 22:46:11 +02:00
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2017-09-17 11:20:31 +02:00
|
|
|
elif i.find("mediaFormat").text == "webvtt":
|
2018-05-08 22:46:11 +02:00
|
|
|
yield subtitle(copy.copy(self.config), "wrst", i.find("url").text, output=self.output)
|
2014-04-03 19:52:51 +02:00
|
|
|
|
2017-10-07 15:01:26 +02:00
|
|
|
url = "https://prima.tv4play.se/api/web/asset/{0}/play?protocol=hls3".format(vid)
|
2015-08-31 19:45:15 +02:00
|
|
|
data = self.http.request("get", url, cookies=self.cookies).content
|
2014-07-13 22:48:34 +02:00
|
|
|
xml = ET.XML(data)
|
|
|
|
ss = xml.find("items")
|
2018-01-13 20:27:40 +01:00
|
|
|
sa = list(ss.iter("item"))
|
2014-07-13 22:48:34 +02:00
|
|
|
for i in sa:
|
2014-08-18 22:22:03 +02:00
|
|
|
if i.find("mediaFormat").text == "mp4":
|
|
|
|
parse = urlparse(i.find("url").text)
|
|
|
|
if parse.path.endswith("m3u8"):
|
2018-05-21 00:56:22 +02:00
|
|
|
streams = hlsparse(self.config, self.http.request("get", i.find("url").text), i.find("url").text, output=self.output)
|
2018-05-08 22:48:55 +02:00
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2014-07-13 22:48:34 +02:00
|
|
|
|
2015-10-07 19:52:37 +02:00
|
|
|
def _get_show_info(self):
|
2017-10-07 15:55:22 +02:00
|
|
|
show = self._get_showname()
|
2018-05-13 13:06:45 +02:00
|
|
|
live = str(self.config.get("live")).lower()
|
2018-01-30 23:10:22 +01:00
|
|
|
data = self.http.request("get", "http://webapi.tv4play.se/play/video_assets?type=episode&is_live={0}&"
|
|
|
|
"platform=web&node_nids={1}&per_page=99999".format(live, show)).text
|
2015-10-07 19:52:37 +02:00
|
|
|
jsondata = json.loads(data)
|
|
|
|
return jsondata
|
|
|
|
|
2015-12-15 00:30:40 +01:00
|
|
|
def _get_clip_info(self, vid):
|
2017-10-07 15:55:22 +02:00
|
|
|
show = self._get_showname()
|
2015-12-15 00:30:40 +01:00
|
|
|
page = 1
|
|
|
|
assets = page * 1000
|
|
|
|
run = True
|
2018-05-13 13:06:45 +02:00
|
|
|
live = str(self.config.get("live")).lower()
|
2015-12-15 00:30:40 +01:00
|
|
|
while run:
|
2018-01-30 23:10:22 +01:00
|
|
|
data = self.http.request("get", "http://webapi.tv4play.se/play/video_assets?type=clips&is_live={0}"
|
|
|
|
"&platform=web&node_nids={1}&per_page=1000&page={2}".format(live, show, page)).text
|
2015-12-15 00:30:40 +01:00
|
|
|
jsondata = json.loads(data)
|
|
|
|
for i in jsondata["results"]:
|
|
|
|
if vid == i["id"]:
|
|
|
|
return i["title"]
|
|
|
|
if not run:
|
|
|
|
return None
|
|
|
|
total = jsondata["total_hits"]
|
|
|
|
if assets > total:
|
|
|
|
run = False
|
|
|
|
page += 1
|
|
|
|
assets = page * 1000
|
|
|
|
return None
|
|
|
|
|
2017-10-07 15:55:22 +02:00
|
|
|
def _get_showname(self):
|
2016-01-03 15:10:24 +01:00
|
|
|
parse = urlparse(self.url)
|
2017-10-07 15:55:22 +02:00
|
|
|
show = None
|
2016-01-03 15:10:24 +01:00
|
|
|
if parse.path.count("/") > 2:
|
|
|
|
match = re.search("^/([^/]+)/", parse.path)
|
2017-09-12 12:33:49 +02:00
|
|
|
if "program" == match.group(1):
|
|
|
|
match = re.search("^/program/([^/]+)/", parse.path)
|
|
|
|
if match:
|
|
|
|
show = match.group(1)
|
|
|
|
else:
|
|
|
|
show = match.group(1)
|
2016-01-03 15:10:24 +01:00
|
|
|
else:
|
2018-01-30 20:11:37 +01:00
|
|
|
show = parse.path[parse.path.find("/", 1) + 1:]
|
2017-10-07 15:55:22 +02:00
|
|
|
if show and not re.search("%", show):
|
2016-01-03 15:10:24 +01:00
|
|
|
show = quote_plus(show)
|
|
|
|
return show
|
|
|
|
|
2017-02-19 11:50:33 +01:00
|
|
|
def _seasoninfo(self, data):
|
|
|
|
if "season" in data and data["season"]:
|
|
|
|
season = "{:02d}".format(data["season"])
|
2017-05-01 22:04:57 +02:00
|
|
|
if "episode" in data:
|
|
|
|
episode = "{:02d}".format(data["episode"])
|
|
|
|
if int(season) == 0 and int(episode) == 0:
|
2018-05-13 13:06:45 +02:00
|
|
|
return False
|
|
|
|
self.output["season"] = season
|
|
|
|
self.output["episode"] = episode
|
|
|
|
return True
|
2017-05-01 22:04:57 +02:00
|
|
|
else:
|
2018-05-13 13:06:45 +02:00
|
|
|
self.output["season"] = season
|
|
|
|
return True
|
2017-02-19 11:50:33 +01:00
|
|
|
else:
|
2018-05-13 13:06:45 +02:00
|
|
|
return False
|
2017-02-19 11:50:33 +01:00
|
|
|
|
2015-10-07 19:52:37 +02:00
|
|
|
def _autoname(self, vid):
|
2018-05-21 23:36:11 +02:00
|
|
|
self.output["id"] = vid
|
2018-05-13 13:06:45 +02:00
|
|
|
jsondata = self._get_show_info()
|
|
|
|
for i in jsondata["results"]:
|
|
|
|
if vid == i["id"]:
|
|
|
|
season = self._seasoninfo(i)
|
|
|
|
if season:
|
|
|
|
index = len(i["program"]["name"])
|
|
|
|
self.output["title"] = i["title"][:index]
|
|
|
|
self.output["episodename"] = i["title"][index:]
|
|
|
|
return True
|
|
|
|
self.output["title"] = i["title"]
|
|
|
|
return True
|
|
|
|
|
|
|
|
aname = self._get_clip_info(vid)
|
|
|
|
if aname is not None:
|
|
|
|
self.output["title"] = aname
|
|
|
|
return True
|
2018-05-06 18:54:06 +02:00
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
aname = self._get_showname()
|
|
|
|
if aname is not None:
|
|
|
|
self.output["title"] = aname
|
|
|
|
return True
|
2018-05-06 18:54:06 +02:00
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
return "tv4Stream"
|
2015-10-07 19:52:37 +02:00
|
|
|
|
2016-04-30 14:10:43 +02:00
|
|
|
def _getdays(self, data, text):
|
|
|
|
try:
|
|
|
|
days = int(data["availability"][text])
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
days = 999
|
|
|
|
return days
|
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
def find_all_episodes(self, config):
|
2015-10-05 19:43:57 +02:00
|
|
|
premium = False
|
2015-10-07 19:52:37 +02:00
|
|
|
jsondata = self._get_show_info()
|
|
|
|
|
2014-04-03 19:52:51 +02:00
|
|
|
episodes = []
|
2014-12-21 13:01:28 +01:00
|
|
|
n = 1
|
2014-04-03 19:52:51 +02:00
|
|
|
for i in jsondata["results"]:
|
2015-10-05 19:43:57 +02:00
|
|
|
if premium:
|
|
|
|
text = "availability_group_premium"
|
|
|
|
else:
|
|
|
|
text = "availability_group_free"
|
|
|
|
|
2016-04-30 14:10:43 +02:00
|
|
|
days = self._getdays(i, text)
|
|
|
|
if premium and days == 0:
|
|
|
|
days = self._getdays(i, "availability_group_free")
|
|
|
|
|
2014-11-25 21:48:08 +01:00
|
|
|
if days > 0:
|
2014-07-22 10:13:49 +02:00
|
|
|
video_id = i["id"]
|
2018-05-13 13:06:45 +02:00
|
|
|
url = "http://www.tv4play.se/program/{0}?video_id={1}".format(i["program"]["nid"], video_id)
|
2014-04-03 19:52:51 +02:00
|
|
|
episodes.append(url)
|
2018-05-13 13:06:45 +02:00
|
|
|
if n == config.get("all_last"):
|
2014-12-21 13:01:28 +01:00
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
|
2014-12-21 12:27:16 +01:00
|
|
|
return episodes
|
2015-01-20 13:10:35 +01:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2015-01-20 13:10:35 +01:00
|
|
|
def findvid(url, data):
|
|
|
|
parse = urlparse(url)
|
|
|
|
if "tv4play.se" in url:
|
2017-09-12 12:25:16 +02:00
|
|
|
if "video_id" in parse_qs(parse.query):
|
|
|
|
return parse_qs(parse.query)["video_id"][0]
|
|
|
|
match = re.search(r'burtVmanId: "(\d+)"', data)
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
2015-01-20 13:10:35 +01:00
|
|
|
else:
|
|
|
|
match = re.search(r"\"vid\":\"(\d+)\",", data)
|
|
|
|
if match:
|
2017-09-12 12:25:16 +02:00
|
|
|
return match.group(1)
|
|
|
|
match = re.search(r"-(\d+)$", url)
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
match = re.search(r"meta content='([^']+)' property='og:video'", data)
|
|
|
|
if match:
|
|
|
|
match = re.search(r"vid=(\d+)&", match.group(1))
|
2015-01-20 13:10:35 +01:00
|
|
|
if match:
|
2017-09-12 12:25:16 +02:00
|
|
|
return match.group(1)
|
2017-10-07 15:55:22 +02:00
|
|
|
return None
|