diff --git a/dltxt2m3t2.py b/dltxt2m3t2.py new file mode 100644 index 0000000..d26f81c --- /dev/null +++ b/dltxt2m3t2.py @@ -0,0 +1,85 @@ +import os + +def save_m3t(m3t_path, mes): + with open(m3t_path, "w", encoding="utf-8") as f: + for m in mes: + if 'name' in m: + f.write("○ NAME: {}\n\n".format(m["name"])) + f.write("○ {}\n".format(m["ori"])) + if 'llm' in m: + f.write("△ {}\n".format(m["llm"])) + if 'dst_name' in m: + f.write("○ NAME: {}\n\n".format(m["dst_name"])) + if m['dst']: + f.write("● {}\n".format(m["dst"])) + else: + f.write("●\n") + f.write("\n") + + +def load_dltxt(dltxt_path): + mes = [] + name = None + ori = None + dst_name = None + line_num = 0 + for line in open(dltxt_path, "r", encoding="utf-8"): + if line_num == 0: + line = line.lstrip("\ufeff") + line_num += 1 + line = line.strip() + if line.startswith("○"): + oris = line[1:].strip().split("○", 1) + if len(oris) == 1: + raise ValueError("Invalid format: missing ○ after ○.") + ori = oris[1].strip() + elif line.startswith("●"): + dsts = line[1:].strip().split("●", 1) + if len(dsts) == 1: + raise ValueError("Invalid format: missing ● after ●.") + dst = dsts[1].strip() + if dst.endswith("name▲"): + name = ori + ori = None + dst_name = dst[:-5].strip() + dst = None + continue + if ori is None: + raise ValueError(f"Invalid format: found ● before ○ on line {line_num}.") + target = { + "ori": ori, + "dst": dst, + } + if name is not None: + target["name"] = name + if dst_name is not None: + target["dst_name"] = dst_name + name = None + ori = None + dst_name = None + mes.append(target) + return mes + + +def process_recursive(dltxt_path, m3t_path): + for root, dirs, files in os.walk(dltxt_path): + rel_dir = os.path.relpath(root, dltxt_path) + target_dir = os.path.join(m3t_path, rel_dir) + os.makedirs(target_dir, exist_ok=True) + for file in files: + ori = os.path.join(root, file) + print("Processing:", ori) + mes = load_dltxt(ori) + save_m3t(os.path.join(target_dir, os.path.splitext(file)[0] + '.m3t'), mes) + + +if __name__ == "__main__": + import sys + dltxt_path = sys.argv[1] + m3t_path = sys.argv[2] + if os.path.isdir(dltxt_path): + process_recursive(dltxt_path, m3t_path) + else: + mes = load_dltxt(dltxt_path) + save_m3t(m3t_path, mes) +