-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry.py
More file actions
357 lines (324 loc) · 12.8 KB
/
Copy pathretry.py
File metadata and controls
357 lines (324 loc) · 12.8 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import json
import requests
import time
import os
from variables.universal import *
from variables.corvette import *
from variables.ct import *
from variables.camaro import *
from variables.hummer_ev import *
from variables.silverado_ev import *
from variables.sierra_ev import *
from variables.escalade import *
from variables.escalade_iq import *
from variables.celestiq import *
def extractInfo(text, updated_vin, model):
config = model_configs.get(model)
if not config:
raise ValueError(f"Unsupported model: {model}")
return parse_generic(text, updated_vin, config)
# Main vin processing ---------------------------------------------------------------------------
def processVin(vin):
global testedVIN
sticker_folder = os.path.join(path, "Window Stickers")
pdf_filename = os.path.join(sticker_folder, f"{vin}.pdf")
urlFirst = f"https://cws.gm.com/vs-cws/vehshop/v2/vehicle/windowsticker?vin="
try:
newUrl = urlFirst + vin
max_retries = 3
retries = 0
while retries < max_retries:
try:
# Get Request
contentsGet = requests.get(newUrl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0 ', 'Accept-Language': 'en-US'}, timeout=120)
contentsByte = contentsGet.content
contents = contentsGet.text
time.sleep(1)
# Retry if contents is empty
if contents == "":
print("\033[91mEmpty content. Retrying in 3 seconds...\033[0m")
time.sleep(3)
continue
try:
# If json content found = no window sticker
jsonCont = json.loads(contents)
print("\033[30m" + vin + "\033[0m")
# If request returns not a json content = window sticker found
except json.decoder.JSONDecodeError:
if model in ("CT4", "CT5"):
fullPath = f"{path}/ct4-ct5_{year}.txt"
elif model == "ESCALADE ESV":
fullPath = f"{path}/escalade_{year}.txt"
else:
fullPath = f"{path}/{model.lower()}_{year}.txt"
with open(fullPath, "a") as f:
f.write(f"{vin}\n")
print("\033[33m" + vin + "\033[0m")
os.makedirs(sticker_folder, exist_ok=True)
with open(pdf_filename, "wb") as f:
f.write(contentsByte)
try:
pdf_text = extractPDF(contentsByte, vin, path)
except Exception as e:
print("\033[91mMuPDF error. Retrying in 3 seconds...\033[0m")
time.sleep(3)
continue
pdf_info = extractInfo(pdf_text, vin, model)
# Append only the last 6 digits of the VIN to the list and file
if model in ("CT4", "CT5"):
fullPath = f"{path}/skip_ct4-ct5.txt"
elif model == "ESCALADE ESV":
fullPath = f"{path}/skip_escalade.txt"
else:
fullPath = f"{path}/skip_{model.lower()}.txt"
with open(fullPath, "a") as file:
file.write(f"{vin[-6:]}\n")
writeCSV(pdf_info, path, model)
break
except requests.exceptions.ReadTimeout:
print("\033[91mTimed out, retrying in 2 minutes...\033[0m")
retries += 1
time.sleep(120)
testedVIN += 1
except requests.exceptions.RequestException as e:
if isinstance(e.__cause__, ConnectionResetError):
print(f"\033[91mConnectionResetError: {e}. Continue in 10 seconds...\033[0m")
with open(f'{path}/RETRY.txt', "a") as f:
f.write(f"{vin}\n")
time.sleep(10)
return
else:
if "NameResolutionError" in str(e):
print("\033[91mDNS resolution failed. Waiting 2 minutes before retrying...\033[0m")
time.sleep(120)
else:
print(f"\033[91mError: {e}\033[0m")
print("\033[30mSkipping this VIN.\033[0m")
with open(f'{path}/RETRY.txt', "a") as f:
f.write(f"{vin}\n")
return
except KeyboardInterrupt:
return
def parse_generic(text, updated_vin, config):
global foundVIN
foundVIN += 1
lines = text.split('\n')
info = {
"vin": updated_vin,
"model": config["model_name"],
"drivetrain": config.get("default_drivetrain"),
"body": config.get("default_body"),
}
for i, line in enumerate(lines):
if any(f"{year} {suffix}" in line for suffix in ["CT4 ", "CT5 ", "CT6 "]):
model_info = ' '.join(line.strip().split())
model_info = model_info.replace("LUX HAUT DE GAMME", "PREMIUM LUXURY").replace("LUXE HAUT DE GAMME", "PREMIUM LUXURY").replace("LUXE", "LUXURY").replace("SERIE V", "V-SERIES").replace("SERIE-V", "V-SERIES")
modeltrim = model_info[4:].strip().split()
info["model"] = modeltrim[0]
info["trim"] = ' '.join(modeltrim[1:]).replace(" AWD", "").replace("3.6L ", "").replace("3,6L LUXURY A TI", "LUXURY")
if "PRICE*" in line:
info["msrp"] = lines[i + 1].replace("$", "").replace(",", "").replace(".00", "").strip()
if "DELIVERED" in line:
json_data = ' '.join(lines[i + 7:])
all_json = json.loads(json_data)
all_json["Options"] = [opt for opt in all_json["Options"] if opt]
info.update({
"dealer": lines[i + 1].strip().replace("\u2013", "-"),
"location": lines[i + 3].strip(),
"json": all_json,
"all_rpos": all_json["Options"],
"ordernum": all_json["order_number"],
"year": all_json["model_year"],
})
mmc_code = all_json["mmc_code"] = all_json["mmc_code"].strip()
all_json["sitedealer_code"] = all_json["sitedealer_code"].strip()
for item in info["all_rpos"]:
if item in config["body_dict"]:
if model in ("CT4", "CT5"):
info["body"] = "SEDAN"
else:
info["body"] = config["body_dict"][item]
if item in config["color_dict"]:
info["exterior_color"] = config["color_dict"][item]
if item in engines_dict:
info["engine"] = engines_dict[item]
if item in trans_dict:
info["transmission"] = trans_dict[item]
if item in config["trim_dict"]:
info["trim"] = config["trim_dict"][item]
if item == "HP1" or item == "F46":
info["drivetrain"] = "AWD"
if item == "C6G":
info["drivetrain"] = "4WD"
if info.get("engine") == "2.0L Turbo, 4-cylinder, SIDI, VVT" or (info.get("year") == "2019" and info.get("engine") == "3.6L V6, DI, VVT"):
info["transmission"] = "A8"
if "FH1" in info["all_rpos"]:
info["trim"] = trim_dict_hummer_ev["FH1"]
if info["model"] == "HUMMER EV":
if info["body"] == "TRUCK":
info["model"] = "HUMMER EV PICKUP"
elif info["body"] == "SUV":
info["model"] = "HUMMER EV SUV"
if mmc_code in mmc:
info["model"] = mmc[mmc_code]
if mmc_code == "1YG07" or mmc_code == "1YG67":
info["drivetrain"] = "AWD"
if "json" in info and isinstance(info["json"], dict):
info["json"] = json.dumps(info["json"])
# Reorder and check missing fields as before
field_order = ["vin", "year", "model", "body", "trim", "engine", "transmission", "drivetrain",
"exterior_color", "msrp", "dealer", "location", "ordernum", "json"]
info_ordered = {field: info.get(field, None) for field in field_order}
missing_fields = [field for field, value in info_ordered.items() if value is None]
if missing_fields:
with open(f'{path}/missing_info.txt', "a") as f:
f.write(f"{updated_vin} - {','.join(missing_fields)}\n")
return info_ordered
model_configs = {
"ESCALADE IQ": {
"model_name": "ESCALADE IQ",
"default_drivetrain": "4WD",
"default_body": "SUV",
"body_dict": body_dict,
"color_dict": colors_dict_escalade_iq,
"trim_dict": trim_dict_escalade_iq,
},
"ESCALADE": {
"model_name": "ESCALADE",
"default_drivetrain": "RWD",
"default_body": "SUV",
"body_dict": body_dict,
"color_dict": colors_dict_escalade,
"trim_dict": trim_dict_escalade,
},
"ESCALADE ESV": {
"model_name": "ESCALADE ESV",
"default_drivetrain": "RWD",
"default_body": "SUV",
"body_dict": body_dict,
"color_dict": colors_dict_escalade,
"trim_dict": trim_dict_escalade,
},
"HUMMER EV": {
"model_name": "HUMMER EV",
"default_drivetrain": "4WD",
"default_body": "TRUCK",
"body_dict": body_dict,
"color_dict": colors_dict_hummer_ev,
"trim_dict": trim_dict_hummer_ev,
},
"SIERRA EV": {
"model_name": "SIERRA EV",
"default_drivetrain": "4WD",
"default_body": "TRUCK",
"body_dict": body_dict,
"color_dict": colors_dict_sierra_ev,
"trim_dict": trim_dict_sierra_ev,
},
"SILVERADO EV": {
"model_name": "SILVERADO EV",
"default_drivetrain": "4WD",
"default_body": "TRUCK",
"body_dict": body_dict,
"color_dict": colors_dict_silverado_ev,
"trim_dict": trim_dict_silverado_ev,
},
"CT4": {
"model_name": "CT4",
"default_drivetrain": "RWD",
"default_body": "SEDAN",
"body_dict": body_dict,
"color_dict": colors_dict_ct,
"trim_dict": trim_dict_ct,
},
"CT5": {
"model_name": "CT5",
"default_drivetrain": "RWD",
"default_body": "SEDAN",
"body_dict": body_dict,
"color_dict": colors_dict_ct,
"trim_dict": trim_dict_ct,
},
"CT6": {
"model_name": "CT6",
"default_drivetrain": "RWD",
"default_body": "SEDAN",
"body_dict": body_dict,
"color_dict": colors_dict_ct,
"trim_dict": trim_dict_ct,
},
"CAMARO": {
"model_name": "CAMARO",
"default_drivetrain": "RWD",
"default_body": "COUPE",
"body_dict": body_dict,
"color_dict": colors_dict_camaro,
"trim_dict": trim_dict_camaro,
},
"CORVETTE": {
"model_name": "CORVETTE",
"default_drivetrain": "RWD",
"default_body": "COUPE",
"body_dict": body_dict,
"color_dict": colors_dict_corvette,
"trim_dict": trim_dict_corvette,
},
"CELESTIQ": {
"model_name": "CELESTIQ",
"default_drivetrain": "RWD",
"default_body": "SEDAN",
"body_dict": body_dict,
"color_dict": colors_dict_celestiq,
"trim_dict": trim_dict_celestiq,
},
}
model_map = {
"CT4": "CT4", "CT5": "CT5", "CT6": "CT6",
"CAMARO": "CAMARO",
"HUMMER EV": "HUMMER EV",
"SILVERADO EV": "SILVERADO EV",
"SIERRA EV": "SIERRA EV",
"ESCALADE IQ": "ESCALADE IQ",
"ESCALADE": "ESCALADE", "ESCALADE ESV": "ESCALADE ESV",
"CELESTIQ": "CELESTIQ",
}
while True:
model = input('Enter model to use:\n').upper()
if model == "CORVETTE":
y = int(year)
if y >= 2027:
mmc = mmc_2027
elif y == 2019:
mmc = mmc_2019
else:
mmc = mmc_2020
break
elif model in model_map:
model = model_map[model]
break
else:
print("\033[91mPlease enter a valid model or check the year.\033[0m\n")
path = f"{model}/{year}"
if model in ("CT4", "CT5"):
path = f"CT4-CT5/{year}"
elif model == "ESCALADE ESV":
path = f"ESCALADE/{year}"
with open(f"{path}/RETRY.txt", 'r') as file:
lines = file.readlines()
totalVIN = len(lines)
foundVIN = 0
testedVIN = 0
estTime = totalVIN * 2
time_str = format_time(estTime)
print(f"\033[31mETA: {time_str}\033[0m")
startTime = time.time()
for vin in lines:
vin = vin.strip()
processVin(vin)
print("")
endTime = time.time()
elapsedTime = endTime - startTime
time_str = format_time(elapsedTime)
currentTime = time.strftime("%H:%M:%S", time.localtime())
print(f"Ended: {currentTime} - Elapsed time: {time_str}")
print(f"Tested {testedVIN}/{totalVIN} VIN(s) - Found \033[93m{foundVIN}\033[0m match(es)")