diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdd59ea --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.mp4 +*.orig +*.pyc +__pycache__/ +*.srt diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..940d98c --- /dev/null +++ b/Makefile @@ -0,0 +1,30 @@ +VBIN=${VIRTUAL_ENV}/bin + +help: # Print help on Makefile + @grep '^[^.#]\+:\s\+.*#' Makefile | \ + sed "s/\(.\+\):\s*\(.*\) #\s*\(.*\)/`printf "\e[1;33;4;40m"`\1`printf "\033[0m"` \3/" | \ + expand -t20 + +clean: # Remove files not tracked in source control + find . -type f -name "*.orig" -delete + find . -type f -name "*.pyc" -delete + find . -type d -name "__pycache__" -delete + find . -type d -empty -delete + +format: # Format the code and lint it + ${VBIN}/black *.py + .git/hooks/pre-commit + +init-pre_commit: # Set up git pre-commit hook + echo "make --no-print-directory --quiet lint" > .git/hooks/pre-commit && chmod u+x .git/hooks/pre-commit + +lint: # Lint code + ${VBIN}/black --quiet --check *.py && echo "✅ black" || echo "🚨 black" + +open_all: # Open all projects files + ${EDITOR} ${VBIN}/activate + ${EDITOR} .gitignore delarte.py Makefile README.md requirements-dev.txt requirements.txt + ${EDITOR} .git/hooks/p*-commit + +pre_commit: # Run the pre-commit hook + bash .git/hooks/pre-commit diff --git a/README.md b/README.md index 2c3f356..cb1ce54 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,17 @@ Rajouter le code sous-titre en paramètre: ### 🚀 Chauffe Marcel! - `Python 3.10` à été utilisé +- Code formaté avec [`black`](https://pypi.org/project/black) +- Installation des outils de développement: + * `pip install -r requirements-dev.txt` +- Un `Makefile` équipé: executer `make help` pour le détail +- Un _git hook_ de `pre-commit` + * `make init-pre_commit` + ### 📌 Dépendances -Voir [`requirements.txt`](requirements.txt) +Voir [`requirements.txt`](requirements.txt) & [`requirements-dev.txt`](requirements-dev.txt) ### 🤝 Filer un coup de main diff --git a/delarte.py b/delarte.py index 07edf39..e0b0cb8 100755 --- a/delarte.py +++ b/delarte.py @@ -15,6 +15,7 @@ import webvtt FFMPEG = environ.get("PATH_FFMPEG", "ffmpeg path not found") + def call_api(api_url): http_response = urlopen(api_url) @@ -32,15 +33,20 @@ def call_api(api_url): return (title, versions) + def write_subtitles(m3u8_url, base_name): main = m3u8.load(m3u8_url) - sub_m3u8_urls = [(m.base_uri + '/' + m.uri, m.language) for m in main.media if m.type == "SUBTITLES"] + sub_m3u8_urls = [ + (m.base_uri + "/" + m.uri, m.language) + for m in main.media + if m.type == "SUBTITLES" + ] for sub_m3u8_url, sub_lang in sub_m3u8_urls: sub_m3u8 = m3u8.load(sub_m3u8_url) - sub_urls = [cast(str, sub_m3u8.base_uri) + '/' + f for f in sub_m3u8.files] + sub_urls = [cast(str, sub_m3u8.base_uri) + "/" + f for f in sub_m3u8.files] if not sub_urls: raise ValueError("No subtitle files") @@ -52,9 +58,9 @@ def write_subtitles(m3u8_url, base_name): if http_response.status != HTTPStatus.OK: raise RuntimeError("Subtitle request failed") - buffer = io.StringIO(http_response.read().decode('utf8')) + buffer = io.StringIO(http_response.read().decode("utf8")) - with open(f"{base_name}.{sub_lang}.srt", "w", encoding='utf8') as f: + with open(f"{base_name}.{sub_lang}.srt", "w", encoding="utf8") as f: for i, caption in enumerate(webvtt.read_buffer(buffer), 1): print(i, file=f) print( @@ -66,14 +72,17 @@ def write_subtitles(m3u8_url, base_name): print(caption.text + "\n", file=f) return f.name + # command line arguments -(UI_LANG, _, STREAM_ID, SLUG) = urlparse(sys.argv[1]).path[1:-1].split('/') +(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": +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("/", "-") @@ -87,11 +96,6 @@ M3U8_URL, VERSION_NAME = VERSIONS[VERSION] 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"] ) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..7e66a17 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +black