forked from eirikmun/xwing-data2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysqlpilotids_image.py
More file actions
140 lines (113 loc) · 3.77 KB
/
mysqlpilotids_image.py
File metadata and controls
140 lines (113 loc) · 3.77 KB
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 json
import mysql.connector
# --- DB CONFIG YOU GAVE ---
DB_CONFIG = {
"host": "metro.proxy.rlwy.net",
"user": "root",
"password": "mChKvvEQzxWOKOBhPcYHltMyADqwhpWz",
"database": "railway",
"port": 47124,
}
# Path to your pilots folder
BASE_DIR = r"C:\Users\gregk\Documents\GitHub\xwing-data2\data\pilots"
# Where to write the "what's missing" report
MISSING_LOG = "missing_fields.txt"
# How many digits to pad the ID with (0001, 0002, ...)
ID_WIDTH = 4
def create_ids_table(cursor):
"""
Create the IDs table if it doesn't exist.
Columns:
id - char(4) like '0001'
name - pilot name
xws - pilot xws
image - pilot image URL
"""
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS IDs (
id CHAR(%s) NOT NULL,
name VARCHAR(255),
xws VARCHAR(255),
image TEXT,
PRIMARY KEY (id)
)
"""
% ID_WIDTH
)
# Optional: clear the table every time you run this script.
# Comment this out if you DON'T want to wipe existing data.
cursor.execute("TRUNCATE TABLE IDs")
def iter_json_files(base_dir):
"""Yield full paths to all .json files under base_dir (recursively)."""
for dirpath, _, filenames in os.walk(base_dir):
for fname in filenames:
if fname.lower().endswith(".json"):
yield os.path.join(dirpath, fname)
def main():
# Connect to DB
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
# Create (and optionally truncate) table
create_ids_table(cursor)
# For logging missing fields
missing_lines = []
rows_to_insert = []
counter = 1
for json_path in iter_json_files(BASE_DIR):
try:
with open(json_path, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception as e:
missing_lines.append(
f"FILE ERROR in {json_path}: could not load JSON ({e})"
)
continue
pilots = data.get("pilots")
if not isinstance(pilots, list):
missing_lines.append(
f"NO PILOTS KEY or not a list in file: {json_path}"
)
continue
for pilot in pilots:
# Generate ID like 0001, 0002, ...
card_id = str(counter).zfill(ID_WIDTH)
counter += 1
name = pilot.get("name") or ""
xws = pilot.get("xws") or ""
image = pilot.get("image") or ""
# Check missing fields
missing = []
if not name:
missing.append("name")
if not xws:
missing.append("xws")
if not image:
missing.append("image")
if missing:
# Use xws or name or fallback to card_id for identification
ident = xws or name or f"id={card_id}"
missing_lines.append(
f"Missing {', '.join(missing)} for pilot '{ident}' in file {json_path}"
)
rows_to_insert.append((card_id, name, xws, image))
# Insert into DB
if rows_to_insert:
cursor.executemany(
"INSERT INTO IDs (id, name, xws, image) VALUES (%s, %s, %s, %s)",
rows_to_insert,
)
conn.commit()
# Write missing-fields log
with open(MISSING_LOG, "w", encoding="utf-8") as log_f:
if missing_lines:
log_f.write("\n".join(missing_lines))
else:
log_f.write("No missing fields detected.\n")
cursor.close()
conn.close()
print(f"Inserted {len(rows_to_insert)} pilots into table 'IDs'.")
print(f"Missing field report written to: {os.path.abspath(MISSING_LOG)}")
if __name__ == "__main__":
main()