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

167 lines
6.8 KiB
Python
Raw Normal View History

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
2017-09-16 23:45:29 +02:00
from svtplay_dl.error import ServiceError
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 Service
2017-09-16 23:45:29 +02:00
2018-01-30 20:11:37 +01:00
2017-09-16 23:45:29 +02:00
class Cmore(Service):
2019-08-25 00:27:31 +02:00
supported_domains = ["www.cmore.se", "www.cmore.dk", "www.cmore.no", "www.cmore.fi"]
2017-09-16 23:45:29 +02:00
def get(self):
2018-05-13 13:06:45 +02:00
if not self.config.get("username") or not self.config.get("password"):
yield ServiceError("You need username and password to download things from this site.")
return
token, message = self._login()
2017-09-16 23:45:29 +02:00
if not token:
yield ServiceError(message)
2017-09-16 23:45:29 +02:00
return
2018-10-13 13:55:25 +02:00
vid = self._get_vid()
if not vid:
2017-09-16 23:45:29 +02:00
yield ServiceError("Can't find video id")
return
tld = self._gettld()
2018-10-13 13:55:25 +02:00
self.output["id"] = vid
2021-12-18 19:52:08 +01:00
metaurl = f"https://playback-api.b17g.net/asset/{self.output['id']}?service=cmore.{tld}&device=browser&drm=widevine&protocol=dash%2Chls"
2018-10-13 13:55:25 +02:00
res = self.http.get(metaurl)
2017-09-16 23:45:29 +02:00
janson = res.json()
2018-10-13 13:55:25 +02:00
self._autoname(janson)
if janson["metadata"]["isDrmProtected"]:
yield ServiceError("Can't play this because the video got drm.")
return
2017-09-16 23:45:29 +02:00
2021-12-18 19:52:08 +01:00
url = f"https://playback-api.b17g.net/media/{self.output['id']}?service=cmore.{tld}&device=browser&protocol=hls%2Cdash&drm=widevine"
2021-02-28 22:05:15 +01:00
res = self.http.request("get", url, cookies=self.cookies, headers={"authorization": f"Bearer {token}"})
2018-10-13 13:55:25 +02:00
if res.status_code > 200:
yield ServiceError("Can't play this because the video is geoblocked.")
2017-09-16 23:45:29 +02:00
return
jansson = res.json()
if jansson["playbackItem"]["type"] == "hls":
yield from hlsparse(
2019-08-25 00:27:31 +02:00
self.config,
self.http.request("get", jansson["playbackItem"]["manifestUrl"]),
jansson["playbackItem"]["manifestUrl"],
2019-08-25 00:27:31 +02:00
output=self.output,
httpobject=self.http,
)
yield from dashparse(
self.config,
self.http.request("get", jansson["playbackItem"]["manifestUrl"].replace(".m3u8", ".mpd")),
jansson["playbackItem"]["manifestUrl"].replace(".m3u8", ".mpd"),
output=self.output,
httpobject=self.http,
2019-08-25 00:27:31 +02:00
)
2017-09-16 23:45:29 +02:00
2018-05-13 13:06:45 +02:00
def find_all_episodes(self, config):
2017-09-16 23:45:29 +02:00
episodes = []
seasons = []
2017-09-16 23:45:29 +02:00
token, message = self._login()
if not token:
2018-11-18 12:47:19 +01:00
logging.error(message)
2021-12-18 21:37:09 +01:00
return episodes
parse = urlparse(self.url)
url = f"https://www.cmore.{self._gettld()}/page-data{parse.path}/page-data.json"
res = self.http.get(url)
if res.status_code > 400:
logging.warning("Bad url? it only work with series")
2021-12-18 21:37:09 +01:00
return episodes
janson = res.json()
if "mutualSeasonData" in janson["result"]["pageContext"]:
path = janson["result"]["pageContext"]["mutualSeasonData"]["serieRelativeUrl"]
seasons = janson["result"]["pageContext"]["mutualSeasonData"]["seasonNumbers"]
elif "mutualEpisodeData" in janson["result"]["pageContext"]:
path = janson["result"]["pageContext"]["mutualEpisodeData"]["serieRelativeUrl"]
seasons = janson["result"]["pageContext"]["mutualEpisodeData"]["seasonNumbers"]
elif "serie" in janson["result"]["pageContext"]:
path = janson["result"]["pageContext"]["serie"]["relativeUrl"]
seasons = janson["result"]["pageContext"]["serie"]["seasonNumbers"]
else:
logging.warning("Can't find info needed to find episodes")
for season in seasons:
url = f"https://www.cmore.se/page-data{path}/sasong-{season}/page-data.json"
res = self.http.get(url)
janson = res.json()
for episode in janson["result"]["pageContext"]["season"]["selectedEpisodes"]:
episode_url = f'https://www.cmore.{self._gettld()}{episode["relativeUrl"]}'
if episode_url not in episodes:
episodes.append(episode_url)
2017-09-16 23:45:29 +02:00
2018-05-13 13:06:45 +02:00
if config.get("all_last") > 0:
2019-08-25 00:27:31 +02:00
return sorted(episodes[-config.get("all_last") :])
2017-09-16 23:45:29 +02:00
return sorted(episodes)
def _gettld(self):
if isinstance(self.url, list):
parse = urlparse(self.url[0])
else:
parse = urlparse(self.url)
2019-08-25 00:27:31 +02:00
return re.search(r"\.(\w{2})$", parse.netloc).group(1)
2017-09-16 23:45:29 +02:00
def _login(self):
tld = self._gettld()
2018-05-22 00:02:20 +02:00
if self.config.get("cmoreoperator"):
2021-12-18 21:37:09 +01:00
url = (
f"https://tve.cmore.se/country/{tld}/operator/{self.config.get('cmoreoperator')}"
f"/user/{self.config.get('username')}/exists?client=cmore-web-prod"
)
2019-08-25 00:27:31 +02:00
post = {
"password": self.config.get("password"),
}
2017-09-19 00:30:11 +02:00
else:
2020-07-29 23:14:54 +02:00
url = "https://account-delta.b17g.services/api?client=cmore-web"
post = {
"query": "mutation($username: String, $password: String, $site: String) { login(credentials:"
"{username: $username, password: $password}, site: $site) { user { ...UserFields } session { token vimondToken } }} "
"fragment UserFields on User { acceptedCmoreTerms acceptedPlayTerms countryCode email firstName genericAds "
"lastName tv4UserDataComplete userId username yearOfBirth zipCode type}",
"variables": {"username": self.config.get("username"), "password": self.config.get("password"), "site": "CMORE_SE"},
}
res = self.http.post(url, json=post, cookies=self.cookies)
2017-09-19 00:30:11 +02:00
if res.status_code >= 400:
return None, "Wrong username or password"
2017-09-16 23:45:29 +02:00
janson = res.json()
2020-07-29 23:14:54 +02:00
token = janson["data"]["login"]["session"]["vimondToken"]
return token, None
2017-09-19 00:30:11 +02:00
def operatorlist(self):
2021-02-28 22:05:15 +01:00
res = self.http.get(f"https://tve.cmore.se/country/{self._gettld()}/operator?client=cmore-web-prod")
for i in res.json()["data"]["operators"]:
2021-12-18 19:52:08 +01:00
print(f"operator: '{i['name'].lower()}")
2018-10-13 13:55:25 +02:00
def _get_vid(self):
res = self.http.get(self.url)
match = re.search('data-asset-id="([^"]+)"', res.text)
if match:
return match.group(1)
parse = urlparse(self.url)
match = re.search(r"/(\d+)-[\w-]+$", parse.path)
if match:
return match.group(1)
return None
def _autoname(self, janson):
if "seriesTitle" in janson["metadata"]:
self.output["title"] = janson["metadata"]["seriesTitle"]
self.output["episodename"] = janson["metadata"]["episodeTitle"]
else:
self.output["title"] = janson["metadata"]["title"]
self.output["season"] = janson["metadata"]["seasonNumber"]
self.output["episode"] = janson["metadata"]["episodeNumber"]
self.config.set("live", janson["metadata"]["isLive"])