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/urplay.py

110 lines
4.7 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 -*-
2014-06-07 20:43:40 +02:00
import copy
2019-08-25 00:40:39 +02:00
import json
2018-11-18 12:47:19 +01:00
import logging
2019-08-25 00:40:39 +02:00
import re
from html import unescape
2021-10-30 12:05:08 +02:00
from urllib.parse import urljoin
from svtplay_dl.error import ServiceError
2019-08-25 00:40:39 +02:00
from svtplay_dl.fetcher.hls import hlsparse
from svtplay_dl.service import OpenGraphThumbMixin
from svtplay_dl.service import Service
from svtplay_dl.subtitle import subtitle
from svtplay_dl.utils.http import download_thumbnails
2015-09-15 20:10:32 +02:00
class Urplay(Service, OpenGraphThumbMixin):
2019-08-25 00:27:31 +02:00
supported_domains = ["urplay.se", "ur.se", "betaplay.ur.se", "urskola.se"]
def get(self):
urldata = self.get_urldata()
2021-10-30 12:05:08 +02:00
match = re.search(r"__NEXT_DATA__\" type=\"application\/json\">({.+})<\/script>", urldata)
if not match:
2021-10-30 12:05:08 +02:00
yield ServiceError("Can't find video info.")
return
data = unescape(match.group(1))
jsondata = json.loads(data)
res = self.http.get("https://streaming-loadbalancer.ur.se/loadbalancer.json")
loadbalancer = res.json()["redirect"]
2021-10-30 12:05:08 +02:00
jsondata = jsondata["props"]["pageProps"]["program"]
2021-10-30 12:05:08 +02:00
self.outputfilename(jsondata, urldata)
2021-10-30 12:05:08 +02:00
for streaminfo in jsondata["streamingInfo"].keys():
stream = jsondata["streamingInfo"][streaminfo]
if streaminfo == "raw":
if "sd" in stream:
url = "https://{}/{}playlist.m3u8".format(loadbalancer, stream["sd"]["location"])
yield from hlsparse(self.config, self.http.request("get", url), url, output=self.output)
if "hd" in stream:
url = "https://{}/{}playlist.m3u8".format(loadbalancer, stream["hd"]["location"])
yield from hlsparse(self.config, self.http.request("get", url), url, output=self.output)
if not (self.config.get("get_all_subtitles")) and streaminfo == "sweComplete":
yield subtitle(copy.copy(self.config), "wrst", stream["tt"]["location"].replace(".tt", ".vtt"), output=self.output)
if self.config.get("get_all_subtitles") and "tt" in stream:
label = stream["tt"]["language"]
if stream["tt"]["scope"] != "complete":
label = "{}-{}".format(label, stream["tt"]["scope"])
yield subtitle(copy.copy(self.config), "wrst", stream["tt"]["location"].replace(".tt", ".vtt"), label, output=copy.copy(self.output))
2018-05-13 13:06:45 +02:00
def find_all_episodes(self, config):
episodes = []
2021-10-30 12:05:08 +02:00
match = re.search(r"__NEXT_DATA__\" type=\"application\/json\">({.+})<\/script>", self.get_urldata())
if not match:
2021-10-30 12:05:08 +02:00
logging.error("Can't find video info.")
return
data = unescape(match.group(1))
jsondata = json.loads(data)
2021-10-30 12:05:08 +02:00
seasons = jsondata["props"]["pageProps"]["superSeriesSeasons"]
if seasons:
for season in seasons:
res = self.http.get(f'https://urplay.se/api/v1/series?id={season["id"]}')
for episode in res.json()["programs"]:
episodes.append(urljoin("https://urplay.se", episode["link"]))
else:
for episode in jsondata["props"]["pageProps"]["accessibleEpisodes"]:
episodes.append(urljoin("https://urplay.se", episode["link"]))
episodes_new = []
n = 0
for i in episodes:
2018-05-13 13:06:45 +02:00
if n == config.get("all_last"):
break
2015-09-06 22:41:49 +02:00
if i not in episodes_new:
episodes_new.append(i)
n += 1
return episodes_new
def outputfilename(self, data, urldata):
if "seriesTitle" in data:
self.output["title"] = data["seriesTitle"]
self.output["title_nice"] = data["seriesTitle"]
if "episodeNumber" in data and data["episodeNumber"]:
self.output["episode"] = str(data["episodeNumber"])
if "title" in data:
if self.output["title"] is None:
self.output["title"] = data["title"]
else:
self.output["episodename"] = data["title"]
if "id" in data and data["id"]:
self.output["id"] = str(data["id"])
self.output["episodethumbnailurl"] = data["image"]["1280x720"]
# Try to match Season info from HTML (not available in json, it seems), e.g.: <button class="SeasonsDropdown-module__seasonButton___25Uyt" type="button"><span>Säsong 6</span>
2021-10-30 12:05:08 +02:00
seasonmatch = re.search(r"data-testid=\"season-name-label\">S.song (\d+)...<\/span", urldata)
if seasonmatch:
self.output["season"] = seasonmatch.group(1)
else:
self.output["season"] = "1" # No season info - probably show without seasons
def get_thumbnail(self, options):
download_thumbnails(self.output, options, [(False, self.output["episodethumbnailurl"])])