1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-24 12:15:40 +01:00
svtplay-dl/lib/svtplay_dl/service/barnkanalen.py

97 lines
3.5 KiB
Python
Raw Normal View History

# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
import json
2018-11-18 12:47:19 +01:00
import logging
2019-08-25 00:40:39 +02:00
import re
from urllib.parse import urlparse
from svtplay_dl.error import ServiceError
2019-08-25 00:40:39 +02:00
from svtplay_dl.service.svtplay import Svtplay
class Barnkanalen(Svtplay):
2019-08-25 00:27:31 +02:00
supported_domains = ["svt.se"]
supported_path = "/barnkanalen"
info_search_expr = r"<script id=\"__NEXT_DATA__\" type=\"application\/json\">({.+})<\/script>"
@classmethod
def handles(cls, url):
urlp = urlparse(url)
correctpath = urlp.path.startswith(cls.supported_path)
if urlp.netloc in cls.supported_domains and correctpath:
return True
# For every listed domain, try with www. subdomain as well.
2019-08-25 00:27:31 +02:00
if urlp.netloc in ["www." + x for x in cls.supported_domains] and correctpath:
return True
return False
def get(self):
data = self.get_urldata()
match = re.search(self.info_search_expr, data)
if not match:
yield ServiceError("Can't find video info.")
return
janson = json.loads(match.group(1))
if "episodeId" not in janson["query"]:
yield ServiceError("need a url with one to be able to download")
return
vid = janson["query"]["episodeId"]
title = janson["props"]["pageProps"]["initialState"]["featuredTitle"]["name"]
seasonnr = None
episodenr = None
episodename = None
for season in janson["props"]["pageProps"]["initialState"]["featuredTitle"]["associatedContent"]:
tmp_season = season["name"]
for episode in season["items"]:
if "variants" in episode["item"]:
svtID = episode["item"]["variants"][0]
else:
svtID = episode["item"]
if vid == svtID["svtId"]:
match = re.search(r"S.song (\d+)", tmp_season)
if match:
seasonnr = match.group(1)
match = re.search(r"^(\d+)\. (.*)$", episode["item"]["name"])
if match:
episodenr = match.group(1)
episodename = match.group(2)
else:
episodename = episode["item"]["name"]
break
self.output["title"] = title
self.output["id"] = vid.lower()
self.output["season"] = seasonnr
self.output["episode"] = episodenr
self.output["episodename"] = episodename
2021-02-28 22:05:15 +01:00
res = self.http.get(f"https://api.svt.se/video/{vid}")
janson = res.json()
yield from self._get_video(janson)
def find_all_episodes(self, config):
2021-12-18 20:18:25 +01:00
episodes = []
data = self.get_urldata()
match = re.search(self.info_search_expr, data)
if not match:
logging.error("Can't find video info.")
2021-12-18 20:18:25 +01:00
return episodes
janson = json.loads(match.group(1))
title = janson["query"]["titleSlug"]
2021-12-18 20:18:25 +01:00
for season in janson["props"]["pageProps"]["initialState"]["featuredTitle"]["associatedContent"]:
for episode in season["items"]:
if "variants" in episode["item"]:
svtID = episode["item"]["variants"][0]
else:
svtID = episode["item"]
2021-12-18 19:52:08 +01:00
episodes.append(f"https://www.svt.se/barnkanalen/barnplay/{title}/{svtID['svtId']}/")
if config.get("all_last") > 0:
2019-08-25 00:27:31 +02:00
return episodes[-config.get("all_last") :]
2018-01-09 21:14:01 +01:00
return episodes