-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb-generator.py
44 lines (35 loc) · 1.21 KB
/
db-generator.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
# Generates databases for songs as well as for poems
import json
from pathlib import Path
DIST_JS_TEMPLATE = """/* DO NOT manually edit this file.
It is auto-generated by db-generator.py. */
const songs_list = {};
const poems_list = {};
"""
DIST_JS_FILENAME = "database.js"
song_src_path = Path("audio")
poem_src_path = Path("poems")
songs = {}
poems = {}
for song in song_src_path.iterdir():
print(f"Processing song {str(song)}")
if ".DS_Store" not in str(song):
song_name = song.stem
songs[song_name] = str(song)
for poem in poem_src_path.iterdir():
print(f"Processing poem {str(poem)}")
if ".DS_Store" not in str(poem):
poem_index = poem.stem
poem_contents = poem.open().read()
# split poem contents into Commandian
# and english
poem_merged = poem_contents.split("---\n")
poem_merged = ["".join(contents) for contents in poem_merged]
poems[poem_index] = poem_merged
# Serialize to JSON
songs_json = json.dumps(songs, indent=4)
poems_json = json.dumps(poems, indent=4)
js_code = DIST_JS_TEMPLATE.format(songs_json, poems_json)
with open(DIST_JS_FILENAME, "w") as file:
file.write(js_code)
print("Success! Results at", DIST_JS_FILENAME)