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

131 lines
4.8 KiB
Python
Raw Normal View History

2018-05-13 13:06:45 +02:00
from __future__ import absolute_import, unicode_literals
2017-09-16 23:45:29 +02:00
import re
2018-01-30 22:07:21 +01:00
from urllib.parse import urljoin, urlparse
2018-11-18 12:47:19 +01:00
import logging
2017-09-16 23:45:29 +02:00
from svtplay_dl.service import Service
2018-10-13 13:55:25 +02:00
from svtplay_dl.fetcher.hls import hlsparse
2017-09-16 23:45:29 +02:00
from svtplay_dl.error import ServiceError
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
2019-08-25 00:27:31 +02:00
metaurl = "https://playback-api.b17g.net/asset/{}?service=cmore.{}" "&device=browser&drm=widevine&protocol=dash%2Chls".format(
self.output["id"], tld
)
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
2018-10-13 13:55:25 +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 {0}".format(token)})
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()
url = "https://www.cmore.{}/login".format(tld)
2017-09-16 23:45:29 +02:00
res = self.http.get(url, cookies=self.cookies)
2018-05-22 00:02:20 +02:00
if self.config.get("cmoreoperator"):
2019-08-25 00:27:31 +02:00
post = {
"username": self.config.get("username"),
"password": self.config.get("password"),
"operator": self.config.get("cmoreoperator"),
"country_code": tld,
}
2017-09-19 00:30:11 +02:00
else:
2018-05-22 00:02:20 +02:00
post = {"username": self.config.get("username"), "password": self.config.get("password")}
res = self.http.post("https://account.cmore.{}/session?client=cmore-web-prod".format(tld), 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()
token = janson["data"]["vimond_token"]
return token, None
2017-09-19 00:30:11 +02:00
def operatorlist(self):
res = self.http.get("https://tve.cmore.se/country/{0}/operator?client=cmore-web".format(self._gettld()))
for i in res.json()["data"]["operators"]:
print("operator: '{0}'".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"])