11const fetcher = require ( './fetch-with-cache.js' ) ;
22const cheerio = require ( 'cheerio' ) ;
33const fs = require ( 'fs' ) ;
4+ const childProcess = require ( 'child_process' ) ;
45
56const windowStickerUrl = 'https://window-sticker-services.pse.dealer.com/windowsticker/MAKE?vin=VIN'
67
8+ function getQueryByDealer ( dealerUrl , make ) {
9+ if ( make === 'subaru' ) {
10+ return 'new-inventory/index.htm?search=&model=Outback&trim=Onyx+Edition+XT' ;
11+ }
12+
13+ if ( dealerUrl . includes ( 'fremontcdjr' ) ) {
14+ return 'new-vehicles/#action=im_ajax_call&perform=get_results&model=Wrangler&page=1' ;
15+ }
16+
17+ return 'new-inventory/index.htm?search=&model=Wrangler' ;
18+ }
19+
720async function fetchFromDealer ( dealerUrl , make , query ) {
21+ if ( ! query )
22+ query = getQueryByDealer ( dealerUrl , make ) ;
823 const url = `${ dealerUrl } ${ query } ` ;
924 const body = await fetcher . getHtml ( url ) ;
1025 let result = await parseResults ( body , dealerUrl , make , url ) ;
@@ -47,9 +62,13 @@ async function parseResults(body, dealer, make, pageUrl) {
4762
4863 if ( carList . length === 0 ) {
4964 console . error ( `No cars found at ${ pageUrl } ` ) ;
50- // const dealerForFile = dealer.match(/https?\:\/\/(www\.)?(?<dealer>\w*)\./).groups.dealer;
51- // fs.writeFileSync(`crap/page_${dealerForFile}.html`, body);
52- // console.error('Page saved for inspection');
65+ const dealerForFile = dealer . match ( / h t t p s ? \: \/ \/ ( w w w \. ) ? (?< dealer > \w * ) \. / ) . groups . dealer ;
66+ fs . writeFileSync ( `crap/page_${ dealerForFile } .html` , body ) ;
67+ childProcess . exec ( `curl -L "${ pageUrl } " -o crap/curl_${ dealerForFile } .html` ) ;
68+ console . error ( 'Page saved for inspection' ) ;
69+ if ( content ( '*' ) . text ( ) . toUpperCase ( ) . includes ( 'CAPTCHA' ) ) {
70+ console . error ( `Captcha request at ${ pageUrl } ` ) ;
71+ }
5372 }
5473 let dealerName = content ( '.org' ) . text ( ) . trim ( ) ;
5574 if ( ! dealerName ) {
@@ -127,4 +146,4 @@ async function parseResults(body, dealer, make, pageUrl) {
127146 return { cars, reportedCars } ;
128147}
129148
130- module . exports = { fetchFromDealer }
149+ module . exports = { fetchFromDealer, getQueryByDealer }
0 commit comments