-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-grok-latest.sh
executable file
·203 lines (174 loc) · 7.5 KB
/
build-grok-latest.sh
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# URL for Google Sheets download as XLSX
URL="https://docs.google.com/spreadsheets/d/13U87Sm6e6Fh1SWipl_y_Hi6r2zxfYLyTwjMMZoLjMl4/export?format=xlsx"
# Temporary and output files
TIMESTAMP=$(date +%s)
TEMP_XLSX="temp_$TIMESTAMP.xlsx"
PYTHON_SCRIPT="convert_xlsx_to_json.py"
DOWNLOAD_LOG="download_$TIMESTAMP.log"
# Download the XLSX file with verbose output
echo "Attempting to download from $URL"
curl -f --location -v "$URL" -o "$TEMP_XLSX" 2>&1 | tee "$DOWNLOAD_LOG"
# Check if download was successful
if [ $? -ne 0 ]; then
echo "Failed to download XLSX. Check $DOWNLOAD_LOG for details."
cat "$DOWNLOAD_LOG"
exit 1
fi
# Verify the file extension
if [[ "$TEMP_XLSX" != *.xlsx ]]; then
echo "Downloaded file does not have .xlsx extension. Please check the download URL or the file."
exit 1
fi
# Check if the file exists
if [ ! -f "$TEMP_XLSX" ]; then
echo "File $TEMP_XLSX does not exist after download attempt."
exit 1
fi
echo "File $TEMP_XLSX exists."
# List directory contents to verify file existence
ls -la
# Check file size for verification
echo "File size of $TEMP_XLSX:"
ls -l "$TEMP_XLSX"
# Ensure the file is readable by the current user
chmod +r "$TEMP_XLSX"
# Create Python script to handle XLSX to JSON conversion
cat << EOF > "$PYTHON_SCRIPT"
import subprocess
import sys
import os
import json
from collections import defaultdict
try:
import openpyxl
except ImportError:
print("openpyxl not found. Attempting to install...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "openpyxl"])
import openpyxl
# Check if the file exists and is readable
if not os.path.exists("${TEMP_XLSX}") or not os.access("${TEMP_XLSX}", os.R_OK):
print(f"File '{TEMP_XLSX}' does not exist or is not readable.")
sys.exit(1)
try:
# Load the workbook
wb = openpyxl.load_workbook("${TEMP_XLSX}")
countries_data = {
"countries": []
}
for sheet in wb.worksheets:
sheet_data = {
"level1": []
}
level_hierarchy = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(list))))
country_counter = 0
# Skip the first row which contains headers
for row in sheet.iter_rows(min_row=2, values_only=True):
# Only take the first 6 columns (A-F)
name1, name2, name3, name4, name5, uid = row[:6]
# Clean up and ensure string types
name1 = str(name1).replace('=', '') if name1 else None
name2 = str(name2).replace('=', '') if name2 else None
name3 = str(name3).replace('=', '') if name3 else None
name4 = str(name4).replace('=', '') if name4 else None
name5 = str(name5).replace('=', '') if name5 else None
# Clean up UID
if not uid or not isinstance(uid, str) or '-' not in uid:
country_counter += 1
uid = f"{country_counter:03d}-0001-00000-000000-0000000-00000000"
else:
uid_parts = uid.split('-')
if len(uid_parts) != 6:
uid_parts = ["{:03d}".format(country_counter), "0001", "00000", "000000", "0000000", "00000000"]
# Building the hierarchy with UID
if name1:
current_level1 = level_hierarchy[name1]
current_level1["uid"] = '-'.join(uid_parts)
uid_parts[1] = "0001"
uid_parts[2] = "00000"
uid_parts[3] = "000000"
uid_parts[4] = "0000000"
uid_parts[5] = "00000000"
if name2:
current_level2 = current_level1[name2]
uid_parts[1] = f"{int(uid_parts[1]) + 1:04d}"
current_level2["uid"] = '-'.join(uid_parts)
if name3:
current_level3 = current_level2[name3]
uid_parts[2] = f"{int(uid_parts[2]) + 1:05d}"
current_level3["uid"] = '-'.join(uid_parts)
if name4:
if not isinstance(current_level3, dict):
current_level3 = {}
current_level4 = current_level3[name4]
if not isinstance(current_level4, dict):
current_level4 = {}
uid_parts[3] = f"{int(uid_parts[3]) + 1:06d}"
current_level4["uid"] = '-'.join(uid_parts)
if name5:
if not isinstance(current_level4, dict):
current_level4 = {}
if "level5" not in current_level4:
current_level4["level5"] = []
uid_parts[5] = f"{int(uid_parts[5]) + 1:08d}"
current_level4["level5"].append({"name": name5, "uid": '-'.join(uid_parts)})
# Convert defaultdict to regular dict for JSON serialization
for name1, level2 in level_hierarchy.items():
level1_item = {
"name": name1,
"uid": level2["uid"],
"level2": []
}
for name2, level3 in level2.items():
if isinstance(level3, dict):
level2_item = {
"name": name2,
"uid": level3["uid"],
"level3": []
}
for name3, level4 in level3.items():
if isinstance(level4, dict):
level3_item = {
"name": name3,
"uid": level4["uid"],
"level4": []
}
for name4, level5 in level4.items():
if isinstance(level5, dict) and "level5" in level5:
level4_item = {
"name": name4,
"uid": level5["uid"] if "uid" in level5 else None,
"level5": level5["level5"]
}
level3_item["level4"].append(level4_item)
else:
level3_item["level4"].append({
"name": name4,
"uid": level5["uid"] if isinstance(level5, dict) else None,
"level5": []
})
level2_item["level3"].append(level3_item)
level1_item["level2"].append(level2_item)
sheet_data["level1"].append(level1_item)
# Write JSON for each sheet with lowercase filename
json_filename = f"{sheet.title.lower()}.json"
with open(json_filename, 'w') as json_file:
json.dump(sheet_data, json_file, indent=2)
# Append to countries_data for data.json
countries_data["countries"].append({
"name": sheet.title,
"filename": json_filename
})
# Write the mapping to data.json
with open("data.json", 'w') as data_file:
json.dump(countries_data, data_file, indent=2)
print("JSON files have been created for each tab with corresponding lowercase names.")
print("data.json has been updated to map all countries to their JSON files.")
except openpyxl.utils.exceptions.InvalidFileException as e:
print(f"Error reading XLSX file: {e}")
sys.exit(1)
EOF
# Run Python script
python "$PYTHON_SCRIPT"
# Clean up temporary files
rm "$TEMP_XLSX" "$DOWNLOAD_LOG" "$PYTHON_SCRIPT"