1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-30 23:24:16 +01:00
svtplay-dl/lib/svtplay_dl/service/urplay.py

111 lines
4.3 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 -*-
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 urllib.parse import urlparse
from svtplay_dl.error import ServiceError
2023-03-15 06:53:24 +01:00
from svtplay_dl.fetcher.dash import dashparse
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.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)
2023-03-15 06:53:24 +01:00
vid = jsondata["props"]["pageProps"]["program"]["id"]
2021-10-30 12:05:08 +02:00
jsondata = jsondata["props"]["pageProps"]["program"]
2023-03-15 06:53:24 +01:00
res = self.http.get(f"https://media-api.urplay.se/config-streaming/v1/urplay/sources/{vid}")
2023-03-15 06:53:24 +01:00
if "dash" in res.json()["sources"]:
yield from dashparse(
self.config,
self.http.request("get", res.json()["sources"]["dash"]),
res.json()["sources"]["dash"],
output=self.output,
)
if "hls" in res.json()["sources"]:
yield from hlsparse(self.config, self.http.request("get", res.json()["sources"]["hls"]), res.json()["sources"]["hls"], output=self.output)
2023-03-15 06:53:24 +01:00
self.outputfilename(jsondata, urldata)
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.")
2021-12-18 20:18:25 +01:00
return episodes
2024-07-28 23:50:14 +02:00
jsondata = json.loads(match.group(1))
2021-10-30 12:05:08 +02:00
seasons = jsondata["props"]["pageProps"]["superSeriesSeasons"]
2024-07-28 23:50:14 +02:00
build = jsondata["buildId"]
parse = urlparse(self.url)
url = f"https://{parse.netloc}{parse.path}"
episodes.append(url)
2021-10-30 12:05:08 +02:00
if seasons:
for season in seasons:
2024-07-28 23:50:14 +02:00
res = self.http.get(
f'https://urplay.se/_next/data/{build}{season["link"]}.json?productType={jsondata["query"]["productType"]}&id={jsondata["props"]["pageProps"]["program"]["seriesId"]}',
)
for episode in res.json()["pageProps"]["program"]["series"]["programs"]:
url = urljoin("https://urplay.se", episode["link"])
if url not in episodes:
episodes.append(url)
2021-10-30 12:05:08 +02:00
else:
for episode in jsondata["props"]["pageProps"]["accessibleEpisodes"]:
url = urljoin("https://urplay.se", episode["link"])
if url not in episodes:
episodes.append(url)
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"]
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"])])