Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1e94e8

Browse files
committed
Get jeep cars by going to factory inventory list first
1 parent 8225a4a commit c1e94e8

2 files changed

Lines changed: 55 additions & 35 deletions

File tree

jeep-mfg.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const fetch = require('node-fetch');
1+
const fetcher = require('./fetch-with-cache.js');
22
const moment = require('moment');
33
const fs = require('fs');
4-
5-
const radiusMiles = 150;
6-
4+
const { fetchFromDealer } = require('./dealer-common.js');
5+
76
function formatCurrency(numberString) {
87
let result = ''
98
for (let i = numberString.length - 1; i > -1; i--) {
@@ -31,8 +30,7 @@ async function getDealers(zipCode, radiusMiles) {
3130
const requestUrl = url
3231
.replace('ZIPCODE', zipCode)
3332
.replace('RADIUSMILES', radiusMiles);
34-
const response = await fetch(requestUrl);
35-
const data = await response.json();
33+
const data = await fetcher.getJson(requestUrl);
3634
const result = data.status;
3735
if (!result === 200) {
3836
console.error(`Could not get dealer list for ${zipCode}: ${result}`);
@@ -88,7 +86,7 @@ function isCarValid(car, filters) {
8886
return true;
8987
}
9088

91-
async function getCars(filters, zipCode, dealers) {
89+
async function getCars(filters, zipCode, dealers, radiusMiles) {
9290
const url = 'https://www.jeep.com/hostd/inventory/getinventoryresults.json?func=SALES&includeIncentives=Y&matchType=X&modelYearCode=IUJ202010&pageNumber=PAGENUMBER&pageSize=PAGESIZE&radius=RADIUSMILES&sortBy=0&zip=ZIPCODE'
9391
const pageSize = 10000;
9492
const requestUrl = url
@@ -97,8 +95,7 @@ async function getCars(filters, zipCode, dealers) {
9795
.replace('RADIUSMILES', radiusMiles)
9896
.replace('PAGESIZE', pageSize);
9997
// console.log(requestUrl);
100-
const response = await fetch(requestUrl);
101-
const data = await response.json();
98+
const data = await fetcher.getJson(requestUrl);
10299
const result = data.result.result
103100
if (!result === "SUCCESS") {
104101
console.error(`${result}: ${data.result.errors.join(', ')}`);
@@ -164,22 +161,17 @@ async function getCars(filters, zipCode, dealers) {
164161
return filteredCars;
165162
}
166163

167-
async function run() {
164+
async function getCarsFromFactory() {
168165
const zipCodes = [
169166
77478,
170167
94610
171168
]
172169

173-
let promises = [];
174-
for (const zipCode of zipCodes) {
175-
promises.push(getDealers(zipCode, radiusMiles));
176-
}
170+
const radiusMiles = 150;
177171

178-
const dealersByZip = await Promise.all(promises);
179172
const allDealers = [];
180-
for (const dealers of dealersByZip) {
181-
allDealers.push(...dealers);
182-
}
173+
const dealersByZip = await Promise.all(zipCodes.map(zipCode => getDealers(zipCode, radiusMiles)));
174+
dealersByZip.map(dealers => allDealers.push(...dealers));
183175

184176
const filter = {
185177
transmissionCodes: ['DFT'],
@@ -193,16 +185,9 @@ async function run() {
193185
exteriorColorCodes: ['PRC', 'PDN', 'PBM', 'PYV', 'PGG']
194186
}
195187

196-
promises = [];
197-
for (const zipCode of zipCodes) {
198-
promises.push(getCars(filter, zipCode, allDealers));
199-
}
200-
201-
const carsByZip = await Promise.all(promises);
202188
const allCars = [];
203-
for (const cars of carsByZip) {
204-
allCars.push(...cars);
205-
}
189+
const carsByZip = await Promise.all(zipCodes.map(zipCode => getCars(filter, zipCode, allDealers, radiusMiles)));
190+
carsByZip.map(cars => allCars.push(...cars));
206191

207192
allCars.sort(function (a, b) {
208193
if (a.finalPrice < b.finalPrice) return -1;
@@ -222,4 +207,38 @@ async function run() {
222207
return allCars;
223208
}
224209

225-
module.exports = {run, getDealers};
210+
async function getCarsFromDealers() {
211+
// Get cars by filtering the factory inventory search, then looking for them in the dealerships'
212+
// website and matching by VIN.
213+
214+
const dealerCars = [];
215+
const factoryCars = await getCarsFromFactory();
216+
217+
const dealerUrls = [... new Set(factoryCars.map(a => a.dealer.website))];
218+
console.log(`Factory query resulted in ${factoryCars.length} cars from ${dealerUrls.length} dealers.`);
219+
220+
const query = 'new-inventory/index.htm?search=&model=Wrangler';
221+
222+
const carsByDealer = await Promise.all(dealerUrls.map(url => fetchFromDealer(url, 'jeep', query)));
223+
const allCars = [];
224+
carsByDealer.map(cars => allCars.push(...cars));
225+
226+
for (const factoryCar of factoryCars) {
227+
let found = false;
228+
const dealerUrl = factoryCar.dealer.website;
229+
for (const car of allCars) {
230+
if (car.vin.trim() === factoryCar.vin.trim()) {
231+
dealerCars.push(car);
232+
found = true;
233+
break;
234+
}
235+
}
236+
if (!found) {
237+
console.error(`Could not find car ${factoryCar.vin} at ${dealerUrl}${query}`)
238+
}
239+
}
240+
241+
return dealerCars;
242+
}
243+
244+
module.exports = {getCarsFromDealers, getDealers};

main.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const subaru = require('./subaru.js');
2-
const jeep = require('./jeep.js');
3-
const fs = require('fs');
4-
const moment = require('moment');
1+
const subaru = require('./subaru.js');
2+
const jeep = require('./jeep.js');
3+
const jeepMfg = require('./jeep-mfg.js');
4+
const fs = require('fs');
5+
const moment = require('moment');
56

67
function makeDealerTable(allCars) {
78
let html = `
@@ -68,15 +69,15 @@ async function makePage() {
6869
<p>Updated ${moment().format('YYYY-MM-DD HH:mm:ss')}</p>
6970
<table><tr><td valign="top">`;
7071

71-
let cars = await subaru.getCarsFromDealers();
72+
let cars;
73+
cars = await subaru.getCarsFromDealers();
7274
html += makeDealerTable(cars);
7375

7476
html += `</td>
7577
<td valign="top">`;
7678

77-
cars = await jeep.getCarsFromDealers();
79+
cars = await jeepMfg.getCarsFromDealers();
7880
html += makeDealerTable(cars);
79-
// html += await makeJeepMfgTable();
8081

8182
html += `
8283
</td></tr></table>

0 commit comments

Comments
 (0)