@@ -113,10 +113,6 @@ async function getCars(filters, zipCode, dealers, radiusMiles) {
113113 for ( const car of cars ) {
114114 if ( filters && ! isCarValid ( car , filters ) ) continue ;
115115
116- if ( car . incentives ) {
117- console . log ( car . incentives ) ;
118- }
119-
120116 const engine = car . engineDesc ;
121117 const tranny = car . transmissionDesc ;
122118 const model = car . vehicleDesc ;
@@ -211,8 +207,7 @@ async function getCarsFromDealers() {
211207 // Get cars by filtering the factory inventory search, then looking for them in the dealerships'
212208 // website and matching by VIN.
213209
214- const dealerCars = [ ] ;
215- const factoryCars = await getCarsFromFactory ( ) ;
210+ const factoryCars = await getCarsFromFactory ( ) ; // contains the cars we actually want.
216211
217212 const dealerUrls = [ ... new Set ( factoryCars . map ( a => a . dealer . website ) ) ] ;
218213 console . log ( `Factory query resulted in ${ factoryCars . length } cars from ${ dealerUrls . length } dealers.` ) ;
@@ -221,23 +216,54 @@ async function getCarsFromDealers() {
221216
222217 const carsByDealer = await Promise . all ( dealerUrls . map ( url => fetchFromDealer ( url , 'jeep' , query ) ) ) ;
223218 const allCars = [ ] ;
224- carsByDealer . map ( cars => allCars . push ( ...cars ) ) ;
219+ carsByDealer . map ( cars => allCars . push ( ...cars ) ) ; // these are all the cars in dealerships that have at least one car we want.
225220
221+ const dealerCars = [ ] ;
222+ const unmatchedCars = [ ]
226223 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- }
224+ const matches = allCars . filter ( a => a . vin . trim ( ) === factoryCar . vin . trim ( ) ) ;
225+ if ( ! matches || matches . length === 0 ) {
226+ console . error ( `Could not find car ${ factoryCar . vin } at ${ factoryCar . dealer . website } ${ query } ` )
227+ unmatchedCars . push ( { vin : factoryCar . vin , url :`${ factoryCar . dealer . website } ${ query } ` } ) ;
228+ continue ;
235229 }
236- if ( ! found ) {
237- console . error ( `Could not find car ${ factoryCar . vin } at ${ dealerUrl } ${ query } ` )
230+ if ( matches . length > 1 ) {
231+ console . error ( `Found ${ matches . length } cars with ${ factoryCar . vin } !` ) ;
238232 }
233+ const car = matches [ 0 ] ;
234+ dealerCars . push ( car )
239235 }
240236
237+ dealerCars . sort ( function ( a , b ) {
238+ if ( a . finalPrice < b . finalPrice ) return - 1 ;
239+ if ( a . finalPrice > b . finalPrice ) return 1 ;
240+ return 0 ;
241+ } ) ;
242+
243+ const archive = `archive/jeep_${ moment ( ) . format ( 'YYYY-MM-DD_HH-mm-ss' ) } .json` ;
244+ fs . writeFileSync ( archive , JSON . stringify ( dealerCars , null , 2 ) , err => {
245+ console . error ( err ) ;
246+ } ) ;
247+
248+ fs . writeFileSync ( 'jeep.json' , JSON . stringify ( dealerCars , null , 2 ) , err => {
249+ console . error ( err ) ;
250+ } ) ;
251+
252+ unmatchedCars . sort ( function ( a , b ) {
253+ if ( a . url < b . url ) return - 1 ;
254+ if ( a . url > b . url ) return 1 ;
255+ return 0 ;
256+ } ) ;
257+
258+ fs . writeFileSync ( archive . replace ( 'jeep_' , 'unmatched-jeep_' ) , JSON . stringify ( unmatchedCars , null , 2 ) , err => {
259+ console . error ( err ) ;
260+ } ) ;
261+
262+ fs . writeFileSync ( 'unmatched-jeep.json' , JSON . stringify ( unmatchedCars , null , 2 ) , err => {
263+ console . error ( err ) ;
264+ } ) ;
265+
266+ console . log ( `Found ${ dealerCars . length } dealer cars of ${ factoryCars . length } factory cars.` ) ;
241267 return dealerCars ;
242268}
243269
0 commit comments