forked from nmiodice/id4-reservation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock-search.py
177 lines (154 loc) · 4.42 KB
/
stock-search.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
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
import requests as r
import lxml.html
import json
import urllib
import csv
import sys
from time import time
import concurrent.futures
from dataclasses import dataclass, asdict
import itertools
STATES_ROOT='https://www.vw.com/app/dccsearch/vw-us/en/Detailed%20information%20about%20the%20selected%20Volkswagen%20dealer'
INVENTORY_ROOT='https://prod.services.ngw6apps.io/sdl'
WORKER_COUNT=15
@dataclass(frozen=True)
class Dealer:
id: str
name: str
address: dict
contact: dict
lat: float
lon: float
@dataclass(frozen=True)
class Vehicle:
model: str
year: str
vin: str
trim: str
exteriorColor: str
interiorColor: str
msrp: str
adjustedPrice: str
type: str
factoryOrderDate: str
sold: bool
@dataclass(frozen=True)
class Inventory:
dealer: Dealer
vehicles: list
def get_serialized_states(url):
response = r.get(STATES_ROOT)
doc = lxml.html.fromstring(response.content)
states = doc.xpath('//script[@type="x-feature-hub/serialized-states"]')[0].text
statesDecoded = urllib.parse.unquote(states)
js = json.loads(statesDecoded)
return json.loads(
urllib.parse.unquote(js['dccsearch'])
)
def dealers_from_state(state):
return [
Dealer(
id=d['id'],
name=d['name'],
address=d['address'],
contact=d['contact'],
lat=d['geoPosition']['coordinates'][0],
lon=d['geoPosition']['coordinates'][0]
)
for d in state['dealers']['all'].values()
]
def inventory_for_dealer(dealer):
start = time()
params = {
'language': 'en',
'countryCode': 'US',
'currency': 'USD',
'sdlPath': f'/vwsdl/rest/product/dealers/inventory/{dealer.id}.json',
'serviceConfigsServiceConfig': json.dumps({
"key": "service-config",
"urlOrigin": "https://www.vw.com",
"urlPath":"/en.service-config.json",
"tenantCommercial": None,
"tenantPrivate": None,
"customConfig": None,
"homePath": None,
"credentials": {
"username":"",
"password":"",
}
})
}
response = r.get(INVENTORY_ROOT, params=params)
js = response.json()
vehicles = [
Vehicle(
model = item['model'],
year = item['modelYear'],
vin = item['vin'],
trim = item['trimLevel'],
exteriorColor = item['exteriorColorDescription'],
interiorColor = item['interiorColorBaseColor'],
msrp = item['msrp'],
adjustedPrice = item['adjustedListPrice'],
type = item['type'],
factoryOrderDate = item['factoryOrderDate'],
sold = item['sold']
)
for item in js
]
end = time()
print(f'GET dealer/{dealer.id} ({round(end-start, 2)}s)')
return Inventory(dealer, vehicles)
def get_inventory(dealers):
with concurrent.futures.ThreadPoolExecutor(max_workers=WORKER_COUNT) as executor:
return [
r.result()
for r in [
executor.submit(inventory_for_dealer, d)
for d in dealers
]
]
def sort_output(x):
(v, inventory) = x
return (
inventory.dealer.address['province'],
inventory.dealer.address['city'],
inventory.dealer.name,
v.model,
v.trim,
v.exteriorColor,
v.interiorColor,
v.msrp
)
def run():
dealers = dealers_from_state(
get_serialized_states(STATES_ROOT))
print(f'found {len(dealers)} dealers')
results = get_inventory(dealers)
filtered = [
(v, inventory)
for inventory in results
for v in inventory.vehicles
if v.model == 'ID.4'
]
filtered.sort(key=sort_output)
writer = csv.writer(sys.stdout)
for (v, inventory) in filtered:
writer.writerow([
inventory.dealer.id,
inventory.dealer.name,
inventory.dealer.address['province'],
inventory.dealer.address['city'],
inventory.dealer.contact['website'] if 'website' in inventory.dealer.contact else '?',
v.model,
v.trim,
v.exteriorColor,
v.interiorColor,
v.msrp,
v.adjustedPrice,
v.type,
v.factoryOrderDate,
v.sold,
])
if __name__ == '__main__':
run()