-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_roms_csv
executable file
·60 lines (47 loc) · 1.69 KB
/
create_roms_csv
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
#!/usr/bin/env python3
import json
import csv
import sys
import os
def main():
if len(sys.argv) != 2:
print("Usage: create_roms_csv <folder_path>")
sys.exit(1)
folder_path = sys.argv[1]
# Construct full paths for the JSON and CSV files
json_file_path = os.path.join(folder_path, "roms.json")
csv_file_path = os.path.join(folder_path, "roms.csv")
# Check if the JSON file exists
if not os.path.isfile(json_file_path):
print(f"Error: JSON file '{json_file_path}' does not exist.")
sys.exit(1)
# Read the roms.json file
with open(json_file_path, "r") as file:
data = json.load(file)
# Sort the data alphabetically ignoring case by the name key
sorted_data = sorted(data, key=lambda x: x["name"].lower())
# CSV header
header = ["URL", "Name", "Description", "Tags", "Size (KB)"]
# Base URL to check and remove if present
base_url = "http://roms.sidecartridge.com/"
# Writing to CSV file
with open(csv_file_path, "w", newline="") as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL, delimiter=",", quotechar='"')
writer.writerow(header)
for rom in sorted_data:
writer.writerow(
[
(
rom["url"][len(base_url):]
if rom["url"].startswith(base_url)
else rom["url"]
),
rom["name"],
rom["description"],
"; ".join(rom["tags"]),
rom["size_kb"],
]
)
print(f"CSV file '{csv_file_path}' created successfully.")
if __name__ == "__main__":
main()