#!/usr/bin/python

import json
import os


"""
{
title: {
    count: int,
    cover: str
    desc: list[str]
    },
titles: list[str]
}
"""

def main():
    novels = os.listdir("comic")
    chapters = dict()

    for i in novels:
        with open(f"comic/{i}/desc.txt", "r") as f:
            desc = f.readlines()
        chapters[i] = {
            "count": len(os.listdir(f"comic/{i}"))-1, 
            "cover": f"https://s2.felion.myaddr.io/assets/{i}.jpg",
            "desc": desc
        }
        chapter_list = os.listdir(f"comic/{i}")
        chapter_list = [i for i in chapter_list if i not in ["index.json", "desc.txt"]]
        
        for chapter in chapter_list:
            pages = os.listdir(f"comic/{i}/{chapter}")
            pages = [i for i in pages if i not in ["index.json"]]
            with open(f"comic/{i}/{chapter}/index.json", "w") as f:
                f.write(json.dumps({"pages": pages}))


        dt = ''
        for j in i:
            if j in "QWERTYUIOPLKJHGFDSAZXCVBNM":
                dt = dt + " " + j
            else:
                dt += j
        chapters[i]["display_title"] = dt
        novel_index = {
            "chapters": chapter_list, 
            "cover": f"https://s2.felion.myaddr.io/assets/{i}.jpg",
            "desc": desc,
            "title": i,
            "display_title": dt
        }
        with open(f"comic/{i}/index.json", "w") as f:
            f.write(json.dumps(novel_index))

    chapters["titles"] = novels

    with open("index.json", "w") as f:
        f.write(json.dumps(chapters))
    


if __name__ == "__main__":
    main()
