From e7668fafe1e2d9a2147c0cee0e01d9baedb38a24 Mon Sep 17 00:00:00 2001 From: freezed Date: Tue, 6 Dec 2022 01:22:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=20=20Wrap=20CLI=20code=20in=20main?= =?UTF-8?q?()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Idiomatic `__main__` usage to ease (future) tests and code structure see: https://docs.python.org/3/library/__main__.html#idiomatic-usage --- delarte.py | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/delarte.py b/delarte.py index a3f797e..0bdafd0 100755 --- a/delarte.py +++ b/delarte.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# coding:utf-8 +# coding: utf8 """delarte. @@ -88,29 +88,43 @@ def write_subtitles(m3u8_url, base_name): return f.name -# command line arguments -(UI_LANG, _, STREAM_ID, SLUG) = urlparse(sys.argv[1]).path[1:-1].split("/") -VERSION = " ".join(sys.argv[2:]) +def main(): + """CLI function, options passed as arguments.""" + (UI_LANG, _, STREAM_ID, SLUG) = urlparse(sys.argv[1]).path[1:-1].split("/") + VERSION = " ".join(sys.argv[2:]) -if UI_LANG not in ("fr", "de", "en", "es", "pl", "it") or _ != "videos": - raise ValueError("Invalid URL") + 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}" -) + TITLE, VERSIONS = call_api( + f"https://api.arte.tv/api/player/v2/config/{UI_LANG}/{STREAM_ID}" + ) -FILENAME = TITLE.replace("/", "-") + FILENAME = TITLE.replace("/", "-") -if VERSION not in VERSIONS: - print(TITLE) - for v, (_, l) in VERSIONS.items(): - print(f"\t{v} : {l}") - exit(1) + if VERSION not in VERSIONS: + print(TITLE) + for v, (_, l) in VERSIONS.items(): + print(f"\t{v} : {l}") + exit(1) -M3U8_URL, VERSION_NAME = VERSIONS[VERSION] + M3U8_URL, VERSION_NAME = VERSIONS[VERSION] -write_subtitles(M3U8_URL, FILENAME) + write_subtitles(M3U8_URL, FILENAME) -subprocess.run( - [FFMPEG, "-i", M3U8_URL, "-c", "copy", "-bsf:a", "aac_adtstoasc", f"{FILENAME}.mp4"] -) + subprocess.run( + [ + FFMPEG, + "-i", + M3U8_URL, + "-c", + "copy", + "-bsf:a", + "aac_adtstoasc", + f"{FILENAME}.mp4", + ] + ) + + +if __name__ == "__main__": + sys.exit(main())