-
Notifications
You must be signed in to change notification settings - Fork 52
/
nameReplacer.py
140 lines (103 loc) · 3.99 KB
/
nameReplacer.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
130
131
132
133
134
135
136
137
138
139
140
import os
import sys
import json
import random
current_dir = os.path.join(os.getcwd(), "data")
output_path = os.path.join(current_dir, "_name_map.json")
countries_path = os.path.join(current_dir, "_countries.json")
write_only_name_map = False
name_map={}
lookup_map={}
with open(output_path, 'r', encoding='utf-8') as file:
name_map = json.load(file)
for key, value in name_map.items():
lookup_map[value.lower()] = int(key)
id_digits = 10
id_lower = 10**(id_digits-1)
id_upper = 10**id_digits - 1
def get_id(username):
username = username.strip()
if username.lower() in lookup_map:
return lookup_map[username.lower()]
user_id = 0
while user_id == 0:
user_id = random.randint(id_lower, id_upper)
if user_id in name_map:
user_id = 0
name_map[user_id] = username
lookup_map[username.lower()] = user_id
return user_id
def replace_users(names):
for i in range(len(names)):
if type(names[i]) == int:
continue
names[i] = get_id(names[i])
with open(countries_path, 'r+', encoding='utf-8') as file:
countries = json.load(file)
for country in countries:
replace_users(country['users'])
if not write_only_name_map:
file.seek(0)
json.dump(countries, file, ensure_ascii=False, indent="\t")
file.truncate()
with open(os.path.join(current_dir, "_editors.json"), 'r+', encoding='utf-8') as file:
editors = json.load(file)
for editor_group in editors:
members = editor_group['members']
for i in range(len(members)):
name = members[i]['name']
if type(name) == int:
continue
members[i]['name'] = get_id(name)
if not write_only_name_map:
file.seek(0)
json.dump(editors, file, ensure_ascii=False, indent="\t")
file.truncate()
with open(os.path.join(current_dir, "_supporters.json"), 'r+', encoding='utf-8') as file:
supporters = json.load(file)
for editor_group in supporters:
members = editor_group['members']
for i in range(len(members)):
name = members[i]['name']
if type(name) == int:
continue
members[i]['name'] = get_id(name)
if not write_only_name_map:
file.seek(0)
json.dump(supporters, file, ensure_ascii=False, indent="\t")
file.truncate()
with open(os.path.join(current_dir, "_leaderboard_banned.json"), 'r+', encoding='utf-8') as file:
banned = json.load(file)
replace_users(banned)
if not write_only_name_map:
file.seek(0)
json.dump(banned, file, ensure_ascii=False, indent="\t")
file.truncate()
levels = []
with open(os.path.join(current_dir, "_list.json"), 'r', encoding='utf-8') as file:
levels = json.load(file)
with open(os.path.join(current_dir, "_legacy.json"), 'r', encoding='utf-8') as file:
levels = levels + json.load(file)
for level_name in levels:
with open(os.path.join(current_dir, level_name + ".json"), 'r+', encoding='utf-8') as file:
level = json.load(file)
name = level['author']
if type(name) != int:
level['author'] = get_id(name)
name = level['verifier']
if type(name) != int:
level['verifier'] = get_id(name)
replace_users(level['creators'])
for i in range(len(level['records'])):
name = level['records'][i]['user']
if type(name) == int:
continue
level['records'][i]['user'] = get_id(name)
if not write_only_name_map:
file.seek(0)
json.dump(level, file, ensure_ascii=False, indent="\t")
file.truncate()
name_map = dict(sorted(name_map.items(), key=lambda x: x[1]))
with open(output_path, 'w+', encoding='utf-8') as file:
json.dump(name_map, file, ensure_ascii=False, indent="\t")
print("Written names to file")