🔨 Apply black formatting to project

This commit is contained in:
freezed 2022-12-06 00:18:15 +01:00
parent f9a3adf9ae
commit e221555e94
5 changed files with 62 additions and 15 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.mp4
*.orig
*.pyc
__pycache__/
*.srt

30
Makefile Normal file
View File

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

View File

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

View File

@ -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"]
)

1
requirements-dev.txt Normal file
View File

@ -0,0 +1 @@
black