-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadMonsters.py
77 lines (70 loc) · 1.95 KB
/
loadMonsters.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
#!/bin/python3
import urllib.request
import os
import json
import re
source_version = "v2.3"
source_filename = "monsterData"
source_extension = "js"
source_filename_full = source_filename + "." + source_version + "." + source_extension
source_url_path = "https://github.com/mjcadz/sfrpgtools/raw/master/app/static/js/"
source_url = source_url_path + source_filename_full
filePrefix = "var monsterData = "
fileJSCommentRegex = ",\n\s*?\/\*(.|\n|\r)+?\*\/"
def main():
urllib.request.urlretrieve(source_url, source_filename_full) # downloads file
f = open(source_filename_full, "r")
file_contents = f.read()
f.close()
file_contents = file_contents.rstrip()
# print(file_contents)
if len(file_contents) == 0:
print("File empty");
return 1
# print(file_contents[-1])
# print(file_contents[0:len(filePrefix)+1])
if file_contents[-1] != ";" or file_contents[0:len(filePrefix)] != filePrefix:
print("File formatting has changed");
return 1
file_contents = file_contents[len(filePrefix):-1]
file_contents = re.sub(fileJSCommentRegex, "", file_contents)
# print(file_contents)
data = json.loads(file_contents)
for name in data:
s = convertMonster(name, data[name])
f = open(os.path.join("Bestiary", name + ".md"), "w")
f.write(s)
f.close()
return 0
def convertMonster(name, stats):
return '''
# {}
- [cr::{}]
- [combatType::{}]
- [alignment::{}]
- [size::{}]
- [type::{}]
- [subtype::{}]
- [environment::{}]
- [climate::{}]
- [planet::{}]
- [organization::{}]
- [source::{}]
- [page::{}]
'''.format(
name,
stats["cr"],
stats["combatType"],
stats["alignment"],
stats["size"],
stats["type"],
stats["subtype"],
stats["environment"],
stats["climate"],
stats["planet"],
stats["organization"],
stats["source"],
stats["page"]
)
if __name__ == "__main__":
main()