"""Extract text from a saved HTML file with Trafilatura."""
import argparse
from pathlib import Path
from trafilatura import extract

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("html", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--recall", action="store_true")
args = parser.parse_args()
text = extract(args.html.read_text(encoding="utf-8"), include_comments=False,
               favor_precision=not args.recall, favor_recall=args.recall)
if not text or not text.strip():
    raise SystemExit("Extraction returned no text. Inspect the saved HTML.")
with args.output.open("x", encoding="utf-8") as stream:
    stream.write(text.strip() + "\n")
print(f"Saved {len(text.split())} words to {args.output}")
