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-09-06 14:19:10 +02:00
|
|
|
from svtplay_dl.log import log
|
2014-01-19 14:26:48 +01:00
|
|
|
from svtplay_dl.service import Service, OpenGraphThumbMixin
|
2015-08-30 00:06:20 +02:00
|
|
|
from svtplay_dl.utils import filenamify, ensure_unicode
|
2015-03-08 00:44:26 +01:00
|
|
|
from svtplay_dl.utils.urllib import urlparse, urljoin
|
2014-04-27 20:48:13 +02:00
|
|
|
from svtplay_dl.fetcher.hds import hdsparse
|
2014-04-21 21:55:39 +02:00
|
|
|
from svtplay_dl.fetcher.hls import HLS, hlsparse
|
2014-04-21 18:41:15 +02:00
|
|
|
from svtplay_dl.fetcher.rtmp import RTMP
|
|
|
|
from svtplay_dl.fetcher.http import HTTP
|
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
|
|
|
|
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):
|
|
|
|
if re.findall("svt.se", self.url):
|
2015-08-30 00:06:20 +02:00
|
|
|
data = self.get_urldata()
|
2014-12-08 23:07:02 +01:00
|
|
|
match = re.search(r"data-json-href=\"(.*)\"", data)
|
2013-01-17 00:21:47 +01:00
|
|
|
if match:
|
|
|
|
filename = match.group(1).replace("&", "&").replace("&format=json", "")
|
|
|
|
url = "http://www.svt.se%s" % filename
|
|
|
|
else:
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't find video file for: %s" % self.url)
|
2014-10-06 23:08:03 +02:00
|
|
|
return
|
2014-01-06 23:14:06 +01:00
|
|
|
else:
|
|
|
|
url = self.url
|
2013-04-21 12:29:16 +02:00
|
|
|
|
2013-11-14 22:46:08 +01:00
|
|
|
pos = url.find("?")
|
|
|
|
if pos < 0:
|
2015-08-24 22:59:14 +02:00
|
|
|
if "svt.se" in url:
|
|
|
|
dataurl = "%s?format=json" % url
|
|
|
|
else:
|
|
|
|
dataurl = "%s?output=json" % url
|
2013-11-14 22:46:08 +01:00
|
|
|
else:
|
2015-08-24 22:59:14 +02:00
|
|
|
if "svt.se" in url:
|
|
|
|
dataurl = "%s&format=json" % url
|
|
|
|
else:
|
|
|
|
dataurl = "%s&output=json" % url
|
2015-08-31 19:45:15 +02:00
|
|
|
data = self.http.request("get", dataurl)
|
2015-08-30 11:27:31 +02:00
|
|
|
if data.status_code == 404:
|
2015-09-06 14:19:10 +02:00
|
|
|
yield ServiceError("Can't get the json file for %s" % self.json)
|
2015-08-30 11:27:31 +02:00
|
|
|
return
|
2015-08-30 00:06:20 +02:00
|
|
|
data = data.json()
|
2013-04-16 13:18:40 +02:00
|
|
|
if "live" in data["video"]:
|
|
|
|
options.live = data["video"]["live"]
|
2014-01-11 23:02:47 +01:00
|
|
|
|
2014-08-27 22:41:38 +02:00
|
|
|
if options.output_auto:
|
2014-08-27 22:59:31 +02:00
|
|
|
options.service = "svtplay"
|
2015-08-30 00:06:20 +02:00
|
|
|
options.output = outputfilename(data, options.output, ensure_unicode(self.get_urldata()))
|
2014-08-27 22:41:38 +02:00
|
|
|
|
2014-12-22 17:41:40 +01:00
|
|
|
if self.exclude(options):
|
2015-09-06 23:04:48 +02:00
|
|
|
yield ServiceError("Excluding video")
|
2014-12-22 17:41:40 +01:00
|
|
|
return
|
|
|
|
|
2014-12-22 10:04:32 +01:00
|
|
|
if data["video"]["subtitleReferences"]:
|
|
|
|
try:
|
|
|
|
suburl = data["video"]["subtitleReferences"][0]["url"]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
if suburl and len(suburl) > 0:
|
|
|
|
yield subtitle(copy.copy(options), "wrst", suburl)
|
|
|
|
|
2014-09-21 19:12:17 +02:00
|
|
|
if options.force_subtitle:
|
|
|
|
return
|
|
|
|
|
2013-01-17 00:21:47 +01:00
|
|
|
for i in data["video"]["videoReferences"]:
|
2013-10-14 20:05:03 +02:00
|
|
|
parse = urlparse(i["url"])
|
2014-01-11 23:02:47 +01:00
|
|
|
|
2014-04-21 18:41:15 +02:00
|
|
|
if parse.path.find("m3u8") > 0:
|
2015-08-31 19:45:15 +02:00
|
|
|
streams = hlsparse(i["url"], self.http.request("get", i["url"]).text)
|
2015-02-01 09:09:37 +01:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield HLS(copy.copy(options), streams[n], n)
|
2014-04-21 19:12:21 +02:00
|
|
|
elif parse.path.find("f4m") > 0:
|
2014-04-21 18:41:15 +02:00
|
|
|
match = re.search(r"\/se\/secure\/", i["url"])
|
|
|
|
if not match:
|
2015-08-31 19:45:15 +02:00
|
|
|
res = self.http.request("get", i["url"], params={"hdcore": "3.7.0"})
|
|
|
|
streams = hdsparse(copy.copy(options), res.text, i["url"])
|
2014-10-12 23:31:02 +02:00
|
|
|
if streams:
|
|
|
|
for n in list(streams.keys()):
|
|
|
|
yield streams[n]
|
2014-04-21 19:12:21 +02:00
|
|
|
elif parse.scheme == "rtmp":
|
2014-04-21 18:41:15 +02:00
|
|
|
embedurl = "%s?type=embed" % url
|
2015-09-01 23:53:13 +02:00
|
|
|
data = self.http.request("get", embedurl).text
|
2014-04-21 18:41:15 +02:00
|
|
|
match = re.search(r"value=\"(/(public)?(statiskt)?/swf(/video)?/svtplayer-[0-9\.a-f]+swf)\"", data)
|
|
|
|
swf = "http://www.svtplay.se%s" % match.group(1)
|
|
|
|
options.other = "-W %s" % swf
|
2014-06-07 20:43:40 +02:00
|
|
|
yield RTMP(copy.copy(options), i["url"], i["bitrate"])
|
2014-04-21 18:41:15 +02:00
|
|
|
else:
|
2014-06-07 20:43:40 +02:00
|
|
|
yield HTTP(copy.copy(options), i["url"], "0")
|
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
|
|
|
|
|
|
|
|
|
|
|
def outputfilename(data, filename, raw):
|
|
|
|
directory = os.path.dirname(filename)
|
|
|
|
name = data["statistics"]["folderStructure"]
|
|
|
|
if name.find(".") > 0:
|
|
|
|
name = name[:name.find(".")]
|
2014-12-28 14:18:16 +01:00
|
|
|
match = re.search("^arkiv-", name)
|
|
|
|
if match:
|
|
|
|
name = name.replace("arkiv-", "")
|
2014-12-28 13:57:50 +01:00
|
|
|
name = name.replace("-", ".")
|
|
|
|
season = seasoninfo(raw)
|
2015-04-16 21:58:48 +02:00
|
|
|
other = filenamify(data["context"]["title"])
|
2014-12-28 13:57:50 +01:00
|
|
|
if season:
|
|
|
|
title = "%s.%s.%s-%s-svtplay" % (name, season, other, data["videoId"])
|
|
|
|
else:
|
2014-12-28 14:18:16 +01:00
|
|
|
title = "%s.%s-%s-svtplay" % (name, other, data["videoId"])
|
2014-12-28 13:57:50 +01:00
|
|
|
title = filenamify(title)
|
|
|
|
if len(directory):
|
2015-08-24 23:02:18 +02:00
|
|
|
output = os.path.join(directory, title)
|
2014-12-28 13:57:50 +01:00
|
|
|
else:
|
|
|
|
output = title
|
|
|
|
return output
|
|
|
|
|
|
|
|
|
2014-12-28 14:33:25 +01:00
|
|
|
def seasoninfo(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)
|
|
|
|
else:
|
|
|
|
match = re.search(r'data-title="([^"]+)"', 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:
|
|
|
|
return None
|
2014-12-28 14:33:25 +01:00
|
|
|
|
2015-01-05 21:52:34 +01:00
|
|
|
line = re.sub(" +", "", match.group(1)).replace('\n', '')
|
2014-12-28 14:33:25 +01:00
|
|
|
match = re.search(r"(song(\d+)-)?Avsnitt(\d+)", line)
|
|
|
|
if match:
|
|
|
|
if match.group(2) is None:
|
|
|
|
season = 1
|
|
|
|
else:
|
2014-12-28 16:08:50 +01:00
|
|
|
season = int(match.group(2))
|
|
|
|
if season < 10:
|
|
|
|
season = "0%s" % season
|
2014-12-29 20:02:49 +01:00
|
|
|
episode = int(match.group(3))
|
|
|
|
if episode < 10:
|
|
|
|
episode = "0%s" % episode
|
|
|
|
return "S%sE%s" % (season, episode)
|
2014-12-28 13:57:50 +01:00
|
|
|
else:
|
2014-12-29 20:02:49 +01:00
|
|
|
return None
|