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-01-17 00:21:47 +01:00
|
|
|
import re
|
2014-08-27 22:41:38 +02:00
|
|
|
import os
|
2014-02-18 18:56:28 +01:00
|
|
|
import xml.etree.ElementTree as ET
|
2014-06-07 20:43:40 +02:00
|
|
|
import copy
|
2016-06-29 23:52:13 +02:00
|
|
|
import json
|
2015-12-27 20:55:10 +01:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from svtplay_dl.log import log
|
2014-01-19 14:26:48 +01:00
|
|
|
from svtplay_dl.service import Service, OpenGraphThumbMixin
|
2016-11-06 15:24:53 +01:00
|
|
|
from svtplay_dl.utils import filenamify, is_py2
|
2016-03-20 23:21:21 +01:00
|
|
|
from svtplay_dl.utils.urllib import urlparse, urljoin, parse_qs
|
2014-04-27 20:48:13 +02:00
|
|
|
from svtplay_dl.fetcher.hds import hdsparse
|
2015-10-04 14:37:16 +02:00
|
|
|
from svtplay_dl.fetcher.hls import hlsparse
|
2016-03-26 21:38:31 +01:00
|
|
|
from svtplay_dl.fetcher.dash import dashparse
|
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-01-17 00:21:47 +01:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2014-01-19 14:26:48 +01:00
|
|
|
class Svtplay(Service, OpenGraphThumbMixin):
|
2014-05-01 19:51:21 +02:00
|
|
|
supported_domains = ['svtplay.se', 'svt.se', 'beta.svtplay.se', 'svtflow.se']
|
2013-01-17 00:21:47 +01:00
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
def get(self):
|
2015-12-27 14:40:27 +01:00
|
|
|
parse = urlparse(self.url)
|
2015-12-28 11:41:15 +01:00
|
|
|
if parse.netloc == "www.svtplay.se" or parse.netloc == "svtplay.se":
|
2016-03-22 22:26:15 +01:00
|
|
|
if parse.path[:6] != "/video" and parse.path[:6] != "/klipp":
|
2015-12-27 14:40:27 +01:00
|
|
|
yield ServiceError("This mode is not supported anymore. need the url with the video")
|
|
|
|
return
|
2016-01-10 15:33:30 +01:00
|
|
|
|
2016-11-06 14:52:11 +01:00
|
|
|
match = re.search("__svtplay'] = ({.*});", self.get_urldata())
|
|
|
|
if not match:
|
|
|
|
yield ServiceError("Cant find video info.")
|
2016-11-06 14:55:43 +01:00
|
|
|
return
|
2016-11-06 14:52:11 +01:00
|
|
|
janson = json.loads(match.group(1))["videoTitlePage"]
|
2015-12-27 14:40:27 +01:00
|
|
|
|
2016-11-06 14:52:11 +01:00
|
|
|
if "live" in janson["video"]:
|
2017-02-15 23:15:50 +01:00
|
|
|
self.options.live = janson["video"]["live"]
|
2015-12-27 14:40:27 +01:00
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
if self.options.output_auto:
|
|
|
|
self.options.service = "svtplay"
|
2017-02-15 00:40:25 +01:00
|
|
|
self.options.output = self.outputfilename(janson["video"], self.options.output)
|
2014-08-27 22:41:38 +02:00
|
|
|
|
2016-05-14 22:54:30 +02:00
|
|
|
if self.exclude():
|
2015-09-06 23:04:48 +02:00
|
|
|
yield ServiceError("Excluding video")
|
2014-12-22 17:41:40 +01:00
|
|
|
return
|
|
|
|
|
2017-01-26 21:54:58 +01:00
|
|
|
if "programVersionId" in janson["video"]:
|
|
|
|
vid = janson["video"]["programVersionId"]
|
|
|
|
else:
|
|
|
|
vid = janson["video"]["id"]
|
2017-02-12 09:01:40 +01:00
|
|
|
res = self.http.get("http://api.svt.se/videoplayer-api/video/{0}".format(vid))
|
2017-01-26 21:54:58 +01:00
|
|
|
janson = res.json()
|
2014-12-22 10:04:32 +01:00
|
|
|
|
2017-01-27 02:10:46 +01:00
|
|
|
if "subtitleReferences" in janson:
|
|
|
|
for i in janson["subtitleReferences"]:
|
|
|
|
if i["format"] == "websrt" and "url" in i:
|
|
|
|
yield subtitle(copy.copy(self.options), "wrst", i["url"])
|
|
|
|
|
2017-01-26 21:54:58 +01:00
|
|
|
if "videoReferences" in janson:
|
|
|
|
if len(janson["videoReferences"]) == 0:
|
2016-11-06 19:48:22 +01:00
|
|
|
yield ServiceError("Media doesn't have any associated videos (yet?)")
|
|
|
|
return
|
2015-10-25 15:44:47 +01:00
|
|
|
|
2017-01-26 21:54:58 +01:00
|
|
|
for i in janson["videoReferences"]:
|
2016-11-06 19:48:22 +01:00
|
|
|
parse = urlparse(i["url"])
|
|
|
|
query = parse_qs(parse.query)
|
2017-01-26 21:54:58 +01:00
|
|
|
if i["format"] == "hls":
|
2016-11-06 19:48:22 +01:00
|
|
|
streams = hlsparse(self.options, self.http.request("get", i["url"]), i["url"])
|
2014-10-12 23:31:02 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2016-09-10 17:30:04 +02:00
|
|
|
if "alt" in query and len(query["alt"]) > 0:
|
|
|
|
alt = self.http.get(query["alt"][0])
|
2016-10-16 19:35:38 +02:00
|
|
|
if alt:
|
2016-11-06 19:48:22 +01:00
|
|
|
streams = hlsparse(self.options, self.http.request("get", alt.request.url), alt.request.url)
|
2016-10-16 19:35:38 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2017-01-26 21:54:58 +01:00
|
|
|
if i["format"] == "hds":
|
2016-11-06 19:48:22 +01:00
|
|
|
match = re.search(r"\/se\/secure\/", i["url"])
|
|
|
|
if not match:
|
|
|
|
streams = hdsparse(self.options, self.http.request("get", i["url"], params={"hdcore": "3.7.0"}), i["url"])
|
2016-10-16 19:35:38 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2016-11-06 19:48:22 +01:00
|
|
|
if "alt" in query and len(query["alt"]) > 0:
|
|
|
|
alt = self.http.get(query["alt"][0])
|
|
|
|
if alt:
|
|
|
|
streams = hdsparse(self.options, self.http.request("get", alt.request.url, params={"hdcore": "3.7.0"}), alt.request.url)
|
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2017-01-26 21:54:58 +01:00
|
|
|
if i["format"] == "dash264" or i["format"] == "dashhbbtv":
|
2016-11-06 19:48:22 +01:00
|
|
|
streams = dashparse(self.options, self.http.request("get", i["url"]), i["url"])
|
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
|
|
|
|
|
|
|
if "alt" in query and len(query["alt"]) > 0:
|
|
|
|
alt = self.http.get(query["alt"][0])
|
|
|
|
if alt:
|
|
|
|
streams = dashparse(self.options, self.http.request("get", alt.request.url), alt.request.url)
|
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2016-09-10 17:30:04 +02:00
|
|
|
|
2016-08-20 13:52:19 +02:00
|
|
|
def _last_chance(self, videos, page, maxpage=2):
|
|
|
|
if page > maxpage:
|
|
|
|
return videos
|
|
|
|
|
|
|
|
res = self.http.get("http://www.svtplay.se/sista-chansen?sida=%s" % page)
|
2016-10-31 19:14:29 +01:00
|
|
|
match = re.search("__svtplay'] = ({.*});", res.text)
|
2016-08-20 13:52:19 +02:00
|
|
|
if not match:
|
|
|
|
return videos
|
|
|
|
|
|
|
|
dataj = json.loads(match.group(1))
|
2016-09-15 08:53:49 +02:00
|
|
|
pages = dataj["gridPage"]["pagination"]["totalPages"]
|
2016-08-20 13:52:19 +02:00
|
|
|
|
2016-09-15 08:53:49 +02:00
|
|
|
for i in dataj["gridPage"]["content"]:
|
2016-08-20 13:52:19 +02:00
|
|
|
videos.append(i["contentUrl"])
|
|
|
|
page += 1
|
|
|
|
self._last_chance(videos, page, pages)
|
|
|
|
return videos
|
|
|
|
|
2016-08-20 15:51:58 +02:00
|
|
|
def _genre(self, jansson):
|
|
|
|
videos = []
|
2017-02-06 13:12:33 +01:00
|
|
|
parse = urlparse(self._url)
|
2017-02-15 23:15:50 +01:00
|
|
|
dataj = jansson["clusterPage"]
|
|
|
|
tab = re.search("tab=(.+)", parse.query)
|
2017-02-15 00:47:36 +01:00
|
|
|
if tab:
|
2017-02-06 13:12:33 +01:00
|
|
|
tab = tab.group(1)
|
|
|
|
for i in dataj["tabs"]:
|
|
|
|
if i["slug"] == tab:
|
2017-02-15 00:47:36 +01:00
|
|
|
videos = self.videos_to_list(i["content"], videos)
|
|
|
|
else:
|
|
|
|
videos = self.videos_to_list(dataj["clips"], videos)
|
|
|
|
|
2016-08-20 15:51:58 +02:00
|
|
|
return videos
|
2016-08-20 13:52:19 +02:00
|
|
|
|
2014-02-18 18:56:28 +01:00
|
|
|
def find_all_episodes(self, options):
|
2016-08-20 13:52:19 +02:00
|
|
|
parse = urlparse(self._url)
|
2016-08-24 00:42:49 +02:00
|
|
|
|
|
|
|
if len(parse.path) > 7 and parse.path[-7:] == "rss.xml":
|
|
|
|
match = self.url
|
|
|
|
else:
|
|
|
|
match = re.search(r'<link rel="alternate" type="application/rss\+xml" [^>]*href="([^"]+)"',
|
2017-02-15 23:15:50 +01:00
|
|
|
self.get_urldata())
|
2016-08-24 00:42:49 +02:00
|
|
|
if match:
|
|
|
|
match = match.group(1)
|
|
|
|
|
2014-05-01 19:51:21 +02:00
|
|
|
if match is None:
|
2016-06-29 23:52:13 +02:00
|
|
|
videos = []
|
2017-02-02 15:57:52 +01:00
|
|
|
tab = None
|
2016-10-31 19:14:29 +01:00
|
|
|
match = re.search("__svtplay'] = ({.*});", self.get_urldata())
|
2016-08-20 13:52:19 +02:00
|
|
|
if re.search("sista-chansen", parse.path):
|
|
|
|
videos = self._last_chance(videos, 1)
|
2016-10-31 19:14:29 +01:00
|
|
|
elif not match:
|
|
|
|
log.error("Couldn't retrieve episode list")
|
|
|
|
return
|
2016-08-20 13:52:19 +02:00
|
|
|
else:
|
2016-10-31 19:14:29 +01:00
|
|
|
dataj = json.loads(match.group(1))
|
|
|
|
if re.search("/genre", parse.path):
|
|
|
|
videos = self._genre(dataj)
|
|
|
|
else:
|
2017-02-02 15:57:52 +01:00
|
|
|
if parse.query:
|
2017-02-15 23:15:50 +01:00
|
|
|
match = re.search("tab=(.+)", parse.query)
|
|
|
|
if match:
|
2017-02-02 15:57:52 +01:00
|
|
|
tab = match.group(1)
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2017-01-26 21:54:58 +01:00
|
|
|
items = dataj["videoTitlePage"]["relatedVideosTabs"]
|
2016-10-31 19:14:29 +01:00
|
|
|
for i in items:
|
2017-02-02 15:57:52 +01:00
|
|
|
if tab:
|
|
|
|
if i["slug"] == tab:
|
2017-02-15 00:40:25 +01:00
|
|
|
videos = self.videos_to_list(i["videos"], videos)
|
|
|
|
|
2017-02-02 15:57:52 +01:00
|
|
|
else:
|
|
|
|
if "sasong" in i["slug"] or "senast" in i["slug"]:
|
2017-02-15 00:40:25 +01:00
|
|
|
videos = self.videos_to_list(i["videos"], videos)
|
2017-02-15 23:15:50 +01:00
|
|
|
|
2017-02-02 15:57:52 +01:00
|
|
|
if self.options.include_clips:
|
2017-02-15 23:15:50 +01:00
|
|
|
if i["slug"] == "klipp":
|
2017-02-15 00:40:25 +01:00
|
|
|
videos = self.videos_to_list(i["videos"], videos)
|
2016-08-20 13:52:19 +02:00
|
|
|
|
2016-06-29 23:52:13 +02:00
|
|
|
episodes = [urljoin("http://www.svtplay.se", x) for x in videos]
|
2015-03-08 00:44:26 +01:00
|
|
|
else:
|
2016-08-24 00:42:49 +02:00
|
|
|
data = self.http.request("get", match).content
|
2015-03-08 00:44:26 +01:00
|
|
|
xml = ET.XML(data)
|
|
|
|
episodes = [x.text for x in xml.findall(".//item/link")]
|
2017-02-10 17:49:42 +01:00
|
|
|
|
|
|
|
if options.all_last > 0:
|
|
|
|
return sorted(episodes[-options.all_last:])
|
|
|
|
return sorted(episodes)
|
2014-12-28 13:57:50 +01:00
|
|
|
|
2017-02-15 00:40:25 +01:00
|
|
|
def videos_to_list(self, lvideos, videos):
|
|
|
|
for n in lvideos:
|
|
|
|
parse = urlparse(n["contentUrl"])
|
|
|
|
if parse.path not in videos:
|
|
|
|
filename = self.outputfilename(n, self.options.output)
|
|
|
|
if not self.exclude2(filename):
|
|
|
|
videos.append(parse.path)
|
|
|
|
return videos
|
|
|
|
|
2016-11-06 14:52:11 +01:00
|
|
|
def outputfilename(self, data, filename):
|
2017-02-15 00:40:25 +01:00
|
|
|
if filename:
|
|
|
|
directory = os.path.dirname(filename)
|
|
|
|
else:
|
|
|
|
directory = ""
|
2017-02-01 13:19:59 +01:00
|
|
|
name = None
|
2017-02-15 00:40:25 +01:00
|
|
|
if data["programTitle"]:
|
|
|
|
name = filenamify(data["programTitle"])
|
|
|
|
other = filenamify(data["title"])
|
2017-01-26 21:54:58 +01:00
|
|
|
|
2017-02-15 00:40:25 +01:00
|
|
|
if "programVersionId" in data:
|
|
|
|
vid = str(data["programVersionId"])
|
2015-12-27 14:40:27 +01:00
|
|
|
else:
|
2017-02-15 00:40:25 +01:00
|
|
|
vid = str(data["id"])
|
2016-11-06 14:52:11 +01:00
|
|
|
if is_py2:
|
|
|
|
id = hashlib.sha256(vid).hexdigest()[:7]
|
|
|
|
else:
|
|
|
|
id = hashlib.sha256(vid.encode("utf-8")).hexdigest()[:7]
|
2015-12-27 14:40:27 +01:00
|
|
|
|
2016-01-03 02:42:32 +01:00
|
|
|
if name == other:
|
|
|
|
other = None
|
2017-02-15 23:15:50 +01:00
|
|
|
elif name is None:
|
2017-01-26 21:54:58 +01:00
|
|
|
name = other
|
|
|
|
other = None
|
2016-11-06 14:52:11 +01:00
|
|
|
season = self.seasoninfo(data)
|
2016-01-03 02:42:32 +01:00
|
|
|
title = name
|
2015-12-27 14:40:27 +01:00
|
|
|
if season:
|
2016-01-03 02:42:32 +01:00
|
|
|
title += ".%s" % season
|
|
|
|
if other:
|
|
|
|
title += ".%s" % other
|
2017-02-15 00:40:25 +01:00
|
|
|
if data["accessServices"]["audioDescription"]:
|
2017-02-15 23:15:50 +01:00
|
|
|
title += "-syntolkat"
|
2017-02-15 00:40:25 +01:00
|
|
|
if data["accessServices"]["signInterpretation"]:
|
2017-02-15 23:15:50 +01:00
|
|
|
title += "-teckentolkat"
|
2016-01-03 02:42:32 +01:00
|
|
|
title += "-%s-svtplay" % id
|
2015-12-27 14:40:27 +01:00
|
|
|
title = filenamify(title)
|
|
|
|
if len(directory):
|
|
|
|
output = os.path.join(directory, title)
|
|
|
|
else:
|
|
|
|
output = title
|
|
|
|
return output
|
|
|
|
|
|
|
|
def seasoninfo(self, data):
|
2017-02-15 00:40:25 +01:00
|
|
|
if "season" in data and data["season"]:
|
|
|
|
season = "{:02d}".format(data["season"])
|
|
|
|
episode = "{:02d}".format(data["episodeNumber"])
|
2016-11-06 14:52:11 +01:00
|
|
|
if int(season) == 0 and int(episode) == 0:
|
|
|
|
return None
|
2015-12-27 14:40:27 +01:00
|
|
|
return "S%sE%s" % (season, episode)
|
2014-12-28 14:33:25 +01:00
|
|
|
else:
|
2015-12-27 14:40:27 +01:00
|
|
|
return None
|