Skip to content

Commit

Permalink
Allow multi-countries
Browse files Browse the repository at this point in the history
  • Loading branch information
kami4ka committed Jun 10, 2022
1 parent bd5611d commit 50c38f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ require('yargs')
},
country: {
alias: 'c',
default: 'us',
default: '',
type: 'string',
describe: 'Country of proxies location'
describe: 'Country of proxies location (default: all possible countries)'
}
})
.check(argv => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrapingant/amazon-proxy-scraper",
"version": "2.1.1",
"version": "2.1.2",
"description": "Amazon products scraper by keyword with using ScrapingAnt API",
"main": "index.js",
"bin": {
Expand Down
13 changes: 8 additions & 5 deletions products-scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ProductsScraper {
this.numberOfProducts = parseInt(number) || CONSTANTS.defaultItemLimit;
this.currentSearchPage = 1;
this.saveToFile = save || false;
this.country = country || 'us';
this.country = country;
this.progressBar = showProgress ? new cliProgress.SingleBar({
format: `Amazon Scraping: ${this.keyword} | {bar} | {percentage}% - {value}/{total} Products || ETA: {eta}s`,
}, cliProgress.Presets.shades_classic) : null;
Expand Down Expand Up @@ -76,10 +76,12 @@ class ProductsScraper {
}

checkForCountry() {
this.country = this.country.toLowerCase();
if (this.country) {
this.country = this.country.toLowerCase();

if (!CONSTANTS.supported_countries.includes(this.country)) {
throw `Not supported country. Please use one from the following: ${CONSTANTS.supported_countries.join(", ")}`;
if (!CONSTANTS.supported_countries.includes(this.country)) {
throw `Not supported country. Please use one from the following: ${CONSTANTS.supported_countries.join(", ")}`;
}
}
}

Expand Down Expand Up @@ -137,9 +139,10 @@ class ProductsScraper {
for (let i = 0; i < CONSTANTS.limit.retry; i++) {
try {
// Retry for any network or accessibility cases
const params = this.country ? { proxy_country: this.country } : {};
const response = await retry((attempt) => this.client.scrape(
`${this.host}/s?${queryParams}`,
{ proxy_country: this.country }
params
).catch(attempt), { retries: CONSTANTS.limit.retry });

const pageBody = response.content;
Expand Down

0 comments on commit 50c38f1

Please sign in to comment.