# scraper/scriptgen.py # Generates scripts (allinone.txt, makebook.txt, say.txt) # using external templates + dynamic merge generation. import os import stat from logbus.publisher import log TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates") # ------------------------------------------------------------ # Load a template file from scraper/templates/ # ------------------------------------------------------------ def load_template(name: str) -> str: path = os.path.join(TEMPLATE_DIR, name) if not os.path.exists(path): log(f"[SCRIPTGEN] Template missing: {path}") return "" with open(path, "r", encoding="utf-8") as f: return f.read() # ------------------------------------------------------------ # Detect volumes (Volume_001, Volume_002, ...) # ------------------------------------------------------------ def detect_volumes(book_base: str): vols = [] for name in os.listdir(book_base): p = os.path.join(book_base, name) if os.path.isdir(p) and name.lower().startswith("volume_"): try: num = int(name.split("_")[1]) vols.append((num, name)) except Exception: continue vols.sort() return [v[0] for v in vols] # ------------------------------------------------------------ # Build the dynamic merge block # ------------------------------------------------------------ def build_merge_block(title: str, author: str, volumes): lines = [] for vol in volumes: line = ( f'm4b-tool merge --jobs=4 --writer="{author}" ' f'--albumartist="{author}" --album="{title}" ' f'--name="{title}" --output-file="{title}-{vol}.m4b" ' f'"{vol}" -vvv' ) lines.append(line) if not lines: return "" return " \\\n&& ".join(lines) + "\n" # ------------------------------------------------------------ # Main generator # ------------------------------------------------------------ def generate_all_scripts(book_base: str, title: str, author: str): log(f"[SCRIPTGEN] Generating scripts in {book_base}") # Load templates say_template = load_template("say.template") cleanup_template = load_template("cleanup.template") volumes = detect_volumes(book_base) log(f"[SCRIPTGEN] Volumes detected: {volumes}") merge_block = build_merge_block(title, author, volumes) # -------------------------------------------------------- # allinone.txt = say + cleanup + merge # -------------------------------------------------------- outfile = os.path.join(book_base, "allinone.txt") with open(outfile, "w", encoding="utf-8") as f: f.write(say_template) f.write("\n") f.write(cleanup_template) f.write("\n") f.write(merge_block) os.chmod(outfile, os.stat(outfile).st_mode | stat.S_IEXEC) log(f"[SCRIPTGEN] Created {outfile}") # -------------------------------------------------------- # makebook.txt = merge only # -------------------------------------------------------- outfile2 = os.path.join(book_base, "makebook.txt") with open(outfile2, "w", encoding="utf-8") as f: f.write(merge_block) os.chmod(outfile2, os.stat(outfile2).st_mode | stat.S_IEXEC) log(f"[SCRIPTGEN] Created {outfile2}") # -------------------------------------------------------- # say.txt = say + cleanup # -------------------------------------------------------- outfile3 = os.path.join(book_base, "say.txt") with open(outfile3, "w", encoding="utf-8") as f: f.write(say_template) f.write("\n") f.write(cleanup_template) os.chmod(outfile3, os.stat(outfile3).st_mode | stat.S_IEXEC) log(f"[SCRIPTGEN] Created {outfile3}") log(f"[SCRIPTGEN] All scripts generated successfully for '{title}'") __all__ = ["generate_all_scripts"]