1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-28 06:04:17 +01:00
svtplay-dl/lib/svtplay_dl/service/cmore.py

137 lines
5.3 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 urljoin
from urllib.parse import urlparse
2017-09-16 23:45:29 +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
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
metaurl = "https://playback-api.b17g.net/asset/{}?service=cmore.{}" "&device=browser&drm=widevine&protocol=dash%2Chls".format(
self.output["id"], tld
2019-08-25 00:27:31 +02:00
)
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
url = "https://playback-api.b17g.net/media/{}?service=cmore.{}&device=browser&protocol=hls%2Cdash&drm=widevine".format(self.output["id"], tld)
res = self.http.request("get", url, cookies=self.cookies, headers={"authorization": "Bearer {}".format(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
2018-10-13 13:55:25 +02:00
if res.json()["playbackItem"]["type"] == "hls":
2019-08-25 00:27:31 +02:00
streams = hlsparse(
self.config,
self.http.request("get", res.json()["playbackItem"]["manifestUrl"]),
res.json()["playbackItem"]["manifestUrl"],
output=self.output,
)
2018-10-13 13:55:25 +02:00
for n in list(streams.keys()):
yield streams[n]
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 = []
token, message = self._login()
if not token:
2018-11-18 12:47:19 +01:00
logging.error(message)
return
2017-09-16 23:45:29 +02:00
res = self.http.get(self.url)
tags = re.findall('<a class="card__link" href="([^"]+)"', res.text)
for i in tags:
url = urljoin("https://www.cmore.{}/".format(self._gettld()), i)
2017-09-16 23:45:29 +02:00
if url not in episodes:
episodes.append(url)
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"):
2020-07-29 23:14:54 +02:00
url = "https://tve.cmore.se/country/{}/operator/{}/user/{}/exists?client=cmore-web-prod".format(
tld, self.config.get("cmoreoperator"), self.config.get("username")
)
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):
2020-07-29 23:14:54 +02:00
res = self.http.get("https://tve.cmore.se/country/{}/operator?client=cmore-web-prod".format(self._gettld()))
for i in res.json()["data"]["operators"]:
2019-08-25 00:33:51 +02:00
print("operator: '{}'".format(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"])