From e1bed8b1be42aa3ef02fc76de6b60c5507927977 Mon Sep 17 00:00:00 2001 From: Barbagus Date: Tue, 27 Dec 2022 08:54:14 +0100 Subject: [PATCH] Provide programmatic access #20 --- src/delarte/__init__.py | 30 ++++++++++++++++++++++++++++++ src/delarte/__main__.py | 15 +++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/delarte/__init__.py b/src/delarte/__init__.py index 4affe09..199c34d 100644 --- a/src/delarte/__init__.py +++ b/src/delarte/__init__.py @@ -4,3 +4,33 @@ """delarte - ArteTV downloader.""" __version__ = "0.1" + +from . import api, hls, muxing +from .model import ( + Metadata, + Rendition, + RenditionAudio, + RenditionSubtitles, + Source, + Variant, +) +from .www import parse_url as parse_web_url + + +def fetch_sources(http_session, target_id, www_lang): + """Fetch a target's sources.""" + return [ + source + for metadata, master_playlist_url in api.fetch_program_info( + http_session, target_id, www_lang + ) + for source in hls.fetch_program_sources( + http_session, metadata, master_playlist_url + ) + ] + + +def download_source(http_session, source, file_name, progress): + """Download the given source into given file.""" + with hls.download_source(http_session, source, progress) as local_source: + muxing.mux_source(local_source, file_name, progress) diff --git a/src/delarte/__main__.py b/src/delarte/__main__.py index 4065fff..dd7db50 100644 --- a/src/delarte/__main__.py +++ b/src/delarte/__main__.py @@ -14,7 +14,7 @@ import time import requests -from . import api, cli, error, hls, muxing, naming, www +from . import cli, download_source, error, fetch_sources, naming, www class Abort(error.ModuleError): @@ -147,15 +147,7 @@ def main(): http_session = requests.sessions.Session() - sources = [ - source - for metadata, master_playlist_url in api.fetch_program_info( - http_session, target_id, www_lang - ) - for source in hls.fetch_program_sources( - http_session, metadata, master_playlist_url - ) - ] + sources = fetch_sources(http_session, target_id, www_lang) if not args: print(f"Available versions:") @@ -178,8 +170,7 @@ def main(): progress = create_progress() for source, file_name in zip(sources, file_names): - with hls.download_source(http_session, source, progress) as local_source: - muxing.mux_source(local_source, file_name, progress) + download_source(http_session, source, file_name, progress) except error.UnexpectedError as e: print(str(e))