2018-01-08 20:38:20 +01:00
|
|
|
# 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
|
2018-01-08 20:38:20 +01:00
|
|
|
|
|
|
|
from svtplay_dl.error import ServiceError
|
2019-08-25 00:40:39 +02:00
|
|
|
from svtplay_dl.service.svtplay import Svtplay
|
2018-01-08 20:38:20 +01:00
|
|
|
|
|
|
|
|
2018-01-08 22:06:17 +01:00
|
|
|
class Barnkanalen(Svtplay):
|
2019-08-25 00:27:31 +02:00
|
|
|
supported_domains = ["svt.se"]
|
2018-01-08 20:38:20 +01:00
|
|
|
supported_path = "/barnkanalen"
|
2021-02-20 14:01:05 +01:00
|
|
|
info_search_expr = r"<script id=\"__NEXT_DATA__\" type=\"application\/json\">({.+})<\/script>"
|
2018-01-08 20:38:20 +01:00
|
|
|
|
|
|
|
@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:
|
2018-01-08 20:38:20 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get(self):
|
2021-02-20 14:01:05 +01:00
|
|
|
data = self.get_urldata()
|
|
|
|
match = re.search(self.info_search_expr, data)
|
2018-01-08 20:38:20 +01:00
|
|
|
if not match:
|
|
|
|
yield ServiceError("Can't find video info.")
|
|
|
|
return
|
|
|
|
|
2021-02-20 14:01:05 +01:00
|
|
|
janson = json.loads(match.group(1))
|
|
|
|
if "episodeId" not in janson["query"]:
|
|
|
|
yield ServiceError("need a url with one to be able to download")
|
2018-01-08 20:38:20 +01:00
|
|
|
return
|
|
|
|
|
2021-02-20 14:01:05 +01:00
|
|
|
vid = janson["query"]["episodeId"]
|
|
|
|
title = janson["props"]["pageProps"]["initialState"]["gridContent"]["featuredContent"]["name"]
|
|
|
|
seasonnr = None
|
|
|
|
episodenr = None
|
|
|
|
episodename = None
|
|
|
|
for season in janson["props"]["pageProps"]["initialState"]["gridContent"]["featuredContent"]["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}")
|
2021-02-20 14:01:05 +01:00
|
|
|
janson = res.json()
|
2018-01-08 20:38:20 +01:00
|
|
|
videos = self._get_video(janson)
|
2019-08-25 00:33:51 +02:00
|
|
|
yield from videos
|
2018-01-08 20:38:20 +01:00
|
|
|
|
2018-07-19 15:55:49 +02:00
|
|
|
def find_all_episodes(self, config):
|
2021-02-20 14:01:05 +01:00
|
|
|
data = self.get_urldata()
|
|
|
|
match = re.search(self.info_search_expr, data)
|
2018-01-08 20:38:20 +01:00
|
|
|
if not match:
|
2021-02-20 14:01:05 +01:00
|
|
|
logging.error("Can't find video info.")
|
2018-01-08 20:38:20 +01:00
|
|
|
return
|
|
|
|
|
2021-02-20 14:01:05 +01:00
|
|
|
janson = json.loads(match.group(1))
|
|
|
|
title = janson["query"]["titleSlug"]
|
|
|
|
episodes = []
|
|
|
|
for season in janson["props"]["pageProps"]["initialState"]["gridContent"]["featuredContent"]["associatedContent"]:
|
|
|
|
for episode in season["items"]:
|
|
|
|
if "variants" in episode["item"]:
|
|
|
|
svtID = episode["item"]["variants"][0]
|
|
|
|
else:
|
|
|
|
svtID = episode["item"]
|
|
|
|
episodes.append("https://www.svt.se/barnkanalen/barnplay/{}/{}/".format(title, svtID["svtId"]))
|
2018-07-19 15:55:49 +02:00
|
|
|
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
|