parent
9a774c4955
commit
6154b396e3
@ -0,0 +1,112 @@
|
|||||||
|
# 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"]
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
main_dir="$( cd "$( dirname "$0" )" && pwd )"
|
||||||
|
|
||||||
|
shopt -s nocasematch # For case-insensitive regex matching
|
||||||
|
|
||||||
|
for subfolder in "$main_dir"/*; do
|
||||||
|
|
||||||
|
if [ -d "$subfolder" ]; then
|
||||||
|
audiofolder="$subfolder/Audio"
|
||||||
|
mkdir -p "$audiofolder"
|
||||||
|
|
||||||
|
for entry in "$subfolder"/*.txt; do
|
||||||
|
fn=$(basename "$entry")
|
||||||
|
[[ "${entry##*.}" =~ txt ]]
|
||||||
|
|
||||||
|
echo "$fn"
|
||||||
|
inputfile="$subfolder/$fn"
|
||||||
|
outputfile="$audiofolder/${fn%.*}.m4b"
|
||||||
|
|
||||||
|
now=$(date +"%T")
|
||||||
|
echo "Current time : $now"
|
||||||
|
echo "$inputfile ->"
|
||||||
|
echo "$outputfile" && \
|
||||||
|
|
||||||
|
if [ -f $outputfile ]; then
|
||||||
|
echo $outputfile + "exists: skipping"
|
||||||
|
else
|
||||||
|
say --voice=Sinji \
|
||||||
|
--output-file="$outputfile" \
|
||||||
|
--input-file="$inputfile" \
|
||||||
|
--file-format=m4bf \
|
||||||
|
--quality=127 \
|
||||||
|
-r 200 \
|
||||||
|
--data-format=aac
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
# CLEANUP WILL BE APPENDED BY scriptgen.py
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
find . -name "*.m4b" -size -580c | while read fname; do
|
||||||
|
echo "deleting $(ls -lah \"$fname\")"
|
||||||
|
rm "$fname"
|
||||||
|
done
|
||||||
Loading…
Reference in new issue