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

120 lines
5.0 KiB
Python
Raw Normal View History

2018-02-12 00:55:51 +01:00
import json
2019-08-25 00:40:39 +02:00
import re
from urllib.parse import quote
from urllib.parse import urlparse
2018-02-12 00:55:51 +01: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
2018-02-12 00:55:51 +01:00
class Eurosport(Service):
2019-08-25 00:27:31 +02:00
supported_domains_re = [r"^([^.]+\.)*eurosportplayer.com"]
2018-02-12 00:55:51 +01:00
def get(self):
parse = urlparse(self.url)
2019-08-25 00:27:31 +02:00
match = re.search("window.server_path = ({.*});", self.get_urldata())
2018-02-12 00:55:51 +01:00
if not match:
yield ServiceError("Cant find api key")
return
janson = json.loads(match.group(1))
clientapikey = janson["sdk"]["clientApiKey"]
devices = "https://eu.edge.bamgrid.com/devices"
postdata = {"deviceFamily": "browser", "applicationRuntime": "firefox", "deviceProfile": "macosx", "attributes": {}}
2021-02-28 22:05:15 +01:00
header = {"authorization": f"Bearer {clientapikey}"}
res = self.http.post(devices, headers=header, json=postdata)
assertion = res.json()["assertion"]
token = "https://eu.edge.bamgrid.com/token"
2019-08-25 00:27:31 +02:00
data = {
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"latitude": 0,
"longitude": 0,
"platform": "browser",
"subject_token": assertion,
"subject_token_type": "urn:bamtech:params:oauth:token-type:device",
}
2018-02-12 00:55:51 +01:00
res = self.http.post(token, headers=header, data=data)
access_token = res.json()["access_token"]
login = "https://eu.edge.bamgrid.com/idp/login"
2021-02-28 22:05:15 +01:00
header = {"authorization": f"Bearer {access_token}"}
res = self.http.post(login, headers=header, json={"email": self.config.get("username"), "password": self.config.get("password")})
2018-02-12 00:55:51 +01:00
if res.status_code > 400:
yield ServiceError("Wrong username or password")
return
id_token = res.json()["id_token"]
grant = "https://eu.edge.bamgrid.com/accounts/grant"
res = self.http.post(grant, headers=header, json={"id_token": id_token})
assertion = res.json()["assertion"]
2018-02-12 00:55:51 +01:00
token = "https://eu.edge.bamgrid.com/token"
2019-08-25 00:27:31 +02:00
data = {
"grant_type": "urn:ietf:params:oauth:grant-type:token-exchange",
"latitude": 0,
"longitude": 0,
"platform": "browser",
"subject_token": assertion,
"subject_token_type": "urn:bamtech:params:oauth:token-type:account",
}
2021-02-28 22:05:15 +01:00
header = {"authorization": f"Bearer {clientapikey}"}
res = self.http.post(token, headers=header, data=data)
2018-02-12 00:55:51 +01:00
access_token = res.json()["access_token"]
query = {"preferredLanguages": ["en"], "mediaRights": ["GeoMediaRight"], "uiLang": "en", "include_images": True}
2018-02-12 00:55:51 +01:00
if parse.path[:11] == "/en/channel":
2018-02-12 00:55:51 +01:00
pagetype = "channel"
2019-08-25 00:27:31 +02:00
match = re.search("/([^/]+)$", parse.path)
2018-02-12 00:55:51 +01:00
if not match:
yield ServiceError("Cant find channel")
return
2020-05-03 11:38:40 +02:00
(vid,) = match.groups()
2018-02-12 00:55:51 +01:00
query["pageType"] = pagetype
query["channelCallsign"] = vid
query["channelCallsigns"] = vid
query["onAir"] = True
2018-05-13 13:06:45 +02:00
self.config.set("live", True) # lets override to true
2018-02-12 00:55:51 +01:00
2019-08-25 00:27:31 +02:00
url = (
"https://search-api.svcs.eurosportplayer.com/svc/search/v2/graphql/persisted/"
"query/eurosport/web/Airings/onAir?variables={}".format(quote(json.dumps(query)))
2019-08-25 00:27:31 +02:00
)
2018-02-12 00:55:51 +01:00
res = self.http.get(url, headers={"authorization": access_token})
vid2 = res.json()["data"]["Airings"][0]["channel"]["id"]
2021-02-28 22:05:15 +01:00
url = f"https://global-api.svcs.eurosportplayer.com/channels/{vid2}/scenarios/browser"
res = self.http.get(url, headers={"authorization": access_token, "Accept": "application/vnd.media-service+json; version=1"})
2018-02-12 00:55:51 +01:00
hls_url = res.json()["stream"]["slide"]
else:
pagetype = "event"
2019-08-25 00:27:31 +02:00
match = re.search("/([^/]+)/([^/]+)$", parse.path)
2018-02-12 00:55:51 +01:00
if not match:
yield ServiceError("Cant fint event id")
return
query["title"], query["contentId"] = match.groups()
query["pageType"] = pagetype
url = "https://search-api.svcs.eurosportplayer.com/svc/search/v2/graphql/" "persisted/query/eurosport/Airings?variables={}".format(
2020-12-26 13:10:56 +01:00
quote(json.dumps(query)),
2019-08-25 00:27:31 +02:00
)
2018-02-12 00:55:51 +01:00
res = self.http.get(url, headers={"authorization": access_token})
programid = res.json()["data"]["Airings"][0]["programId"]
mediaid = res.json()["data"]["Airings"][0]["mediaId"]
2021-02-28 22:05:15 +01:00
url = f"https://global-api.svcs.eurosportplayer.com/programs/{programid}/media/{mediaid}/scenarios/browser"
res = self.http.get(url, headers={"authorization": access_token, "Accept": "application/vnd.media-service+json; version=1"})
2018-02-12 00:55:51 +01:00
hls_url = res.json()["stream"]["complete"]
streams = hlsparse(self.config, self.http.request("get", hls_url), hls_url, authorization=access_token, output=self.output)
2018-05-08 22:48:55 +02:00
for n in list(streams.keys()):
yield streams[n]