diff --git a/lib/svtplay_dl/service/koket.py b/lib/svtplay_dl/service/koket.py index 8dd903e..d29579e 100644 --- a/lib/svtplay_dl/service/koket.py +++ b/lib/svtplay_dl/service/koket.py @@ -1,5 +1,7 @@ # ex:ts=4:sw=4:sts=4:et # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- +import json +import re from urllib.parse import urlparse from svtplay_dl.error import ServiceError @@ -8,59 +10,38 @@ from svtplay_dl.service import OpenGraphThumbMixin from svtplay_dl.service import Service -def findCourse(data, courseSlug): - for c in data["content"]["coursePages"]: - if c["slug"] == courseSlug: - return c - return None - - -def findLesson(course, lessonSlug): - for l in course["lessons"]: - if l["slug"] == lessonSlug: - return l - return None - - class Koket(Service, OpenGraphThumbMixin): supported_domains = ["koket.se"] - supported_path = "/kurser" - - def __init__(self, config, _url, http=None): - Service.__init__(self, config, _url, http) - self._data = None def get(self): urlp = urlparse(self.url) - slugs = urlp.path.split("/") - - courseSlug = slugs[2] - lessonSlug = slugs[3] - - login = self._login() - if not login: - yield ServiceError("Could not login") + if urlp.path.startswith("/kurser"): + res = self.http.post( + "https://www.koket.se/konto/authentication/login", + json={"username": self.config.get("username"), "password": self.config.get("password")}, + ) + if "errorMessage" in res.json(): + yield ServiceError("Wrong username or password") + return + data = self.http.get(self.url) + match = re.search(r'({"@.*})', data) + if not match: + yield ServiceError("Can't find video info") return - data = self._getData() - if data is None: - yield ServiceError("Could not fetch data") + janson = json.loads(f"[{match.group(1)}]") + for i in janson: + if "video" in i: + self.output["title"] = i["video"]["name"] + break + + match = re.search(r"dataLayer = (\[.*\]);<", data) + if not match: + yield ServiceError("Can't find video id") return - course = findCourse(data, courseSlug) - - if course is None: - yield ServiceError("Could not find course") - return - - lesson = findLesson(course, lessonSlug) - - if lesson is None: - yield ServiceError("Could not find lesson") - return - - self.output["id"] = lesson["videoAssetId"] - self.output["title"] = lesson["title"] + janson = json.loads(match.group(1)) + self.output["id"] = janson[0]["video"] url = "https://playback-api.b17g.net/media/{}?service=tv4&device=browser&protocol=hls%2Cdash&drm=widevine".format(self.output["id"]) @@ -72,35 +53,3 @@ class Koket(Service, OpenGraphThumbMixin): videoDataRes.json()["playbackItem"]["manifestUrl"], output=self.output, ) - - def _login(self): - if self._getAuthToken() is None: - username = self.config.get("username") - password = self.config.get("password") - - if (not username) or (not password): - return False - - url = "https://www.koket.se/account/login" - login = {"username": username, "password": password} - - self.http.get(url) - self.http.post(url, data=login) - - if self._getAuthToken() is None: - return False - - return True - - def _getAuthToken(self): - return self.http.cookies.get("authToken") - - def _getData(self): - auth_token = self._getAuthToken() - if auth_token is None: - return None - - if self._data is None: - self._data = self.http.get(f"https://www.koket.se/kurser/api/data/{auth_token}").json() - - return self._data