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
|
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-03-20 23:21:21 +01:00
|
|
|
from svtplay_dl.utils import filenamify, ensure_unicode, is_py2, decode_html_entities
|
|
|
|
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
|
|
|
old = False
|
2013-04-21 12:29:16 +02:00
|
|
|
|
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-01-10 20:42:23 +01:00
|
|
|
vid = self.find_video_id()
|
2016-01-10 15:33:30 +01:00
|
|
|
if vid is None:
|
|
|
|
yield ServiceError("Cant find video id for this video")
|
|
|
|
return
|
2015-12-27 14:40:27 +01:00
|
|
|
if re.match("^[0-9]+$", vid):
|
|
|
|
old = True
|
2015-10-29 18:08:25 +01:00
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
url = "http://www.svt.se/videoplayer-api/video/%s" % vid
|
|
|
|
data = self.http.request("get", url)
|
2015-08-30 11:27:31 +02:00
|
|
|
if data.status_code == 404:
|
2015-10-29 18:08:25 +01:00
|
|
|
yield ServiceError("Can't get the json file for %s" % url)
|
2015-08-30 11:27:31 +02:00
|
|
|
return
|
2015-12-27 14:40:27 +01:00
|
|
|
|
2015-08-30 00:06:20 +02:00
|
|
|
data = data.json()
|
2015-12-27 14:40:27 +01:00
|
|
|
if "live" in data:
|
|
|
|
self.options.live = data["live"]
|
|
|
|
if old:
|
|
|
|
params = {"output": "json"}
|
2016-03-17 16:57:58 +01:00
|
|
|
try:
|
|
|
|
dataj = self.http.request("get", self.url, params=params).json()
|
|
|
|
except ValueError:
|
|
|
|
dataj = data
|
|
|
|
old = False
|
2015-12-27 14:40:27 +01:00
|
|
|
else:
|
|
|
|
dataj = data
|
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
if self.options.output_auto:
|
|
|
|
self.options.service = "svtplay"
|
2015-12-27 14:40:27 +01:00
|
|
|
self.options.output = self.outputfilename(dataj, self.options.output, ensure_unicode(self.get_urldata()))
|
2014-08-27 22:41:38 +02:00
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
if self.exclude(self.options):
|
2015-09-06 23:04:48 +02:00
|
|
|
yield ServiceError("Excluding video")
|
2014-12-22 17:41:40 +01:00
|
|
|
return
|
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
if "subtitleReferences" in data:
|
|
|
|
for i in data["subtitleReferences"]:
|
2015-12-28 11:41:39 +01:00
|
|
|
if i["format"] == "websrt":
|
2015-12-27 14:40:27 +01:00
|
|
|
yield subtitle(copy.copy(self.options), "wrst", i["url"])
|
|
|
|
if old and dataj["video"]["subtitleReferences"]:
|
2014-12-22 10:04:32 +01:00
|
|
|
try:
|
2015-12-27 14:40:27 +01:00
|
|
|
suburl = dataj["video"]["subtitleReferences"][0]["url"]
|
2014-12-22 10:04:32 +01:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if suburl and len(suburl) > 0:
|
2015-12-26 11:46:14 +01:00
|
|
|
yield subtitle(copy.copy(self.options), "wrst", suburl)
|
2014-12-22 10:04:32 +01:00
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
if len(data["videoReferences"]) == 0:
|
2015-10-25 15:44:47 +01:00
|
|
|
yield ServiceError("Media doesn't have any associated videos (yet?)")
|
|
|
|
return
|
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
for i in data["videoReferences"]:
|
2016-01-02 15:58:55 +01:00
|
|
|
if i["format"] == "hls" or i["format"] == "ios":
|
2015-12-26 11:46:14 +01:00
|
|
|
streams = hlsparse(self.options, self.http.request("get", i["url"]), i["url"])
|
2015-02-01 09:09:37 +01:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
2015-10-04 14:37:16 +02:00
|
|
|
yield streams[n]
|
2016-01-02 15:58:55 +01:00
|
|
|
if i["format"] == "hds" or i["format"] == "flash":
|
2014-04-21 18:41:15 +02:00
|
|
|
match = re.search(r"\/se\/secure\/", i["url"])
|
|
|
|
if not match:
|
2015-12-26 11:46:14 +01:00
|
|
|
streams = hdsparse(self.options, self.http.request("get", i["url"], params={"hdcore": "3.7.0"}), i["url"])
|
2014-10-12 23:31:02 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2016-03-26 21:38:31 +01:00
|
|
|
if i["format"] == "dash264":
|
|
|
|
streams = dashparse(self.options, self.http.request("get", i["url"]), i["url"])
|
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
|
|
|
|
2016-01-10 15:33:30 +01:00
|
|
|
|
2016-01-10 20:42:23 +01:00
|
|
|
def find_video_id(self):
|
2016-01-10 15:33:30 +01:00
|
|
|
match = re.search('data-video-id="([^"]+)"', self.get_urldata())
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
parse = urlparse(self.url)
|
2016-03-20 23:21:21 +01:00
|
|
|
query = parse_qs(parse.query)
|
2016-01-10 15:33:30 +01:00
|
|
|
match = re.search("/video/([0-9]+)/", parse.path)
|
2016-03-20 23:21:21 +01:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
match = re.search("/klipp/([0-9]+)/", parse.path)
|
2016-03-17 16:57:58 +01:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
match = re.search("data-video-id='([^']+)'", self.get_urldata())
|
2016-01-10 15:33:30 +01:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
2016-01-10 20:35:31 +01:00
|
|
|
match = re.search("/videoEpisod-([^/]+)/", parse.path)
|
2016-03-15 21:18:56 +01:00
|
|
|
if not match:
|
|
|
|
match = re.search(r'data-id="(\d+)-', self.get_urldata())
|
2016-03-20 23:21:21 +01:00
|
|
|
vid = None
|
2016-01-10 20:35:31 +01:00
|
|
|
if match:
|
2016-03-20 23:21:21 +01:00
|
|
|
vid = match.group(1)
|
|
|
|
if not vid:
|
|
|
|
for i in query.keys():
|
|
|
|
if i == "articleId":
|
|
|
|
vid = query["articleId"][0]
|
|
|
|
break
|
|
|
|
if vid:
|
|
|
|
vtype = None
|
|
|
|
for i in ["video", "klipp"]:
|
|
|
|
url = "http://www.svtplay.se/%s/%s/" % (i, vid)
|
|
|
|
data = self.http.request("get", url)
|
|
|
|
if data.status_code == 200:
|
|
|
|
vtype = i
|
|
|
|
break
|
|
|
|
if vtype:
|
|
|
|
self._url = "http://www.svtplay.se/%s/%s/" % (vtype, vid)
|
|
|
|
self._urldata = None
|
|
|
|
self.get_urldata()
|
|
|
|
return self.find_video_id()
|
|
|
|
if not match:
|
|
|
|
match = re.search(r'src="(//www.svt.se/wd?[^"]+)"', self.get_urldata())
|
|
|
|
if match:
|
|
|
|
self._urldata = None
|
|
|
|
self._url = "http:%s" % decode_html_entities(match.group(1))
|
|
|
|
self.get_urldata()
|
|
|
|
return self.find_video_id()
|
|
|
|
|
2016-01-10 15:33:30 +01:00
|
|
|
return None
|
2014-01-11 23:02:47 +01:00
|
|
|
|
2014-02-18 18:56:28 +01:00
|
|
|
def find_all_episodes(self, options):
|
2014-05-01 19:51:21 +02:00
|
|
|
match = re.search(r'<link rel="alternate" type="application/rss\+xml" [^>]*href="([^"]+)"',
|
2015-08-31 22:04:59 +02:00
|
|
|
self.get_urldata())
|
2014-05-01 19:51:21 +02:00
|
|
|
if match is None:
|
2015-08-31 22:04:59 +02:00
|
|
|
match = re.findall(r'a class="play[^"]+"\s+href="(/video[^"]+)"', self.get_urldata())
|
2015-03-08 00:44:26 +01:00
|
|
|
if not match:
|
|
|
|
log.error("Couldn't retrieve episode list")
|
|
|
|
return
|
|
|
|
episodes = [urljoin("http://www.svtplay.se", x) for x in match]
|
|
|
|
else:
|
2015-09-01 23:53:13 +02:00
|
|
|
data = self.http.request("get", match.group(1)).content
|
2015-03-08 00:44:26 +01:00
|
|
|
xml = ET.XML(data)
|
2014-02-18 18:56:28 +01:00
|
|
|
|
2015-03-08 00:44:26 +01:00
|
|
|
episodes = [x.text for x in xml.findall(".//item/link")]
|
2014-12-21 13:01:51 +01:00
|
|
|
episodes_new = []
|
|
|
|
n = 1
|
|
|
|
for i in episodes:
|
|
|
|
episodes_new.append(i)
|
|
|
|
if n == options.all_last:
|
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
return sorted(episodes_new)
|
2014-12-28 13:57:50 +01:00
|
|
|
|
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
def outputfilename(self, data, filename, raw):
|
|
|
|
directory = os.path.dirname(filename)
|
|
|
|
if "statistics" in data:
|
|
|
|
name = data["statistics"]["folderStructure"]
|
|
|
|
if name.find(".") > 0:
|
|
|
|
name = name[:name.find(".")]
|
|
|
|
match = re.search("^arkiv-", name)
|
|
|
|
if match:
|
|
|
|
name = name.replace("arkiv-", "")
|
2016-01-03 02:42:32 +01:00
|
|
|
name = filenamify(name.replace("-", "."))
|
2015-12-27 14:40:27 +01:00
|
|
|
other = filenamify(data["context"]["title"])
|
|
|
|
id = data["videoId"]
|
|
|
|
else:
|
|
|
|
name = data["programTitle"]
|
2016-03-15 21:18:56 +01:00
|
|
|
if not name:
|
|
|
|
match = re.search('data-title="([^"]+)"', raw)
|
|
|
|
if match:
|
|
|
|
name = filenamify(match.group(1).replace(" - ", "."))
|
|
|
|
other = None
|
|
|
|
else:
|
|
|
|
if name.find(".") > 0:
|
|
|
|
name = name[:name.find(".")]
|
|
|
|
name = filenamify(name.replace(" - ", "."))
|
|
|
|
other = filenamify(data["episodeTitle"])
|
2016-02-07 20:08:48 +01:00
|
|
|
if is_py2:
|
|
|
|
id = hashlib.sha256(data["programVersionId"]).hexdigest()[:7]
|
|
|
|
else:
|
|
|
|
id = hashlib.sha256(data["programVersionId"].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
|
2015-12-27 14:40:27 +01:00
|
|
|
season = self.seasoninfo(raw)
|
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
|
|
|
|
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):
|
|
|
|
match = re.search(r'play_video-area-aside__sub-title">([^<]+)<span', data)
|
2014-12-28 13:57:50 +01:00
|
|
|
if match:
|
2014-12-28 14:33:25 +01:00
|
|
|
line = match.group(1)
|
2014-12-28 13:57:50 +01:00
|
|
|
else:
|
2015-12-27 14:40:27 +01:00
|
|
|
match = re.search(r'data-title="([^"]+)"', data)
|
|
|
|
if match:
|
|
|
|
line = match.group(1)
|
|
|
|
else:
|
|
|
|
return None
|
2014-12-28 14:33:25 +01:00
|
|
|
|
2015-12-27 14:40:27 +01:00
|
|
|
line = re.sub(" +", "", match.group(1)).replace('\n', '')
|
|
|
|
match = re.search(r"(song(\d+)-)?Avsnitt(\d+)", line)
|
|
|
|
if match:
|
|
|
|
if match.group(2) is None:
|
|
|
|
season = 1
|
|
|
|
else:
|
|
|
|
season = int(match.group(2))
|
|
|
|
if season < 10:
|
|
|
|
season = "0%s" % season
|
|
|
|
episode = int(match.group(3))
|
|
|
|
if episode < 10:
|
|
|
|
episode = "0%s" % episode
|
|
|
|
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
|