1- const fetch = require ( 'node- fetch' ) ;
1+ const fetcher = require ( './ fetch-with-cache.js ' ) ;
22const moment = require ( 'moment' ) ;
33const fs = require ( 'fs' ) ;
4-
5- const radiusMiles = 150 ;
6-
4+ const { fetchFromDealer } = require ( './dealer-common.js' ) ;
5+
76function 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} ;
0 commit comments