mirror of
https://github.com/spaam/svtplay-dl.git
synced 2024-11-24 04:05:39 +01:00
parent
462e700485
commit
5a819afa2f
@ -5,19 +5,19 @@ import re
|
|||||||
import copy
|
import copy
|
||||||
import json
|
import json
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
from urllib.parse import urljoin, urlparse, parse_qs
|
from urllib.parse import urljoin, urlparse, parse_qs
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
|
|
||||||
from svtplay_dl.service import Service, MetadataThumbMixin
|
from svtplay_dl.service import Service, MetadataThumbMixin
|
||||||
from svtplay_dl.utils.text import filenamify
|
from svtplay_dl.utils.text import filenamify
|
||||||
from svtplay_dl.fetcher.hds import hdsparse
|
|
||||||
from svtplay_dl.fetcher.hls import hlsparse
|
from svtplay_dl.fetcher.hls import hlsparse
|
||||||
from svtplay_dl.fetcher.dash import dashparse
|
from svtplay_dl.fetcher.dash import dashparse
|
||||||
from svtplay_dl.subtitle import subtitle
|
from svtplay_dl.subtitle import subtitle
|
||||||
from svtplay_dl.error import ServiceError
|
from svtplay_dl.error import ServiceError
|
||||||
|
|
||||||
URL_VIDEO_API = "http://api.svt.se/videoplayer-api/video/"
|
URL_VIDEO_API = "https://api.svt.se/video/"
|
||||||
|
|
||||||
|
|
||||||
class Svtplay(Service, MetadataThumbMixin):
|
class Svtplay(Service, MetadataThumbMixin):
|
||||||
@ -63,28 +63,24 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
return
|
return
|
||||||
janson = json.loads(match.group(1))["videoPage"]
|
janson = json.loads(match.group(1))["videoPage"]
|
||||||
|
|
||||||
if "programTitle" not in janson["video"]:
|
self.visibleid = list(janson['visible'].keys())[0]
|
||||||
yield ServiceError("Can't find any video on that page.")
|
match = re.search(r"__svtplay_apollo'] = ({.*});", urldata)
|
||||||
|
if not match:
|
||||||
|
yield ServiceError("Can't find video info.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.access:
|
janson = json.loads(match.group(1))
|
||||||
for i in janson["video"]["versions"]:
|
for key in janson["ROOT_QUERY"].keys():
|
||||||
if i["accessService"] == self.access:
|
if "listablesByEsceni" in key:
|
||||||
url = urljoin("http://www.svtplay.se", i["contentUrl"])
|
esceni = key
|
||||||
res = self.http.get(url)
|
break
|
||||||
match = re.search("__svtplay'] = ({.*});", res.text)
|
|
||||||
if not match:
|
|
||||||
yield ServiceError("Can't find video info.")
|
|
||||||
return
|
|
||||||
janson = json.loads(match.group(1))["videoPage"]
|
|
||||||
|
|
||||||
self.outputfilename(janson["video"])
|
self.type_name = janson["ROOT_QUERY"][esceni][0]["typename"]
|
||||||
self.extrametadata(janson)
|
vid = janson["{}:{}".format(self.type_name, self.visibleid)]["videoSvtId"]
|
||||||
|
|
||||||
|
self.outputfilename(janson)
|
||||||
|
self.extrametadata(janson, self.type_name, self.visibleid)
|
||||||
|
|
||||||
if "programVersionId" in janson["video"]:
|
|
||||||
vid = janson["video"]["programVersionId"]
|
|
||||||
else:
|
|
||||||
vid = janson["video"]["id"]
|
|
||||||
res = self.http.get(URL_VIDEO_API + vid)
|
res = self.http.get(URL_VIDEO_API + vid)
|
||||||
try:
|
try:
|
||||||
janson = res.json()
|
janson = res.json()
|
||||||
@ -119,14 +115,6 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
if alt:
|
if alt:
|
||||||
alt_streams = hlsparse(self.config, self.http.request("get", alt.request.url), alt.request.url, output=self.output)
|
alt_streams = hlsparse(self.config, self.http.request("get", alt.request.url), alt.request.url, output=self.output)
|
||||||
|
|
||||||
elif i["format"] == "hds":
|
|
||||||
match = re.search(r"\/se\/secure\/", i["url"])
|
|
||||||
if not match:
|
|
||||||
streams = hdsparse(self.config, self.http.request("get", i["url"], params={"hdcore": "3.7.0"}),
|
|
||||||
i["url"], output=self.output)
|
|
||||||
if alt:
|
|
||||||
alt_streams = hdsparse(self.config, self.http.request("get", alt.request.url, params={"hdcore": "3.7.0"}),
|
|
||||||
alt.request.url, output=self.output)
|
|
||||||
elif i["format"] == "dash264" or i["format"] == "dashhbbtv":
|
elif i["format"] == "dash264" or i["format"] == "dashhbbtv":
|
||||||
streams = dashparse(self.config, self.http.request("get", i["url"]), i["url"], output=self.output)
|
streams = dashparse(self.config, self.http.request("get", i["url"]), i["url"], output=self.output)
|
||||||
if alt:
|
if alt:
|
||||||
@ -155,7 +143,7 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
for i in dataj["gridPage"]["content"]:
|
for i in dataj["gridPage"]["content"]:
|
||||||
videos.append(i["contentUrl"])
|
videos.append(i["contentUrl"])
|
||||||
page += 1
|
page += 1
|
||||||
self._last_chance(videos, page, pages)
|
videos.extend(self._last_chance(videos, page, pages))
|
||||||
return videos
|
return videos
|
||||||
|
|
||||||
def _genre(self, jansson):
|
def _genre(self, jansson):
|
||||||
@ -178,34 +166,53 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
|
|
||||||
videos = []
|
videos = []
|
||||||
tab = None
|
tab = None
|
||||||
match = re.search("__svtplay'] = ({.*});", self.get_urldata())
|
if parse.query:
|
||||||
|
query = parse_qs(parse.query)
|
||||||
|
if "tab" in query:
|
||||||
|
tab = query["tab"][0]
|
||||||
|
|
||||||
if re.search("sista-chansen", parse.path):
|
if re.search("sista-chansen", parse.path):
|
||||||
videos = self._last_chance(videos, 1)
|
videos = self._last_chance(videos, 1)
|
||||||
elif not match:
|
|
||||||
logging.error("Couldn't retrieve episode list.")
|
|
||||||
return
|
|
||||||
else:
|
else:
|
||||||
dataj = json.loads(match.group(1))
|
match = re.search(r"__svtplay'] = ({.*});", self.get_urldata())
|
||||||
if re.search("/genre", parse.path):
|
if not match:
|
||||||
videos = self._genre(dataj)
|
logging.error("Can't find video info.")
|
||||||
else:
|
return videos
|
||||||
if parse.query:
|
janson = json.loads(match.group(1))["videoPage"]
|
||||||
query = parse_qs(parse.query)
|
self.visibleid = list(janson['visible'].keys())[0]
|
||||||
if "tab" in query:
|
match = re.search(r"__svtplay_apollo'] = ({.*});", self.get_urldata())
|
||||||
tab = query["tab"][0]
|
if not match:
|
||||||
|
logging.error("Can't find video info.")
|
||||||
|
return videos
|
||||||
|
janson = json.loads(match.group(1))
|
||||||
|
episode = janson["Variant:{}".format(self.visibleid)]
|
||||||
|
associatedContent = episode["associatedContent({\"include\":[\"season\",\"productionPeriod\",\"clips\",\"upcoming\"]})"]
|
||||||
|
|
||||||
if dataj["relatedVideoContent"]:
|
keys = []
|
||||||
items = dataj["relatedVideoContent"]["relatedVideosAccordion"]
|
videos = []
|
||||||
for i in items:
|
videos.append(janson[episode["urls"]["id"]]["svtplay"])
|
||||||
if tab:
|
for i in associatedContent:
|
||||||
if i["slug"] == tab:
|
if tab:
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
section = "Selection:{}".format(tab)
|
||||||
else:
|
if section == i["id"]:
|
||||||
if "klipp" not in i["slug"] and "kommande" not in i["slug"]:
|
keys.append(section)
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
else:
|
||||||
if self.config.get("include_clips"):
|
if i["id"] == "Selection:upcoming":
|
||||||
if i["slug"] == "klipp":
|
continue
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
elif self.config.get("include_clips") and "Selection:clips" in i["id"]:
|
||||||
|
keys.append(i["id"])
|
||||||
|
elif "Selection:clips" not in i["id"]:
|
||||||
|
keys.append(i["id"])
|
||||||
|
|
||||||
|
for i in keys:
|
||||||
|
for n in janson[i]["items"]:
|
||||||
|
epi = janson[janson[n["id"]]["item"]["id"]]
|
||||||
|
if "variants" in epi:
|
||||||
|
for z in epi["variants"]:
|
||||||
|
if janson[janson[z["id"]]["urls"]["id"]]["svtplay"] not in videos:
|
||||||
|
videos.append(janson[janson[z["id"]]["urls"]["id"]]["svtplay"])
|
||||||
|
if janson[epi["urls"]["id"]]["svtplay"] not in videos:
|
||||||
|
videos.append(janson[epi["urls"]["id"]]["svtplay"])
|
||||||
|
|
||||||
episodes = [urljoin("http://www.svtplay.se", x) for x in videos]
|
episodes = [urljoin("http://www.svtplay.se", x) for x in videos]
|
||||||
|
|
||||||
@ -231,16 +238,11 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
def outputfilename(self, data):
|
def outputfilename(self, data):
|
||||||
name = None
|
name = None
|
||||||
desc = None
|
desc = None
|
||||||
if "programTitle" in data and data["programTitle"]:
|
pid = data["{}:{}".format(self.type_name, self.visibleid)]["parent"]["id"]
|
||||||
name = filenamify(data["programTitle"])
|
|
||||||
elif "titleSlug" in data and data["titleSlug"]:
|
|
||||||
name = filenamify(data["titleSlug"])
|
|
||||||
other = data["title"]
|
|
||||||
|
|
||||||
if "programVersionId" in data:
|
name = data[pid]["slug"]
|
||||||
vid = str(data["programVersionId"])
|
other = data["{}:{}".format(self.type_name, self.visibleid)]["slug"]
|
||||||
else:
|
vid = data["{}:{}".format(self.type_name, self.visibleid)]["id"]
|
||||||
vid = str(data["id"])
|
|
||||||
id = hashlib.sha256(vid.encode("utf-8")).hexdigest()[:7]
|
id = hashlib.sha256(vid.encode("utf-8")).hexdigest()[:7]
|
||||||
|
|
||||||
if name == other:
|
if name == other:
|
||||||
@ -250,10 +252,10 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
other = None
|
other = None
|
||||||
|
|
||||||
season, episode = self.seasoninfo(data)
|
season, episode = self.seasoninfo(data)
|
||||||
if "accessService" in data:
|
if "accessibility" in data["{}:{}".format(self.type_name, self.visibleid)]:
|
||||||
if data["accessService"] == "audioDescription":
|
if data["{}:{}".format(self.type_name, self.visibleid)]["accessibility"] == "AudioDescribed":
|
||||||
desc = "syntolkat"
|
desc = "syntolkat"
|
||||||
if data["accessService"] == "signInterpretation":
|
if data["{}:{}".format(self.type_name, self.visibleid)]["accessibility"] == "SignInterpreted":
|
||||||
desc = "teckentolkat"
|
desc = "teckentolkat"
|
||||||
|
|
||||||
if not other:
|
if not other:
|
||||||
@ -261,7 +263,7 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
elif desc:
|
elif desc:
|
||||||
other += "-{}".format(desc)
|
other += "-{}".format(desc)
|
||||||
|
|
||||||
self.output["title"] = name
|
self.output["title"] = filenamify(name)
|
||||||
self.output["id"] = id
|
self.output["id"] = id
|
||||||
self.output["season"] = season
|
self.output["season"] = season
|
||||||
self.output["episode"] = episode
|
self.output["episode"] = episode
|
||||||
@ -269,34 +271,34 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
|
|
||||||
def seasoninfo(self, data):
|
def seasoninfo(self, data):
|
||||||
season, episode = None, None
|
season, episode = None, None
|
||||||
if "season" in data and data["season"]:
|
|
||||||
season = "{:02d}".format(data["season"])
|
if "episode" not in data["{}:{}".format(self.type_name, self.visibleid)]:
|
||||||
if int(season) == 0:
|
return season, episode
|
||||||
season = None
|
|
||||||
if "episodeNumber" in data and data["episodeNumber"]:
|
episodeid = data["{}:{}".format(self.type_name, self.visibleid)]["episode"]["id"]
|
||||||
episode = "{:02d}".format(data["episodeNumber"])
|
if "positionInSeason" not in data[episodeid]:
|
||||||
if int(episode) == 0:
|
return season, episode
|
||||||
episode = None
|
|
||||||
if episode is not None and season is None:
|
match = re.search(r"Säsong (\d+) — Avsnitt (\d+)", data[episodeid]["positionInSeason"])
|
||||||
# Missing season, happens for some barnkanalen shows assume first and only
|
if not match:
|
||||||
season = "01"
|
return season, episode
|
||||||
|
|
||||||
|
season = "{:02d}".format(match.group(1))
|
||||||
|
episode = "{:02d}".format(match.group(2))
|
||||||
|
|
||||||
return season, episode
|
return season, episode
|
||||||
|
|
||||||
def extrametadata(self, data):
|
def extrametadata(self, data, type_name, visibleid):
|
||||||
|
episode = data["{}:{}".format(type_name, visibleid)]
|
||||||
|
|
||||||
self.output["tvshow"] = (self.output["season"] is not None and self.output["episode"] is not None)
|
self.output["tvshow"] = (self.output["season"] is not None and self.output["episode"] is not None)
|
||||||
try:
|
if "validFrom" in episode:
|
||||||
self.output["publishing_datetime"] = data["video"]["broadcastDate"] / 1000
|
self.output["publishing_datetime"] = int(datetime.datetime.strptime(episode["validFrom"], "%Y-%m-%dT%H:%M:%S%z").strftime('%s'))
|
||||||
except KeyError:
|
|
||||||
pass
|
self.output["title_nice"] = data[data["{}:{}".format(type_name, visibleid)]["parent"]["id"]]["name"]
|
||||||
try:
|
|
||||||
title = data["video"]["programTitle"]
|
|
||||||
self.output["title_nice"] = title
|
|
||||||
except KeyError:
|
|
||||||
title = data["video"]["titleSlug"]
|
|
||||||
self.output["title_nice"] = title
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
t = data['state']["titleModel"]["thumbnail"]
|
t = data[data[episode["parent"]["id"]]["image"]["id"]]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
t = ""
|
t = ""
|
||||||
if isinstance(t, dict):
|
if isinstance(t, dict):
|
||||||
@ -307,12 +309,9 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
url = t.format(format="large")
|
url = t.format(format="large")
|
||||||
self.output["showthumbnailurl"] = url
|
self.output["showthumbnailurl"] = url
|
||||||
try:
|
try:
|
||||||
t = data["video"]["thumbnailXL"]
|
t = data[episode["image"]["id"]]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
t = ""
|
||||||
t = data["video"]["thumbnail"]
|
|
||||||
except KeyError:
|
|
||||||
t = ""
|
|
||||||
if isinstance(t, dict):
|
if isinstance(t, dict):
|
||||||
url = "https://www.svtstatic.se/image/original/default/{id}/{changed}?format=auto&quality=100".format(**t)
|
url = "https://www.svtstatic.se/image/original/default/{id}/{changed}?format=auto&quality=100".format(**t)
|
||||||
self.output["episodethumbnailurl"] = url
|
self.output["episodethumbnailurl"] = url
|
||||||
@ -320,11 +319,9 @@ class Svtplay(Service, MetadataThumbMixin):
|
|||||||
# Get the image if size/format is not specified in the URL set it to large
|
# Get the image if size/format is not specified in the URL set it to large
|
||||||
url = t.format(format="large")
|
url = t.format(format="large")
|
||||||
self.output["episodethumbnailurl"] = url
|
self.output["episodethumbnailurl"] = url
|
||||||
try:
|
|
||||||
self.output["showdescription"] = data['state']["titleModel"]["description"]
|
if "longDescription" in data[episode["parent"]["id"]]:
|
||||||
except KeyError:
|
self.output["showdescription"] = data[episode["parent"]["id"]]["longDescription"]
|
||||||
pass
|
|
||||||
try:
|
if "longDescription" in episode:
|
||||||
self.output["episodedescription"] = data["video"]["description"]
|
self.output["episodedescription"] = episode["longDescription"]
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
Loading…
Reference in New Issue
Block a user