1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-11-24 04:05:39 +01:00
svtplay-dl/lib/svtplay_dl/service/koket.py

107 lines
3.0 KiB
Python
Raw Normal View History

2018-08-23 23:54:32 +02:00
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
from urllib.parse import urlparse
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 OpenGraphThumbMixin
from svtplay_dl.service import Service
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
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
2018-08-23 23:54:32 +02:00
class Koket(Service, OpenGraphThumbMixin):
2019-08-25 00:27:31 +02:00
supported_domains = ["koket.se"]
2018-08-23 23:54:32 +02:00
supported_path = "/kurser"
2018-08-24 01:51:17 +02:00
def __init__(self, config, _url, http=None):
Service.__init__(self, config, _url, http)
self._data = None
2018-08-23 23:54:32 +02:00
def get(self):
urlp = urlparse(self.url)
2019-08-25 00:27:31 +02:00
slugs = urlp.path.split("/")
2018-08-23 23:54:32 +02:00
courseSlug = slugs[2]
lessonSlug = slugs[3]
login = self._login()
if not login:
yield ServiceError("Could not login")
return
2018-08-24 01:51:17 +02:00
data = self._getData()
if data is None:
yield ServiceError("Could not fetch data")
return
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
course = findCourse(data, courseSlug)
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
if course is None:
2018-08-23 23:54:32 +02:00
yield ServiceError("Could not find course")
return
2018-08-24 01:51:17 +02:00
lesson = findLesson(course, lessonSlug)
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
if lesson is None:
2018-08-23 23:54:32 +02:00
yield ServiceError("Could not find lesson")
return
self.output["id"] = lesson["videoAssetId"]
self.output["title"] = lesson["title"]
url = "https://playback-api.b17g.net/media/{}?service=tv4&device=browser&protocol=hls%2Cdash&drm=widevine".format(self.output["id"])
2018-08-23 23:54:32 +02:00
2018-08-24 00:25:05 +02:00
videoDataRes = self.http.get(url)
2018-08-23 23:54:32 +02:00
if videoDataRes.json()["playbackItem"]["type"] == "hls":
yield from hlsparse(
2019-08-25 00:27:31 +02:00
self.config,
self.http.get(videoDataRes.json()["playbackItem"]["manifestUrl"]),
videoDataRes.json()["playbackItem"]["manifestUrl"],
output=self.output,
)
2018-08-23 23:54:32 +02:00
def _login(self):
2018-08-24 01:51:17 +02:00
if self._getAuthToken() is None:
username = self.config.get("username")
password = self.config.get("password")
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
if (not username) or (not password):
return False
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
url = "https://www.koket.se/account/login"
2019-08-25 00:27:31 +02:00
login = {"username": username, "password": password}
2018-08-23 23:54:32 +02:00
2018-08-24 01:51:17 +02:00
self.http.get(url)
self.http.post(url, data=login)
2018-08-23 23:54:32 +02:00
if self._getAuthToken() is None:
return False
return True
def _getAuthToken(self):
return self.http.cookies.get("authToken")
2018-08-24 01:51:17 +02:00
def _getData(self):
auth_token = self._getAuthToken()
if auth_token is None:
return None
if self._data is None:
2021-02-28 22:05:15 +01:00
self._data = self.http.get(f"https://www.koket.se/kurser/api/data/{auth_token}").json()
2018-08-24 01:51:17 +02:00
return self._data