For each immediate subfolder of `root_folder`, look for a file named `poster.png`. If found, call `get_poster_text(path)` and save the returned text to `poster_text.md` in that same subfolder. Returns a summary dict with counts and any errors.
(root_folder: str | Path)
| 5 | import os |
| 6 | |
| 7 | def write_poster_texts(root_folder: str | Path) -> dict: |
| 8 | """ |
| 9 | For each immediate subfolder of `root_folder`, look for a file named `poster.png`. |
| 10 | If found, call `get_poster_text(path)` and save the returned text to `poster_text.md` |
| 11 | in that same subfolder. |
| 12 | |
| 13 | Returns a summary dict with counts and any errors. |
| 14 | """ |
| 15 | root = Path(root_folder) |
| 16 | processed = 0 |
| 17 | missing = 0 |
| 18 | errors: list[tuple[Path, str]] = [] |
| 19 | |
| 20 | for subdir in root.iterdir(): |
| 21 | if not subdir.is_dir(): |
| 22 | continue |
| 23 | if os.path.exists(subdir / "poster_text.md"): |
| 24 | print(f"Skipping {subdir.name} as poster_text.md already exists.") |
| 25 | continue |
| 26 | print(f"Processing {subdir.name}...") |
| 27 | |
| 28 | poster_path = subdir / "poster.png" |
| 29 | if not poster_path.exists(): |
| 30 | print(f"Missing poster.png in {subdir.name}.") |
| 31 | missing += 1 |
| 32 | continue |
| 33 | |
| 34 | try: |
| 35 | text = get_poster_text(poster_path, False) # assumes this function is available |
| 36 | out_path = subdir / "poster_text.md" |
| 37 | # Ensure we always write UTF-8 with a trailing newline. |
| 38 | Path(out_path).write_text((text or "").rstrip() + "\n", encoding="utf-8") |
| 39 | processed += 1 |
| 40 | except Exception as e: # keep going even if one folder fails |
| 41 | errors.append((poster_path, str(e))) |
| 42 | |
| 43 | return { |
| 44 | "processed": processed, |
| 45 | "missing_poster_png": missing, |
| 46 | "errors": errors, |
| 47 | } |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | parser = argparse.ArgumentParser(description="Extract poster texts from images.") |
no test coverage detected