1+ const fetch = require ( 'node-fetch' ) ;
12const cheerio = require ( 'cheerio' ) ;
23const fs = require ( 'fs' ) ;
34
45const windowStickerUrl = 'https://window-sticker-services.pse.dealer.com/windowsticker/MAKE?vin=VIN'
56
7+ async function fetchFromDealer ( dealerUrl , make , query ) {
8+ const url = `${ dealerUrl } ${ query } ` ;
9+ const response = await fetch ( url ) ;
10+ const body = await response . text ( ) ;
11+ let result = parseResults ( body , dealerUrl , make , url ) ;
12+ const cars = result . cars ;
13+ console . log ( `${ cars . length } car(s) found at ${ url } ` ) ;
14+ while ( result . reportedCars > cars . length ) {
15+ const response = await fetch ( url . replace ( 'search=' , `start=${ cars . length } ` ) ) ;
16+ const body = await response . text ( ) ;
17+ result = parseResults ( body , dealerUrl , make , url ) ;
18+ cars . push ( ...result . cars ) ;
19+ }
20+ return cars ;
21+ }
22+
623function parseResults ( body , dealer , make , pageUrl ) {
7- console . log ( pageUrl ) ;
824 const cars = [ ] ;
925 const content = cheerio . load ( body ) ;
1026 fs . writeFileSync ( 'page.html' , body )
1127 const numCars = content ( '.vehicle-count' ) . last ( ) . text ( ) ;
12- const apparentNumCars = content ( '.hproduct' , '.bd' ) . length ;
13- console . log ( `numCars = ${ numCars } ; apparentNumCars = ${ apparentNumCars } ` ) ;
14- if ( ! numCars || numCars === '0' || apparentNumCars === 0 )
15- return cars ;
28+ const carList = content ( '.hproduct' , '.bd' ) ;
29+ console . log ( `numCars = ${ numCars } ; carList.length = ${ carList . length } at ${ pageUrl } ` ) ;
1630
1731 let dealerName = content ( '.org' ) . text ( ) . trim ( ) ;
1832 if ( ! dealerName ) {
1933 dealerName = 'Unkown dealer name' ;
2034 }
2135 const dealerAddress = `${ content ( '.street-address' ) . text ( ) . trim ( ) } , ${ content ( '.locality' ) . text ( ) . trim ( ) } , ${ content ( '.region' ) . text ( ) . trim ( ) } , ${ content ( '.postal-code' ) . text ( ) . trim ( ) } ` ;
2236 const dealerCityState = `${ content ( '.locality' ) . text ( ) . trim ( ) } , ${ content ( '.region' ) . text ( ) . trim ( ) } ` ;
23- content ( '.hproduct' , '.bd' ) . each (
37+ carList . each (
2438 ( i , car ) => {
2539 const name = content ( '.url' , car ) . text ( ) . trim ( ) ;
2640 const url = `${ dealer } ${ content ( '.url' , car ) . attr ( 'href' ) } ` ;
@@ -50,6 +64,7 @@ function parseResults(body, dealer, make, pageUrl) {
5064 let vin = content ( '.vin dd' , car ) . text ( ) ;
5165 const engine = content ( '.description dt:contains("Engine:")' , car ) . next ( ) . text ( ) . replace ( ',' , '' ) ;
5266 const theCar = {
67+ pageUrl,
5368 dealerName,
5469 dealerAddress,
5570 dealerCityState,
@@ -74,10 +89,13 @@ function parseResults(body, dealer, make, pageUrl) {
7489 cars . push ( theCar ) ;
7590 }
7691 ) ;
77- if ( cars . length !== parseInt ( numCars ) ) {
78- console . error ( `${ dealerName } website reports ${ numCars } but we retrieved ${ cars . length } !` ) ;
92+
93+ let reportedCars = 0 ;
94+ if ( ! isNaN ( parseInt ( numCars ) ) && carList . length > 0 ) {
95+ reportedCars = parseInt ( numCars )
7996 }
80- return cars ;
97+
98+ return { cars, reportedCars } ;
8199}
82100
83- module . exports = { parseResults }
101+ module . exports = { fetchFromDealer }
0 commit comments