46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
|
|
"""Convert transcript JSON into a readable timestamped txt for LLM analysis."""
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
|
||
|
|
from config import TRANSCRIPT_DIR
|
||
|
|
from feed import load_episodes
|
||
|
|
from download import slug as ep_slug
|
||
|
|
|
||
|
|
TXT_DIR = os.path.join(TRANSCRIPT_DIR, "txt")
|
||
|
|
|
||
|
|
|
||
|
|
def txt_path(ep: dict) -> str:
|
||
|
|
return os.path.join(TXT_DIR, ep_slug(ep) + ".txt")
|
||
|
|
|
||
|
|
|
||
|
|
def export_txt(ep: dict) -> str:
|
||
|
|
tp = os.path.join(TRANSCRIPT_DIR, ep_slug(ep) + ".json")
|
||
|
|
with open(tp, encoding="utf-8") as f:
|
||
|
|
data = json.load(f)
|
||
|
|
os.makedirs(TXT_DIR, exist_ok=True)
|
||
|
|
out = txt_path(ep)
|
||
|
|
with open(out + ".tmp", "w", encoding="utf-8") as f:
|
||
|
|
f.write(f"# {data['title']}\n# published: {data['pub_date']}\n"
|
||
|
|
f"# episode: {data.get('ep_num')}\n\n")
|
||
|
|
for seg in data["segments"]:
|
||
|
|
s = int(seg["start"])
|
||
|
|
stamp = f"{s // 3600:02d}:{(s % 3600) // 60:02d}:{s % 60:02d}"
|
||
|
|
f.write(f"[{stamp}] {seg['text']}\n")
|
||
|
|
os.replace(out + ".tmp", out)
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
eps = load_episodes()
|
||
|
|
want = {int(a) for a in sys.argv[1:]} if len(sys.argv) > 1 else None
|
||
|
|
n = 0
|
||
|
|
for ep in eps:
|
||
|
|
if want and (ep["ep_num"] not in want):
|
||
|
|
continue
|
||
|
|
src = os.path.join(TRANSCRIPT_DIR, ep_slug(ep) + ".json")
|
||
|
|
if os.path.exists(src):
|
||
|
|
print(export_txt(ep))
|
||
|
|
n += 1
|
||
|
|
print(f"exported {n}")
|