-
Notifications
You must be signed in to change notification settings - Fork 15
/
generate_index_json.py
87 lines (73 loc) · 2.59 KB
/
generate_index_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import json
import png
import base64
from argparse import ArgumentParser
DEFAULT_URL = "https://github.com/SillyTavern/SillyTavern-Content/raw/main/"
ASSETS_FOLDER = "assets/"
EXTENSIONS_FILE = "extensions.json"
OUTPUT_JSON = "index.json"
HIGHLIGHT_JSON = "highlight.json"
highlighted = []
with open(HIGHLIGHT_JSON, "r") as highlightfile:
try:
highlighted = json.load(highlightfile)
except:
print("Error parsing highlight.json")
highlighted = []
parser = ArgumentParser()
parser.add_argument("--url", help="URL to prepend assets path with.")
args = parser.parse_args()
repository_url = args.url if args.url else DEFAULT_URL
if repository_url[-1] != "/":
repository_url += "/"
if not args.url:
print("No --url argument given, default to ", repository_url)
else:
print("Using given --url argument", repository_url)
def read_character(path: str, name: str, entry: dict) -> bool:
reader = png.Reader(os.path.join(path, name))
chunks = reader.chunks()
png_data = None
for chunk in chunks:
if chunk[0] == b'tEXt':
png_data = chunk[1][6:] # skip "chara" and 0x00
break
if not png_data:
print("No tEXt chunk found in", entry)
return False
try:
text = base64.b64decode(png_data)
data = json.loads(text)
entry['name'] = data['data']['name']
entry['description'] = data['data']['creator_notes']
entry['highlight'] = entry['id'] in highlighted
return True
except:
print("Error parsing tEXt chunk in", entry)
return False
if __name__ == "__main__":
assets_json = []
for path, subdirs, files in os.walk(ASSETS_FOLDER):
for name in files:
if name.startswith("."):
continue
type = path[len(ASSETS_FOLDER) :]
id = name
url = repository_url + ASSETS_FOLDER + type + "/" + name
entry = {"type": type, "id": id, "url": url}
if type == "character":
read_result = read_character(path, name, entry)
if not read_result:
print("Skipping invalid entry:", entry)
continue
print("Creating entry:", entry)
assets_json.append(entry)
with open(EXTENSIONS_FILE, "r") as extfile:
extensions = json.load(extfile)
for extension in extensions:
print("Adding extension:", extension)
assets_json.append(extension)
with open(OUTPUT_JSON, "w") as outfile:
outfile.write(json.dumps(assets_json, indent=2))
print("Done.")