Skip to content

added save to directory #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Commands:
Options:
--help, -h help [boolean]
--version Show version number [boolean]
--path, -p Location for saving the results [string] [default: "."]
--async, -a Number of async tasks [string] [default: "5"]
--keyword, -k Amazon search keyword ex. 'Xbox one' [string] [default: ""]
--number, -n Number of products to scrape. Maximum 100 products or 300
Expand Down Expand Up @@ -105,6 +106,7 @@ Examples:
amazon-buddy asin B01GW3H3U8
amazon-buddy categories
amazon-buddy countries
amazon-buddy --country "DE" asin B0039N480I --path ~/results --filetype all
```

#### Example 1
Expand Down
16 changes: 15 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

const fs = require("fs")
const AmazonScraper = require('../lib');

const startScraper = async (argv) => {
Expand Down Expand Up @@ -60,6 +60,12 @@ require('yargs')
alias: 'h',
describe: 'help',
},
path: {
alias: 'p',
default: '.',
type: 'string',
describe: 'Location for saving the results',
},
async: {
alias: 'a',
default: '5',
Expand Down Expand Up @@ -163,6 +169,14 @@ require('yargs')
}
}

// checking the path
if (argv.path){
const pathToDir = argv.path

// check if the path is valid to write
fs.accessSync(pathToDir, fs.constants.W_OK);
}

// Minimum allowed rating is 1
if (!argv['min-rating']) {
argv['min-rating'] = 1;
Expand Down
18 changes: 11 additions & 7 deletions lib/Amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const spinner = ora('Amazon Scraper Started');
const { Parser } = require('json2csv');
const moment = require('moment');
const { SocksProxyAgent } = require('socks-proxy-agent');
const path = require("path")

const CONST = require('./constant');

Expand Down Expand Up @@ -36,6 +37,7 @@ class AmazonScraper {
asyncTasks,
reviewFilter,
referer,
path
}) {
this.asyncTasks = asyncTasks;
this.asyncPage = 1;
Expand Down Expand Up @@ -71,6 +73,7 @@ class AmazonScraper {
this.initTime = Date.now();
this.ua = ua || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36';
this.reviewFilter = reviewFilter;
this.path = path;
}

/**
Expand Down Expand Up @@ -236,13 +239,13 @@ class AmazonScraper {

this.sortAndFilterResult();

await this.saveResultToFile();
await this.saveResultToFile(this.path);

if (this.cli) {
spinner.stop();
}
if (this.fileType && this.cli) {
console.log(`Result was saved to: ${this.fileName}`);
console.log(`Results were saved to: ${path.resolve(this.path)}`);
}
return {
...(this.scrapeType === 'products' ? { totalProducts: this.totalProducts, category: this.productSearchCategory } : {}),
Expand Down Expand Up @@ -305,19 +308,20 @@ class AmazonScraper {
/**
* Save results to the file
*/
async saveResultToFile() {
async saveResultToFile(savePath) {
if (this.collector.length) {
const baseFilePath = path.join(savePath, `${this.fileName}`)
switch (this.fileType) {
case 'json':
await fromCallback((cb) => writeFile(`${this.fileName}.json`, JSON.stringify(this.collector), cb));
await fromCallback((cb) => writeFile(`${baseFilePath}.json`, JSON.stringify(this.collector), cb));
break;
case 'csv':
await fromCallback((cb) => writeFile(`${this.fileName}.csv`, this.jsonToCsv.parse(this.collector), cb));
await fromCallback((cb) => writeFile(`${baseFilePath}.csv`, this.jsonToCsv.parse(this.collector), cb));
break;
case 'all':
await Promise.all([
await fromCallback((cb) => writeFile(`${this.fileName}.json`, JSON.stringify(this.collector), cb)),
await fromCallback((cb) => writeFile(`${this.fileName}.csv`, this.jsonToCsv.parse(this.collector), cb)),
await fromCallback((cb) => writeFile(`${baseFilePath}.json`, JSON.stringify(this.collector), cb)),
await fromCallback((cb) => writeFile(`${baseFilePath}.csv`, this.jsonToCsv.parse(this.collector), cb)),
]);
break;
default:
Expand Down