2015-10-04 14:40:00 +02:00
|
|
|
# ex:ts=4:sw=4:sts=4:et
|
|
|
|
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
2021-02-14 16:59:21 +01:00
|
|
|
import datetime
|
2018-02-22 03:30:36 +01:00
|
|
|
import hashlib
|
2018-05-27 16:05:54 +02:00
|
|
|
import logging
|
2019-08-25 00:40:39 +02:00
|
|
|
import random
|
|
|
|
import re
|
2018-01-30 22:07:21 +01:00
|
|
|
from urllib.parse import urlparse
|
2015-10-04 14:40:00 +02:00
|
|
|
|
|
|
|
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 Service
|
2018-05-27 16:05:54 +02:00
|
|
|
from svtplay_dl.subtitle import subtitle
|
2015-10-04 14:40:00 +02:00
|
|
|
|
2018-01-30 20:11:37 +01:00
|
|
|
|
2018-05-27 16:04:13 +02:00
|
|
|
country = {"sv": ".se", "da": ".dk", "no": ".no"}
|
2021-01-18 21:44:05 +01:00
|
|
|
REALMS = {"discoveryplus.se": "dplayse", "discoveryplus.no": "dplayno", "discoveryplus.dk": "dplaydk"}
|
2018-05-27 16:04:13 +02:00
|
|
|
|
2018-05-27 16:10:22 +02:00
|
|
|
|
2015-10-04 14:40:00 +02:00
|
|
|
class Dplay(Service):
|
2021-01-18 21:44:05 +01:00
|
|
|
supported_domains = ["discoveryplus.se", "discoveryplus.no", "discoveryplus.dk"]
|
2021-02-14 16:59:21 +01:00
|
|
|
packages = []
|
2015-10-04 14:40:00 +02:00
|
|
|
|
2015-12-26 11:46:14 +01:00
|
|
|
def get(self):
|
2016-02-07 20:41:21 +01:00
|
|
|
parse = urlparse(self.url)
|
2021-01-18 21:44:05 +01:00
|
|
|
self.domain = re.search(r"(discoveryplus\.\w\w)", parse.netloc).group(1)
|
2015-10-04 14:40:00 +02:00
|
|
|
|
2018-02-22 03:30:36 +01:00
|
|
|
if not self._token():
|
2018-05-27 16:05:54 +02:00
|
|
|
logging.error("Something went wrong getting token for requests")
|
2015-10-29 22:42:27 +01:00
|
|
|
|
2020-09-15 23:44:08 +02:00
|
|
|
if not self._login():
|
2020-12-05 16:03:04 +01:00
|
|
|
yield ServiceError("You need the 'st' cookie from your web brower for the site to make it work")
|
2020-09-15 23:44:08 +02:00
|
|
|
return
|
2018-02-25 21:20:54 +01:00
|
|
|
|
|
|
|
channel = False
|
|
|
|
if "kanaler" in parse.path:
|
|
|
|
match = re.search("kanaler/([^/]+)$", parse.path)
|
2020-09-29 20:39:27 +02:00
|
|
|
if not match:
|
|
|
|
yield ServiceError("Can't detect 'kanaler'")
|
|
|
|
return
|
2021-04-27 19:44:09 +02:00
|
|
|
path = f"/channels/{match.group(1)}"
|
2021-02-28 22:05:15 +01:00
|
|
|
url = f"https://disco-api.{self.domain}/content{path}"
|
2018-02-25 21:20:54 +01:00
|
|
|
channel = True
|
2018-05-13 13:06:45 +02:00
|
|
|
self.config.set("live", True)
|
2018-03-04 15:06:47 +01:00
|
|
|
elif "program" in parse.path:
|
|
|
|
match = re.search("(programmer|program)/([^/]+)$", parse.path)
|
2020-09-29 20:39:27 +02:00
|
|
|
if not match:
|
|
|
|
yield ServiceError("Can't find program url")
|
|
|
|
return
|
2021-04-27 19:44:09 +02:00
|
|
|
path = f"/shows/{match.group(2)}"
|
2021-02-28 22:05:15 +01:00
|
|
|
url = f"https://disco-api.{self.domain}/content{path}"
|
2019-09-06 22:49:49 +02:00
|
|
|
res = self.http.get(url, headers={"x-disco-client": "WEB:UNKNOWN:dplay-client:0.0.1"})
|
2018-03-04 15:06:47 +01:00
|
|
|
programid = res.json()["data"]["id"]
|
2019-08-25 00:27:31 +02:00
|
|
|
qyerystring = (
|
|
|
|
"include=primaryChannel,show&filter[videoType]=EPISODE&filter[show.id]={}&"
|
2019-09-06 22:49:49 +02:00
|
|
|
"page[size]=100&sort=seasonNumber,episodeNumber,-earliestPlayableStart".format(programid)
|
2019-08-25 00:27:31 +02:00
|
|
|
)
|
2021-02-28 22:05:15 +01:00
|
|
|
res = self.http.get(f"https://disco-api.{self.domain}/content/videos?{qyerystring}")
|
2018-03-04 15:06:47 +01:00
|
|
|
janson = res.json()
|
|
|
|
vid = 0
|
|
|
|
slug = None
|
|
|
|
for i in janson["data"]:
|
|
|
|
if int(i["id"]) > vid:
|
|
|
|
vid = int(i["id"])
|
|
|
|
slug = i["attributes"]["path"]
|
|
|
|
if slug:
|
2021-02-28 22:05:15 +01:00
|
|
|
url = f"https://disco-api.{self.domain}/content/videos/{slug}"
|
2018-03-04 15:06:47 +01:00
|
|
|
else:
|
|
|
|
yield ServiceError("Cant find latest video on program url")
|
|
|
|
return
|
2018-02-25 21:20:54 +01:00
|
|
|
else:
|
2018-02-26 00:34:58 +01:00
|
|
|
match = re.search("(videos|videoer)/(.*)$", parse.path)
|
2021-04-27 19:44:09 +02:00
|
|
|
url = f"https://disco-api.{self.domain}/content/videos/{match.group(2)}"
|
2019-09-06 22:49:49 +02:00
|
|
|
res = self.http.get(url, headers={"x-disco-client": "WEB:UNKNOWN:dplay-client:0.0.1"})
|
2018-02-22 03:30:36 +01:00
|
|
|
janson = res.json()
|
2018-02-25 21:22:59 +01:00
|
|
|
if "errors" in janson:
|
|
|
|
yield ServiceError("Cant find any videos on this url")
|
|
|
|
return
|
2018-01-09 00:40:24 +01:00
|
|
|
|
2018-05-13 13:06:45 +02:00
|
|
|
if channel:
|
|
|
|
name = janson["data"]["attributes"]["name"]
|
|
|
|
self.output["title"] = name
|
|
|
|
else:
|
|
|
|
name = self._autoname(janson)
|
|
|
|
if name is None:
|
|
|
|
yield ServiceError("Cant find vid id for autonaming")
|
2018-02-24 20:59:23 +01:00
|
|
|
return
|
2018-05-13 13:06:45 +02:00
|
|
|
self.output["id"] = janson["data"]["id"]
|
2018-02-24 20:59:23 +01:00
|
|
|
|
2020-10-10 12:57:27 +02:00
|
|
|
api = "https://disco-api.{}/playback/videoPlaybackInfo/{}?usePreAuth=true".format(self.domain, janson["data"]["id"])
|
2018-02-22 03:30:36 +01:00
|
|
|
res = self.http.get(api)
|
|
|
|
if res.status_code > 400:
|
2018-02-25 21:20:54 +01:00
|
|
|
yield ServiceError("You dont have permission to watch this")
|
2016-02-07 20:43:00 +01:00
|
|
|
return
|
2019-08-25 00:27:31 +02:00
|
|
|
streams = hlsparse(
|
|
|
|
self.config,
|
2019-09-06 22:49:49 +02:00
|
|
|
self.http.request("get", res.json()["data"]["attributes"]["streaming"]["hls"]["url"]),
|
2019-08-25 00:27:31 +02:00
|
|
|
res.json()["data"]["attributes"]["streaming"]["hls"]["url"],
|
|
|
|
httpobject=self.http,
|
|
|
|
output=self.output,
|
|
|
|
)
|
2018-05-08 22:48:55 +02:00
|
|
|
for n in list(streams.keys()):
|
2019-09-06 22:49:49 +02:00
|
|
|
if isinstance(streams[n], subtitle): # we get the subtitles from the hls playlist.
|
2018-05-27 16:04:13 +02:00
|
|
|
if self.config.get("get_all_subtitles"):
|
|
|
|
yield streams[n]
|
|
|
|
else:
|
2019-09-06 22:49:49 +02:00
|
|
|
if streams[n].subfix in country and country[streams[n].subfix] in self.domain:
|
2018-05-27 16:04:13 +02:00
|
|
|
yield streams[n]
|
|
|
|
else:
|
|
|
|
yield streams[n]
|
2015-10-29 19:32:38 +01:00
|
|
|
|
|
|
|
def _autoname(self, jsondata):
|
2019-08-25 00:27:31 +02:00
|
|
|
match = re.search("^([^/]+)/", jsondata["data"]["attributes"]["path"])
|
2018-05-13 13:06:45 +02:00
|
|
|
self.output["title"] = match.group(1)
|
|
|
|
self.output["season"] = int(jsondata["data"]["attributes"]["seasonNumber"])
|
|
|
|
self.output["episode"] = int(jsondata["data"]["attributes"]["episodeNumber"])
|
|
|
|
self.output["episodename"] = jsondata["data"]["attributes"]["name"]
|
|
|
|
return self.output["title"]
|
2015-10-30 00:29:48 +01:00
|
|
|
|
2018-05-08 22:46:11 +02:00
|
|
|
def find_all_episodes(self, config):
|
2016-02-07 20:41:21 +01:00
|
|
|
parse = urlparse(self.url)
|
2021-01-18 21:44:05 +01:00
|
|
|
self.domain = re.search(r"(discoveryplus\.\w\w)", parse.netloc).group(1)
|
2020-09-15 23:44:08 +02:00
|
|
|
programid = None
|
|
|
|
seasons = []
|
2021-02-10 20:41:41 +01:00
|
|
|
episodes = []
|
2021-02-10 20:45:00 +01:00
|
|
|
|
2018-02-26 00:34:58 +01:00
|
|
|
match = re.search("^/(program|programmer|videos|videoer)/([^/]+)", parse.path)
|
2015-10-30 00:29:48 +01:00
|
|
|
if not match:
|
2018-05-27 16:05:54 +02:00
|
|
|
logging.error("Can't find show name")
|
2015-10-30 00:29:48 +01:00
|
|
|
return None
|
|
|
|
|
2020-09-15 23:44:08 +02:00
|
|
|
if not self._login():
|
|
|
|
logging.error("Need the 'st' cookie to work")
|
|
|
|
return None
|
|
|
|
|
2018-02-22 03:30:36 +01:00
|
|
|
if not self._token():
|
2018-05-27 16:05:54 +02:00
|
|
|
logging.error("Something went wrong getting token for requests")
|
2015-10-30 00:29:48 +01:00
|
|
|
|
2021-02-14 16:59:21 +01:00
|
|
|
self._getpackages()
|
2020-10-31 17:15:15 +01:00
|
|
|
|
2020-11-19 22:21:52 +01:00
|
|
|
urllocal = ""
|
2021-02-20 16:08:44 +01:00
|
|
|
if self.domain in ["discoveryplus.no", "discoveryplus.dk"]:
|
2020-11-19 22:21:52 +01:00
|
|
|
urllocal = "mer"
|
2020-10-31 17:15:15 +01:00
|
|
|
|
2021-04-27 19:44:09 +02:00
|
|
|
url = f"http://disco-api.{self.domain}/cms/routes/program{urllocal}/{match.group(2)}?decorators=viewingHistory&include=default"
|
2018-02-22 03:30:36 +01:00
|
|
|
res = self.http.get(url)
|
2021-02-10 20:41:41 +01:00
|
|
|
if res.status_code > 400:
|
|
|
|
logging.error("Cant find any videos. wrong url?")
|
|
|
|
return episodes
|
2021-02-10 20:45:00 +01:00
|
|
|
|
|
|
|
showid = None
|
2020-09-15 23:44:08 +02:00
|
|
|
for what in res.json()["included"]:
|
2021-04-22 19:58:17 +02:00
|
|
|
if "attributes" in what and "component" in what["attributes"] and what["attributes"]["component"]["id"] == "tabbed-content":
|
2020-09-15 23:44:08 +02:00
|
|
|
programid = what["id"]
|
|
|
|
for ses in what["attributes"]["component"]["filters"]:
|
|
|
|
if ses["id"] == "seasonNumber":
|
|
|
|
for opt in ses["options"]:
|
2021-04-18 16:11:14 +02:00
|
|
|
if "value" not in opt:
|
|
|
|
continue
|
2020-09-15 23:44:08 +02:00
|
|
|
seasons.append(opt["value"])
|
2021-02-10 21:47:43 +01:00
|
|
|
if "mandatoryParams" in what["attributes"]["component"]:
|
|
|
|
showid = what["attributes"]["component"]["mandatoryParams"]
|
2020-09-15 23:44:08 +02:00
|
|
|
|
|
|
|
if programid:
|
|
|
|
for season in seasons:
|
|
|
|
page = 1
|
|
|
|
totalpages = 1
|
|
|
|
while page <= totalpages:
|
2021-02-10 21:47:43 +01:00
|
|
|
querystring = "decorators=viewingHistory&include=default&page[items.number]={}&pf[seasonNumber]={}".format(
|
2021-02-10 20:45:00 +01:00
|
|
|
page,
|
|
|
|
season,
|
|
|
|
)
|
2021-02-10 21:47:43 +01:00
|
|
|
if showid:
|
2021-02-28 22:05:15 +01:00
|
|
|
querystring += f"&{showid}"
|
|
|
|
res = self.http.get(f"https://disco-api.{self.domain}/cms/collections/{programid}?{querystring}")
|
2020-09-15 23:44:08 +02:00
|
|
|
janson = res.json()
|
|
|
|
totalpages = janson["data"]["meta"]["itemsTotalPages"]
|
|
|
|
for i in janson["included"]:
|
|
|
|
if i["type"] != "video":
|
|
|
|
continue
|
|
|
|
if i["attributes"]["videoType"] == "EPISODE":
|
2021-02-14 16:59:21 +01:00
|
|
|
if not self._playablefile(i["attributes"]["availabilityWindows"]):
|
2020-09-15 23:44:08 +02:00
|
|
|
continue
|
|
|
|
episodes.append("https://www.{}/videos/{}".format(self.domain, i["attributes"]["path"]))
|
|
|
|
page += 1
|
|
|
|
if not episodes:
|
2018-05-27 16:05:54 +02:00
|
|
|
logging.error("Cant find any playable files")
|
2018-05-08 22:46:11 +02:00
|
|
|
if config.get("all_last") > 0:
|
2019-08-25 00:27:31 +02:00
|
|
|
return episodes[: config.get("all_last")]
|
2015-10-30 00:29:48 +01:00
|
|
|
return episodes
|
2018-02-22 03:30:36 +01:00
|
|
|
|
2018-02-25 21:20:54 +01:00
|
|
|
def _login(self):
|
2021-02-28 22:05:15 +01:00
|
|
|
res = self.http.get(f"https://disco-api.{self.domain}/users/me", headers={"authority": f"disco-api.{self.domain}"})
|
2020-12-05 16:44:10 +01:00
|
|
|
if res.status_code >= 400:
|
|
|
|
return False
|
2020-09-15 23:44:08 +02:00
|
|
|
if not res.json()["data"]["attributes"]["anonymous"]:
|
|
|
|
return True
|
|
|
|
return False
|
2018-02-22 03:30:36 +01:00
|
|
|
|
2020-09-15 23:44:08 +02:00
|
|
|
def _token(self) -> bool:
|
2018-02-22 03:30:36 +01:00
|
|
|
# random device id for cookietoken
|
2018-02-26 00:05:21 +01:00
|
|
|
deviceid = hashlib.sha256(bytes(int(random.random() * 1000))).hexdigest()
|
2021-04-27 19:44:09 +02:00
|
|
|
url = f"https://disco-api.{self.domain}/token?realm={REALMS[self.domain]}&deviceId={deviceid}&shortlived=true"
|
2018-02-22 03:30:36 +01:00
|
|
|
res = self.http.get(url)
|
2019-10-19 15:22:53 +02:00
|
|
|
if res.status_code >= 400:
|
2018-02-22 03:30:36 +01:00
|
|
|
return False
|
|
|
|
return True
|
2020-09-15 23:44:08 +02:00
|
|
|
|
2021-02-14 16:59:21 +01:00
|
|
|
def _getpackages(self):
|
2021-02-28 22:05:15 +01:00
|
|
|
res = self.http.get(f"https://disco-api.{self.domain}/users/me", headers={"authority": f"disco-api.{self.domain}"})
|
2020-09-15 23:44:08 +02:00
|
|
|
if res.status_code < 400:
|
2021-02-14 16:59:21 +01:00
|
|
|
self.packages.extend(res.json()["data"]["attributes"]["packages"])
|
|
|
|
|
|
|
|
def _playablefile(self, needs):
|
|
|
|
playable = False
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
for package in self.packages:
|
|
|
|
for need in needs:
|
|
|
|
if package != need["package"]:
|
|
|
|
continue
|
|
|
|
start = datetime.datetime.strptime(need["playableStart"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=None)
|
|
|
|
if now > start:
|
|
|
|
if "playableEnd" in need:
|
|
|
|
end = datetime.datetime.strptime(need["playableEnd"], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=None)
|
|
|
|
if now < end:
|
|
|
|
playable = True
|
|
|
|
else:
|
|
|
|
playable = True
|
|
|
|
return playable
|