🏗 Wrap CLI code in main()

Idiomatic `__main__` usage to ease (future) tests and code structure
see: https://docs.python.org/3/library/__main__.html#idiomatic-usage
This commit is contained in:
freezed 2022-12-06 01:22:07 +01:00
parent e98417e4e9
commit e7668fafe1
1 changed files with 34 additions and 20 deletions

View File

@ -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())