Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/cli.js
100644 → 100755
Empty file.
17 changes: 11 additions & 6 deletions lib/Amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,15 @@ class AmazonScraper {
throw new Error('Done');
}
if (this.scrapeType === 'products') {
let totalResultCount = body.match(/"totalResultCount":\w+(.[0-9])/gm);
let body_parts = body.split('"html" : "').slice(1);
body_parts = body_parts.map((part) => part.split('",\n')[0]);
let body_html = body_parts.join('');
let totalResultCount = body_html.match(/"totalResultCount":\w+(.[0-9])/gm);

if (totalResultCount) {
this.totalProducts = totalResultCount[0].split('totalResultCount":')[1];
}
this.grabProduct(body, item);
this.grabProduct(body_html, item);
}
if (this.scrapeType === 'reviews') {
this.grabReviews(body);
Expand Down Expand Up @@ -352,7 +355,7 @@ class AmazonScraper {
get setRequestEndpoint() {
switch (this.scrapeType) {
case 'products':
return 's';
return 's/query';
case 'reviews':
return `product-reviews/${this.asin}/ref=cm_cr_arp_d_viewopt_srt?formatType=${
CONST.reviewFilter.formatType[this.reviewFilter.formatType]
Expand Down Expand Up @@ -1156,7 +1159,9 @@ class AmazonScraper {
* @param {*} body
*/
grabProduct(body, p) {
const $ = cheerio.load(body.replace(/\s\s+/g, '').replace(/\n/g, ''));
const body_replaced = body.replace(/\s\s+/g, '').replace(/\\n/g, '').replace(/\\"/g,'"');
const $ = cheerio.load(body_replaced);

let productList = $('div[data-index]');
const scrapingResult = {};

Expand Down Expand Up @@ -1246,12 +1251,12 @@ class AmazonScraper {
}

if (ratingSearch) {
scrapingResult[key].reviews.rating = parseFloat(ratingSearch.children[0].children[0].data);
const ratingText = ratingSearch.children[0].children[0].data;
scrapingResult[key].reviews.rating = parseFloat(ratingText.split(' out of')[0]);

scrapingResult[key].reviews.total_reviews = parseInt(
ratingSearch.parent.parent.parent.next.attribs['aria-label'].replace(/\,/g, ''),
);

scrapingResult[key].score = parseFloat(scrapingResult[key].reviews.rating * scrapingResult[key].reviews.total_reviews).toFixed(2);
}

Expand Down