forked from fcode/delarte
- refactor API calls
- define namedtuple for "config" API
This commit is contained in:
parent
e7668fafe1
commit
d908e4f15b
58
delarte.py
58
delarte.py
|
@ -19,7 +19,7 @@ import subprocess
|
|||
|
||||
from http import HTTPStatus
|
||||
from os import environ
|
||||
from typing import cast
|
||||
from typing import NamedTuple, cast
|
||||
from urllib.parse import urlparse
|
||||
from urllib.request import urlopen
|
||||
|
||||
|
@ -28,24 +28,52 @@ import webvtt
|
|||
|
||||
FFMPEG = environ.get("PATH_FFMPEG", "ffmpeg path not found")
|
||||
|
||||
|
||||
def call_api(api_url):
|
||||
"""Retrieve subtitles versions available for a given URL."""
|
||||
http_response = urlopen(api_url)
|
||||
def api_root(url: str):
|
||||
"""Retrieve the root node (infamous "data") of an API call response."""
|
||||
http_response = urlopen(url)
|
||||
|
||||
if http_response.status != HTTPStatus.OK:
|
||||
raise RuntimeError("API request failed")
|
||||
|
||||
config = json.load(http_response)["data"]["attributes"]
|
||||
if http_response.getheader("Content-Type") != "application/vnd.api+json; charset=utf-8":
|
||||
raise ValueError("API response not supported")
|
||||
|
||||
title = config["metadata"]["title"]
|
||||
return json.load(http_response)["data"]
|
||||
|
||||
versions = {
|
||||
s["versions"][0]["eStat"]["ml5"]: (s["url"], s["versions"][0]["label"])
|
||||
for s in config["streams"]
|
||||
}
|
||||
class Config(NamedTuple):
|
||||
provider_id: str
|
||||
title: str
|
||||
subtitle: str
|
||||
versions: dict[str, tuple[str, str]]
|
||||
|
||||
return (title, versions)
|
||||
def api_config(lang: str, provider_id: str) -> Config:
|
||||
"""Retrieve a stream config from API."""
|
||||
|
||||
url = f"https://api.arte.tv/api/player/v2/config/{lang}/{provider_id}"
|
||||
root = api_root(url)
|
||||
|
||||
if root["type"] != "ConfigPlayer":
|
||||
raise ValueError("API response not supported")
|
||||
|
||||
attrs = root["attributes"]
|
||||
|
||||
if attrs["metadata"]["providerId"] != provider_id:
|
||||
raise ValueError("API response not supported")
|
||||
|
||||
return Config(
|
||||
provider_id,
|
||||
attrs["metadata"]["title"],
|
||||
attrs["metadata"]["subtitle"],
|
||||
{
|
||||
s["versions"][0]["eStat"]["ml5"]: (s["url"], s["versions"][0]["label"])
|
||||
for s in attrs["streams"]
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def api_playlist(lang: str, provider_id: str):
|
||||
url = f"https://api.arte.tv/api/player/v2/playlist/{lang}/{provider_id}"
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def write_subtitles(m3u8_url, base_name):
|
||||
|
@ -96,9 +124,9 @@ def main():
|
|||
if UI_LANG not in ("fr", "de", "en", "es", "pl", "it") or _ != "videos":
|
||||
raise ValueError("Invalid URL")
|
||||
|
||||
TITLE, VERSIONS = call_api(
|
||||
f"https://api.arte.tv/api/player/v2/config/{UI_LANG}/{STREAM_ID}"
|
||||
)
|
||||
CONFIG = api_config(UI_LANG, STREAM_ID)
|
||||
TITLE = CONFIG.title
|
||||
VERSIONS = CONFIG.versions
|
||||
|
||||
FILENAME = TITLE.replace("/", "-")
|
||||
|
||||
|
|
Loading…
Reference in New Issue