mirror of
https://github.com/spaam/svtplay-dl.git
synced 2024-11-27 21:54:17 +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)
|
||||||
return
|
|
||||||
|
|
||||||
if self.access:
|
|
||||||
for i in janson["video"]["versions"]:
|
|
||||||
if i["accessService"] == self.access:
|
|
||||||
url = urljoin("http://www.svtplay.se", i["contentUrl"])
|
|
||||||
res = self.http.get(url)
|
|
||||||
match = re.search("__svtplay'] = ({.*});", res.text)
|
|
||||||
if not match:
|
if not match:
|
||||||
yield ServiceError("Can't find video info.")
|
yield ServiceError("Can't find video info.")
|
||||||
return
|
return
|
||||||
janson = json.loads(match.group(1))["videoPage"]
|
|
||||||
|
|
||||||
self.outputfilename(janson["video"])
|
janson = json.loads(match.group(1))
|
||||||
self.extrametadata(janson)
|
for key in janson["ROOT_QUERY"].keys():
|
||||||
|
if "listablesByEsceni" in key:
|
||||||
|
esceni = key
|
||||||
|
break
|
||||||
|
|
||||||
|
self.type_name = janson["ROOT_QUERY"][esceni][0]["typename"]
|
||||||
|
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 re.search("sista-chansen", parse.path):
|
|
||||||
videos = self._last_chance(videos, 1)
|
|
||||||
elif not match:
|
|
||||||
logging.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:
|
|
||||||
if parse.query:
|
if parse.query:
|
||||||
query = parse_qs(parse.query)
|
query = parse_qs(parse.query)
|
||||||
if "tab" in query:
|
if "tab" in query:
|
||||||
tab = query["tab"][0]
|
tab = query["tab"][0]
|
||||||
|
|
||||||
if dataj["relatedVideoContent"]:
|
if re.search("sista-chansen", parse.path):
|
||||||
items = dataj["relatedVideoContent"]["relatedVideosAccordion"]
|
videos = self._last_chance(videos, 1)
|
||||||
for i in items:
|
|
||||||
if tab:
|
|
||||||
if i["slug"] == tab:
|
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
|
||||||
else:
|
else:
|
||||||
if "klipp" not in i["slug"] and "kommande" not in i["slug"]:
|
match = re.search(r"__svtplay'] = ({.*});", self.get_urldata())
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
if not match:
|
||||||
if self.config.get("include_clips"):
|
logging.error("Can't find video info.")
|
||||||
if i["slug"] == "klipp":
|
return videos
|
||||||
videos = self.videos_to_list(i["videos"], videos)
|
janson = json.loads(match.group(1))["videoPage"]
|
||||||
|
self.visibleid = list(janson['visible'].keys())[0]
|
||||||
|
match = re.search(r"__svtplay_apollo'] = ({.*});", self.get_urldata())
|
||||||
|
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\"]})"]
|
||||||
|
|
||||||
|
keys = []
|
||||||
|
videos = []
|
||||||
|
videos.append(janson[episode["urls"]["id"]]["svtplay"])
|
||||||
|
for i in associatedContent:
|
||||||
|
if tab:
|
||||||
|
section = "Selection:{}".format(tab)
|
||||||
|
if section == i["id"]:
|
||||||
|
keys.append(section)
|
||||||
|
else:
|
||||||
|
if i["id"] == "Selection:upcoming":
|
||||||
|
continue
|
||||||
|
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:
|
|
||||||
season = None
|
|
||||||
if "episodeNumber" in data and data["episodeNumber"]:
|
|
||||||
episode = "{:02d}".format(data["episodeNumber"])
|
|
||||||
if int(episode) == 0:
|
|
||||||
episode = None
|
|
||||||
if episode is not None and season is None:
|
|
||||||
# Missing season, happens for some barnkanalen shows assume first and only
|
|
||||||
season = "01"
|
|
||||||
return season, episode
|
return season, episode
|
||||||
|
|
||||||
def extrametadata(self, data):
|
episodeid = data["{}:{}".format(self.type_name, self.visibleid)]["episode"]["id"]
|
||||||
|
if "positionInSeason" not in data[episodeid]:
|
||||||
|
return season, episode
|
||||||
|
|
||||||
|
match = re.search(r"Säsong (\d+) — Avsnitt (\d+)", data[episodeid]["positionInSeason"])
|
||||||
|
if not match:
|
||||||
|
return season, episode
|
||||||
|
|
||||||
|
season = "{:02d}".format(match.group(1))
|
||||||
|
episode = "{:02d}".format(match.group(2))
|
||||||
|
|
||||||
|
return season, episode
|
||||||
|
|
||||||
|
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,10 +309,7 @@ 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:
|
|
||||||
try:
|
|
||||||
t = data["video"]["thumbnail"]
|
|
||||||
except KeyError:
|
except KeyError:
|
||||||
t = ""
|
t = ""
|
||||||
if isinstance(t, dict):
|
if isinstance(t, dict):
|
||||||
@ -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