diff --git a/lib/svtplay_dl/service/svtplay.py b/lib/svtplay_dl/service/svtplay.py index aaa5d02..1cb80b2 100644 --- a/lib/svtplay_dl/service/svtplay.py +++ b/lib/svtplay_dl/service/svtplay.py @@ -5,19 +5,19 @@ import re import copy import json import hashlib +import datetime import logging from urllib.parse import urljoin, urlparse, parse_qs from operator import itemgetter from svtplay_dl.service import Service, MetadataThumbMixin 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.dash import dashparse from svtplay_dl.subtitle import subtitle 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): @@ -63,28 +63,24 @@ class Svtplay(Service, MetadataThumbMixin): return janson = json.loads(match.group(1))["videoPage"] - if "programTitle" not in janson["video"]: - yield ServiceError("Can't find any video on that page.") + self.visibleid = list(janson['visible'].keys())[0] + match = re.search(r"__svtplay_apollo'] = ({.*});", urldata) + if not match: + yield ServiceError("Can't find video info.") 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: - yield ServiceError("Can't find video info.") - return - janson = json.loads(match.group(1))["videoPage"] + janson = json.loads(match.group(1)) + for key in janson["ROOT_QUERY"].keys(): + if "listablesByEsceni" in key: + esceni = key + break - self.outputfilename(janson["video"]) - self.extrametadata(janson) + 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) try: janson = res.json() @@ -119,14 +115,6 @@ class Svtplay(Service, MetadataThumbMixin): if alt: 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": streams = dashparse(self.config, self.http.request("get", i["url"]), i["url"], output=self.output) if alt: @@ -155,7 +143,7 @@ class Svtplay(Service, MetadataThumbMixin): for i in dataj["gridPage"]["content"]: videos.append(i["contentUrl"]) page += 1 - self._last_chance(videos, page, pages) + videos.extend(self._last_chance(videos, page, pages)) return videos def _genre(self, jansson): @@ -178,34 +166,53 @@ class Svtplay(Service, MetadataThumbMixin): videos = [] 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): 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: - query = parse_qs(parse.query) - if "tab" in query: - tab = query["tab"][0] + match = re.search(r"__svtplay'] = ({.*});", self.get_urldata()) + if not match: + logging.error("Can't find video info.") + return 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\"]})"] - if dataj["relatedVideoContent"]: - items = dataj["relatedVideoContent"]["relatedVideosAccordion"] - for i in items: - if tab: - if i["slug"] == tab: - videos = self.videos_to_list(i["videos"], videos) - else: - if "klipp" not in i["slug"] and "kommande" not in i["slug"]: - videos = self.videos_to_list(i["videos"], videos) - if self.config.get("include_clips"): - if i["slug"] == "klipp": - videos = self.videos_to_list(i["videos"], videos) + 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] @@ -231,16 +238,11 @@ class Svtplay(Service, MetadataThumbMixin): def outputfilename(self, data): name = None desc = None - if "programTitle" in data and data["programTitle"]: - name = filenamify(data["programTitle"]) - elif "titleSlug" in data and data["titleSlug"]: - name = filenamify(data["titleSlug"]) - other = data["title"] + pid = data["{}:{}".format(self.type_name, self.visibleid)]["parent"]["id"] - if "programVersionId" in data: - vid = str(data["programVersionId"]) - else: - vid = str(data["id"]) + name = data[pid]["slug"] + other = data["{}:{}".format(self.type_name, self.visibleid)]["slug"] + vid = data["{}:{}".format(self.type_name, self.visibleid)]["id"] id = hashlib.sha256(vid.encode("utf-8")).hexdigest()[:7] if name == other: @@ -250,10 +252,10 @@ class Svtplay(Service, MetadataThumbMixin): other = None season, episode = self.seasoninfo(data) - if "accessService" in data: - if data["accessService"] == "audioDescription": + if "accessibility" in data["{}:{}".format(self.type_name, self.visibleid)]: + if data["{}:{}".format(self.type_name, self.visibleid)]["accessibility"] == "AudioDescribed": desc = "syntolkat" - if data["accessService"] == "signInterpretation": + if data["{}:{}".format(self.type_name, self.visibleid)]["accessibility"] == "SignInterpreted": desc = "teckentolkat" if not other: @@ -261,7 +263,7 @@ class Svtplay(Service, MetadataThumbMixin): elif desc: other += "-{}".format(desc) - self.output["title"] = name + self.output["title"] = filenamify(name) self.output["id"] = id self.output["season"] = season self.output["episode"] = episode @@ -269,34 +271,34 @@ class Svtplay(Service, MetadataThumbMixin): def seasoninfo(self, data): season, episode = None, None - if "season" in data and data["season"]: - season = "{:02d}".format(data["season"]) - 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" + + if "episode" not in data["{}:{}".format(self.type_name, self.visibleid)]: + return season, episode + + 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): + 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) - try: - self.output["publishing_datetime"] = data["video"]["broadcastDate"] / 1000 - except KeyError: - pass - try: - title = data["video"]["programTitle"] - self.output["title_nice"] = title - except KeyError: - title = data["video"]["titleSlug"] - self.output["title_nice"] = title + if "validFrom" in episode: + self.output["publishing_datetime"] = int(datetime.datetime.strptime(episode["validFrom"], "%Y-%m-%dT%H:%M:%S%z").strftime('%s')) + + self.output["title_nice"] = data[data["{}:{}".format(type_name, visibleid)]["parent"]["id"]]["name"] try: - t = data['state']["titleModel"]["thumbnail"] + t = data[data[episode["parent"]["id"]]["image"]["id"]] except KeyError: t = "" if isinstance(t, dict): @@ -307,12 +309,9 @@ class Svtplay(Service, MetadataThumbMixin): url = t.format(format="large") self.output["showthumbnailurl"] = url try: - t = data["video"]["thumbnailXL"] + t = data[episode["image"]["id"]] except KeyError: - try: - t = data["video"]["thumbnail"] - except KeyError: - t = "" + t = "" if isinstance(t, dict): url = "https://www.svtstatic.se/image/original/default/{id}/{changed}?format=auto&quality=100".format(**t) 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 url = t.format(format="large") self.output["episodethumbnailurl"] = url - try: - self.output["showdescription"] = data['state']["titleModel"]["description"] - except KeyError: - pass - try: - self.output["episodedescription"] = data["video"]["description"] - except KeyError: - pass + + if "longDescription" in data[episode["parent"]["id"]]: + self.output["showdescription"] = data[episode["parent"]["id"]]["longDescription"] + + if "longDescription" in episode: + self.output["episodedescription"] = episode["longDescription"]