feat: rede_korrigieren.py -u URL – von YouTube direkt zum korrigierten Text

- extract_text.fetch_text() aus main() herausgelöst und wiederverwendet
- -u/--url statt -i, -l/--lang für die Sprache (Standard: Original-Audio)
- Ohne -o: <Video-ID>.korrigiert.txt; der Rohtext wird daneben gespeichert,
  eine vorhandene, abweichende Rohtext-Datei aber nie überschrieben

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dieter Schlüter 2026-10-10 20:02:53 +02:00
commit 5a072deca3

View file

@ -2,6 +2,7 @@
import io
import json
import shutil
import subprocess
import sys
from pathlib import Path
@ -375,6 +376,51 @@ def test_correct_chunk_sends_model(monkeypatch):
assert sent["model"] == "anderes:7b"
URL = "https://www.youtube.com/watch?v=abc123"
VIDEO = {"id": "abc123", "title": "Rede", "subtitles": {}, "automatic_captions": {"de-orig": []}}
VTT = (
"WEBVTT\n\n00:00:01.000 --> 00:00:02.000\nDer Kremmel ist in Moskau.\n\n"
"00:00:02.000 --> 00:00:03.000\nDer Kremmel ist in Moskau.\nDas ist bekannt.\n"
)
@pytest.fixture
def from_url(fake_yt_dlp, yt_dlp_installed, monkeypatch):
"""Fake download plus fake model for runs with -u."""
monkeypatch.setattr(subprocess, "run", fake_yt_dlp(VIDEO, vtt=VTT))
monkeypatch.setattr(rk, "correct_chunk", identity_correct)
monkeypatch.setattr(rk, "find_paragraph_starts", every_nth_start(5))
def test_main_url_default_names(from_url, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
run_main(monkeypatch, "-u", URL, "-g", SHARED_GLOSSARY)
raw = (tmp_path / "abc123.txt").read_text(encoding="utf-8")
assert raw == "Der Kremmel ist in Moskau.\nDas ist bekannt.\n"
result = (tmp_path / "abc123.korrigiert.txt").read_text(encoding="utf-8")
assert result == "Der Kreml ist in Moskau. Das ist bekannt.\n"
assert (tmp_path / "abc123.aenderungen.html").is_file()
def test_main_url_output_option_and_kept_raw_text(from_url, tmp_path, monkeypatch, capsys):
target = tmp_path / "out" / "rede.txt"
raw_path = tmp_path / "out" / "rede.roh.txt"
run_main(monkeypatch, "-u", URL, "-o", target)
assert raw_path.is_file() and target.is_file()
# A raw text edited by hand is never overwritten
raw_path.write_text("von Hand bearbeitet\n", encoding="utf-8")
run_main(monkeypatch, "-u", URL, "-o", target)
assert raw_path.read_text(encoding="utf-8") == "von Hand bearbeitet\n"
assert "bleibt unverändert" in capsys.readouterr().out
def test_raw_text_path():
assert rk.raw_text_path(Path("a/x.korrigiert.txt")) == Path("a/x.txt")
assert rk.raw_text_path(Path("a/x.txt")) == Path("a/x.roh.txt")
def test_main_warns_on_word_count_deviation(sample_copy, monkeypatch, capsys):
def shortening(chunk, index, system):
words = chunk.split()
@ -401,6 +447,8 @@ def test_main_empty_input(tmp_path, monkeypatch):
(["-i", "{sample}", "-o", "{sample}"], "nicht die Eingabedatei"),
(["-i", "{sample}", "-g", "gibtsnicht.txt"], "Glossar nicht gefunden"),
(["-i", "{sample}", "-g", str(GLOSSARY), "-g", "fehlt.txt"], "Glossar nicht gefunden: fehlt.txt"),
(["-i", "{sample}", "-u", "https://x"], "not allowed with"),
(["-i", "{sample}", "-l", "de"], "nur zusammen mit -u"),
])
def test_cli_errors(sample_copy, monkeypatch, capsys, args, message):
args = [a.format(sample=sample_copy) for a in args]