1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-28 06:04:17 +01:00
svtplay-dl/lib/svtplay_dl/service/svtplay.py

269 lines
10 KiB
Python
Raw Normal View History

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 -*-
from __future__ import absolute_import
import re
import os
import xml.etree.ElementTree as ET
2014-06-07 20:43:40 +02:00
import copy
import json
import hashlib
from svtplay_dl.log import log
from svtplay_dl.service import Service, OpenGraphThumbMixin
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
from svtplay_dl.fetcher.hls import hlsparse
2016-03-26 21:38:31 +01:00
from svtplay_dl.fetcher.dash import dashparse
from svtplay_dl.subtitle import subtitle
from svtplay_dl.error import ServiceError
2015-09-15 20:10:32 +02:00
class Svtplay(Service, OpenGraphThumbMixin):
supported_domains = ['svtplay.se', 'svt.se', 'beta.svtplay.se', 'svtflow.se']
def get(self):
old = False
parse = urlparse(self.url)
if parse.netloc == "www.svtplay.se" or parse.netloc == "svtplay.se":
if parse.path[:6] != "/video" and parse.path[:6] != "/klipp":
yield ServiceError("This mode is not supported anymore. need the url with the video")
return
match = re.search("__svtplay'] = ({.*});", self.get_urldata())
if not match:
yield ServiceError("Cant find video info.")
janson = json.loads(match.group(1))["videoTitlePage"]
if "live" in janson["video"]:
self.optionslive = janson["video"]["live"]
if self.options.output_auto:
self.options.service = "svtplay"
self.options.output = self.outputfilename(janson, self.options.output)
2016-05-14 22:54:30 +02:00
if self.exclude():
2015-09-06 23:04:48 +02:00
yield ServiceError("Excluding video")
return
if "subtitles" in janson["video"]:
for i in janson["video"]["subtitles"]:
if i["format"] == "WebSRT":
yield subtitle(copy.copy(self.options), "wrst", i["url"])
if len(janson["video"]["videoReferences"]) == 0:
yield ServiceError("Media doesn't have any associated videos (yet?)")
return
for i in janson["video"]["videoReferences"]:
parse = urlparse(i["url"])
query = parse_qs(parse.query)
if i["playerType"] == "hls" or i["playerType"] == "ios":
streams = hlsparse(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 = hlsparse(self.options, self.http.request("get", alt.request.url), alt.request.url)
if streams:
for n in list(streams.keys()):
yield streams[n]
if i["playerType"] == "playerType" or i["playerType"] == "flash":
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"])
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 = 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]
if i["playerType"] == "dash264" or i["playerType"] == "dashhbbtv":
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-01-10 20:42:23 +01:00
def find_video_id(self):
match = re.search('data-video-id="([^"]+)"', self.get_urldata())
if match:
return match.group(1)
parse = urlparse(self.url)
query = parse_qs(parse.query)
match = re.search("/video/([0-9]+)/", parse.path)
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())
if match:
return match.group(1)
2016-01-10 20:35:31 +01:00
match = re.search("/videoEpisod-([^/]+)/", parse.path)
if not match:
match = re.search(r'data-id="(\d+)-', self.get_urldata())
vid = None
2016-01-10 20:35:31 +01:00
if match:
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()
return None
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)
match = re.search("__svtplay'] = ({.*});", res.text)
if not match:
return videos
dataj = json.loads(match.group(1))
pages = dataj["gridPage"]["pagination"]["totalPages"]
for i in dataj["gridPage"]["content"]:
videos.append(i["contentUrl"])
page += 1
self._last_chance(videos, page, pages)
return videos
def _genre(self, jansson):
videos = []
for i in jansson["clusterPage"]["content"]["clips"]:
videos.append(i["contentUrl"])
return videos
def find_all_episodes(self, options):
parse = urlparse(self._url)
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="([^"]+)"',
self.get_urldata())
if match:
match = match.group(1)
if match is None:
videos = []
match = re.search("__svtplay'] = ({.*});", self.get_urldata())
if re.search("sista-chansen", parse.path):
videos = self._last_chance(videos, 1)
elif not match:
log.error("Couldn't retrieve episode list")
return
else:
dataj = json.loads(match.group(1))
if re.search("/genre", parse.path):
videos = self._genre(dataj)
else:
items = dataj["videoTitlePage"]["realatedVideosTabs"]
for i in items:
if "sasong" in i["slug"]:
for n in i["videos"]:
if n["url"] not in videos:
videos.append(n["url"])
if "senast" in i["slug"]:
for n in i["videos"]:
if n["url"] not in videos:
videos.append(n["url"])
episodes = [urljoin("http://www.svtplay.se", x) for x in videos]
else:
data = self.http.request("get", match).content
xml = ET.XML(data)
episodes = [x.text for x in xml.findall(".//item/link")]
episodes_new = []
n = 1
for i in episodes:
episodes_new.append(i)
if n == options.all_last:
break
n += 1
return sorted(episodes_new)
def outputfilename(self, data, filename):
directory = os.path.dirname(filename)
name = data["video"]["titlePagePath"]
other = filenamify(data["video"]["title"])
if "programVersionId" in data["video"]:
vid = str(data["video"]["programVersionId"])
else:
vid = str(data["video"]["id"])
if is_py2:
id = hashlib.sha256(vid).hexdigest()[:7]
else:
id = hashlib.sha256(vid.encode("utf-8")).hexdigest()[:7]
if name == other:
other = None
season = self.seasoninfo(data)
title = name
if season:
title += ".%s" % season
if other:
title += ".%s" % other
title += "-%s-svtplay" % id
title = filenamify(title)
if len(directory):
output = os.path.join(directory, title)
else:
output = title
return output
def seasoninfo(self, data):
if "season" in data["video"]:
season = data["video"]["season"]
if season < 10:
season = "0%s" % season
episode = data["video"]["episodeNumber"]
if episode < 10:
episode = "0%s" % episode
if int(season) == 0 and int(episode) == 0:
return None
return "S%sE%s" % (season, episode)
else:
return None