Recursalberg / scripts /gutenberg_index.py
KaraKaraWitch's picture
Upload scripts/gutenberg_index.py with huggingface_hub
6bf7ef0 verified
import multiprocessing
import pathlib
import orjson
qp = multiprocessing.Queue()
# def processor_worker():
# while True:
# data = qp.get()
# if data is None:
# break
# def main():
# workers = 12
# with ProcessPoolExecutor(max_workers=workers) as pool:
# pool.as
# pass
bare_root = pathlib.Path("gutenberg")
idx_nums = [str(i) for i in range(0, 10)]
def iterative_folders(root: pathlib.Path):
files = []
for item in root.iterdir():
if item.is_dir():
if len(item.stem) == 1 and item.stem[0] in idx_nums:
files.extend(iterative_folders(item))
continue
# print(item)
if ".txt" not in item.suffix.lower():
html = item / f"{item.stem}-h"
if html.is_dir() and list(html.glob("*.htm")):
# print(html)
# print("htm",html)
files.append([html, "html"])
elif (item / f"{item.stem}.txt").exists():
# print("txt", item / f"{item.stem}.txt")
files.append([html, "txt"])
elif (item / f"{item.stem}-8.txt").exists():
# print("txt-8 (fb)", item / f"{item.stem}.txt")
files.append([html, "txt-8"])
elif (item / f"{item.stem}-0.txt").exists():
# print("txt-0 (fb)", item / f"{item.stem}.txt")
files.append([html, "txt-0"])
elif (item / f"{item.stem}-t" / f"{item.stem}-t.tex").exists():
# print("txt-0 (fb)", item / f"{item.stem}.txt")
files.append([html, "tex-folder"])
elif (item / "mp3").exists():
files.append([html, "audiobook-mp3"])
# print("mp3", item / f"{item.stem}.txt")
elif (item / "ogg").exists():
files.append([html, "audiobook-ogg-fb"])
pass
# print("mp3", item / f"{item.stem}.txt")
elif (item / "m4b").exists():
files.append([html, "audiobook-m4b-fb"])
pass
# print("mp3", item / f"{item.stem}.txt")
else:
files.append([item, "unk"])
print("????", item)
else:
continue
# if not has_html:
# pass
# print(item)
return files
z = []
for rd in idx_nums:
z.extend(iterative_folders(bare_root / rd))
for idx, item in enumerate(z):
z[idx] = [str(item[0]), item[1]]
pathlib.Path("index.json").write_bytes(orjson.dumps(z, option=orjson.OPT_INDENT_2))