1+ const cheerio = require ( 'cheerio' ) ;
2+ const fs = require ( 'fs' ) ;
3+
4+ const windowStickerUrl = 'https://window-sticker-services.pse.dealer.com/windowsticker/MAKE?vin=VIN'
5+
6+ function parseResults ( body , dealer , make , pageUrl ) {
7+ console . log ( pageUrl ) ;
8+ const cars = [ ] ;
9+ const content = cheerio . load ( body ) ;
10+ fs . writeFileSync ( 'page.html' , body )
11+ 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 ;
16+
17+ let dealerName = content ( '.org' ) . text ( ) . trim ( ) ;
18+ if ( ! dealerName ) {
19+ dealerName = 'noname' ;
20+ }
21+ if ( ! dealerName ) {
22+ dealerName = 'noname' ;
23+ }
24+ if ( ! dealerName ) {
25+ dealerName = 'noname' ;
26+ }
27+ if ( ! dealerName ) {
28+ dealerName = 'noname' ;
29+ }
30+ const dealerAddress = `${ content ( '.street-address' ) . text ( ) . trim ( ) } , ${ content ( '.locality' ) . text ( ) . trim ( ) } , ${ content ( '.region' ) . text ( ) . trim ( ) } , ${ content ( '.postal-code' ) . text ( ) . trim ( ) } ` ;
31+ const dealerCityState = `${ content ( '.locality' ) . text ( ) . trim ( ) } , ${ content ( '.region' ) . text ( ) . trim ( ) } ` ;
32+ content ( '.hproduct' , '.bd' ) . each (
33+ ( i , car ) => {
34+ const name = content ( '.url' , car ) . text ( ) . trim ( ) ;
35+ const url = `${ dealer } ${ content ( '.url' , car ) . attr ( 'href' ) } ` ;
36+ const imgUrl = content ( 'img' , content ( '.media' , car ) ) . attr ( 'src' ) ;
37+ const pricing = content ( '.pricing' , car ) ;
38+ let msrp = content ( 'li' , pricing ) . find ( '.msrp' ) . find ( '.value' ) . text ( ) ;
39+ if ( ! msrp ) {
40+ msrp = content ( '.an-msrp .price' , pricing ) . text ( ) ;
41+ }
42+ if ( ! msrp ) {
43+ msrp = content ( 'span' , pricing ) . first ( ) . next ( ) . text ( ) ;
44+ }
45+ if ( ! msrp ) {
46+ msrp = content ( '.value' , content ( '.salePrice' , pricing ) ) . text ( ) ;
47+ }
48+ if ( ! msrp ) {
49+ msrp = '0'
50+ }
51+ let finalPrice = content ( 'li' , pricing ) . find ( '.final-price' ) . find ( '.value' ) . text ( ) ;
52+ if ( ! finalPrice ) {
53+ finalPrice = content ( '.an-final-price .price' , pricing ) . text ( ) ;
54+ }
55+ if ( ! finalPrice ) {
56+ finalPrice = '0' ;
57+ }
58+ if ( ! finalPrice ) {
59+ finalPrice = '0' ;
60+ }
61+ if ( ! finalPrice ) {
62+ finalPrice = '0' ;
63+ }
64+ const internetPrice = content ( 'li' , pricing ) . find ( '.internetPrice' ) . find ( '.value' ) . text ( ) ;
65+ let vin = content ( '.vin dd' , car ) . text ( ) ;
66+ const engine = content ( '.description dt:contains("Engine:")' , car ) . next ( ) . text ( ) . replace ( ',' , '' ) ;
67+ const theCar = {
68+ dealerName,
69+ dealerAddress,
70+ dealerCityState,
71+ name,
72+ url,
73+ imgUrl,
74+ engine,
75+ vin,
76+ windowSticker : windowStickerUrl . replace ( 'MAKE' , make ) . replace ( 'VIN' , vin ) ,
77+ msrp,
78+ internetPrice,
79+ finalPrice,
80+ prices : [ ]
81+ } ;
82+ content ( 'li' , pricing ) . each (
83+ ( i , price ) => {
84+ const number = content ( '.value' , price ) . text ( ) ;
85+ const label = content ( '.label' , price ) . text ( ) ;
86+ if ( label )
87+ theCar . prices . push ( { label, number } ) ;
88+ } )
89+ cars . push ( theCar ) ;
90+ }
91+ ) ;
92+ if ( cars . length !== parseInt ( numCars ) ) {
93+ console . error ( `${ dealerName } website reports ${ numCars } but we retrieved ${ cars . length } !` ) ;
94+ }
95+ return cars ;
96+ }
97+
98+ module . exports = { parseResults}
0 commit comments