import json

with open('sefaria_index.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

def get_base_texts(node):
    texts = []
    if 'title' in node:
        texts.append(node['title'])
    elif 'contents' in node:
        for child in node['contents']:
            texts.extend(get_base_texts(child))
    return texts

mishnah = []
talmud = []

for item in data:
    if item.get('category') == 'Mishnah':
        mishnah = get_base_texts(item)
    if item.get('category') == 'Talmud':
        for child in item.get('contents', []):
            if child.get('category') == 'Bavli':
                talmud = get_base_texts(child)

print("ACTUAL MISHNAH LENGTH:", len(mishnah))
print("ACTUAL TALMUD LENGTH:", len(talmud))

with open('exact_names.py', 'w', encoding='utf-8') as f:
    f.write(f"MISHNAH_BOOKS = {json.dumps(mishnah)}\n")
    f.write(f"TALMUD_BOOKS = {json.dumps(talmud)}\n")
