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

108 lines
3.1 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 __future__ import absolute_import, unicode_literals
from urllib.parse import urlparse
from svtplay_dl.service import Service, OpenGraphThumbMixin
from svtplay_dl.fetcher.hls import hlsparse
from svtplay_dl.error import ServiceError
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):
supported_domains = ['koket.se']
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)
slugs = urlp.path.split('/')
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-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":
2018-08-24 00:25:05 +02:00
streams = hlsparse(self.config, self.http.get(videoDataRes.json()["playbackItem"]["manifestUrl"]),
2018-08-23 23:54:32 +02:00
videoDataRes.json()["playbackItem"]["manifestUrl"], output=self.output)
for n in list(streams.keys()):
yield streams[n]
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"
login = {
2018-08-24 02:07:59 +02:00
"username": username,
"password": password
2018-08-24 01:51:17 +02:00
}
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:
self._data = self.http.get("https://www.koket.se/kurser/api/data/{}".format(auth_token)).json()
return self._data