59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
# License: GNU AGPL v3: http://www.gnu.org/licenses/
|
|
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
|
|
|
|
"""Provide ArteTV JSON API utilities."""
|
|
|
|
from .error import UnexpectedAPIResponse, UnsupportedHLSProtocol
|
|
from .model import Metadata
|
|
|
|
MIME_TYPE = "application/vnd.api+json; charset=utf-8"
|
|
|
|
|
|
def _fetch_api_data(http_session, path, object_type):
|
|
# Fetch an API object.
|
|
url = "https://api.arte.tv/api/player/v2/" + path
|
|
|
|
r = http_session.get(url)
|
|
r.raise_for_status()
|
|
|
|
if (_ := r.headers["content-type"]) != MIME_TYPE:
|
|
raise UnexpectedAPIResponse("MIME_TYPE", path, MIME_TYPE, _)
|
|
|
|
obj = r.json()["data"]
|
|
|
|
if (_ := obj["type"]) != object_type:
|
|
raise UnexpectedAPIResponse("OBJECT_TYPE", path, object_type, _)
|
|
|
|
return obj["attributes"]
|
|
|
|
|
|
def fetch_program_info(http_session, site, target_id):
|
|
"""Fetch the given target's associated program information."""
|
|
obj = _fetch_api_data(http_session, f"config/{site}/{target_id}", "ConfigPlayer")
|
|
|
|
metadata = Metadata(
|
|
obj["metadata"]["providerId"],
|
|
obj["metadata"]["title"],
|
|
obj["metadata"]["subtitle"],
|
|
obj["metadata"]["description"],
|
|
obj["metadata"]["duration"]["seconds"],
|
|
)
|
|
|
|
cache = set()
|
|
|
|
for s in obj["streams"]:
|
|
if (_ := s["protocol"]) != "HLS_NG":
|
|
raise UnsupportedHLSProtocol(site, target_id, _)
|
|
|
|
if (program_index_url := s["url"]) in cache:
|
|
raise UnexpectedAPIResponse(
|
|
"DUPLICATE_PROGRAM_INDEX_URL",
|
|
site,
|
|
target_id,
|
|
program_index_url,
|
|
)
|
|
|
|
cache.add(program_index_url)
|
|
|
|
yield (metadata, program_index_url)
|