forked from All-Rated-Extreme-Demon-List/AREDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeRecords.py
57 lines (46 loc) · 1.93 KB
/
removeRecords.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
import os
import sys
import json
def remove_records(user):
print(f"Removing records for user: {user}")
nb_removed = 0
current_dir = os.path.join(os.getcwd(), "data")
list_path = os.path.join(current_dir, "_list.json")
legacy_list_path = os.path.join(current_dir, "_legacy.json")
try:
with open(list_path, "r", encoding='utf-8') as file:
levels = json.load(file)
except ValueError as e:
print(f"Invalid json in file _list.json: {str(e)}")
sys.exit(1)
try:
with open(legacy_list_path, "r", encoding='utf-8') as file:
legacy = json.load(file)
levels.extend(legacy)
except ValueError as e:
print(f"Invalid json in file _legacy.json: {str(e)}")
sys.exit(1)
for filename in levels:
file_path = os.path.join(current_dir, f"{filename}.json")
try:
with open(file_path, "r", encoding='utf-8') as file:
data = json.load(file)
except ValueError as e:
print(f"Invalid json in file {filename}.json: {str(e)}")
sys.exit(1)
original_records = data['records'][:]
data['records'] = [record for record in data['records'] if record['user'] != user]
if len(data['records']) < len(original_records):
nb_removed += len(original_records) - len(data['records'])
try:
with open(file_path, "w", encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent="\t")
print(f"Removed record(s) for level {filename}")
except ValueError as e:
print(f"Failed to write json to file {filename}.json: {str(e)}")
print(f"Successfully removed {nb_removed} records for user {user}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python removeRecords.py <username>")
sys.exit(1)
remove_records(sys.argv[1])