forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_meta_from_json.py
129 lines (106 loc) 路 4.27 KB
/
copy_meta_from_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""
A python script to copy the metadata from the metadata.json to the actual JS files
"""
import re, json
# Test if the necessery tags exist
def test_metadata(game):
"""
Tests each game that is passed to see if a field is found & if yes how many occurences it found
"""
game_lines = []
found = {
"title": False,
"author": False,
"img": False,
"tags": False,
"addedOn": False,
}
occurences = {"title": 0, "author": 0, "img": 0, "tags": 0, "addedOn": 0}
# Read game
with open(f"./games/{game}.js") as f:
game_lines = f.readlines()
# Check if tags exist
for i, l in enumerate(game_lines):
"""
Search each line for any tag using regex, if yes count an occurence and increment found
"""
if re.findall("@title: (.+)", l):
found["title"] = True
occurences["title"] = len(re.findall("@title: (.+)", l))
if re.findall("@author: (.+)", l):
found["author"] = True
occurences["author"] = len(re.findall("@author: (.+)", l))
if re.findall("@img: (.+)", l):
found["img"] = True
occurences["img"] = len(re.findall("@img: (.+)", l))
if re.findall("@tags: (.+)", l):
found["tags"] = True
occurences["tags"] = len(re.findall("@tags: (.+)", l))
if re.findall("@addedOn: (.+)", l):
found["addedOn"] = True
occurences["addedOn"] = len(re.findall("@addedOn: (.+)", l))
# Return & print the results
print(f"[TEST] {game}: {found} ({occurences})")
return found
# Replace the game meta
def embed_metadata(game_metadata, test_results):
"""
Embeds the game metadata according to what is missing from the test result
"""
game_lines = []
with open(f'./games/{game_metadata["filename"]}.js') as f:
game_lines = f.readlines()
# If no tag is found, the add a new comment at the start of the file
if (
test_results["title"] == False
and test_results["author"] == False
and test_results["img"] == False
and test_results["tags"] == False
and test_results["addedOn"] == False
):
game_lines.insert(0, "*/\n\n")
game_lines.insert(0, "@addedOn: " + game_metadata["addedOn"] + "\n")
game_lines.insert(
0,
"@img: " + (game_metadata["img"] if game_metadata["img"] else "''") + "\n",
)
game_lines.insert(0, "@tags: " + str(game_metadata["tags"]) + "\n")
game_lines.insert(0, "@author: " + game_metadata["author"] + "\n")
game_lines.insert(0, "@title: " + game_metadata["title"] + "\n")
game_lines.insert(0, "/*\n")
print(f'[EMBED] {game_metadata["filename"]}: All Metadata Embed')
# If all metadata is found, skip
elif (
test_results["title"] == True
and test_results["author"] == True
and test_results["img"] == True
and test_results["tags"] == True
and test_results["addedOn"] == True
):
print(f'[EMBED] {game_metadata["filename"]}: No Metadata Embed')
# If any of the metadata is missing, insert it into the 2nd line
else:
if test_results["addedOn"] == False:
game_lines.insert(1, "@addedOn: " + game_metadata["addedOn"] + "\n")
if test_results["img"] == False:
game_lines.insert(
1,
"@img: "
+ (game_metadata["img"] if game_metadata["img"] else "''")
+ "\n",
)
if test_results["tags"] == False:
game_lines.insert(1, "@tags: " + str(game_metadata["tags"]) + "\n")
if test_results["author"] == False:
game_lines.insert(1, "@author: " + game_metadata["author"] + "\n")
if test_results["title"] == False:
game_lines.insert(1, "@title: " + game_metadata["title"] + "\n")
print(f'[EMBED] {game_metadata["filename"]}: Some Metadata Embed')
with open(f'./games/{game_metadata["filename"]}.js', "w") as f:
f.writelines(game_lines)
# Save changes
with open("./games/metadata.json") as f:
metadata = json.load(f)
for game_metadata in metadata:
test_results = test_metadata(game_metadata["filename"])
embed_metadata(game_metadata, test_results)