From acf183a29d132aefbe99852afd2884f240bbf810 Mon Sep 17 00:00:00 2001 From: AYehia0 Date: Wed, 10 Aug 2022 22:44:03 +0200 Subject: [PATCH] added save to directory --- README.md | 2 ++ bin/cli.js | 16 +++++++++++++++- lib/Amazon.js | 18 +++++++++++------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e90fb2f..6f7c9b6 100755 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/bin/cli.js b/bin/cli.js index f99ef62..45e22d8 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -1,5 +1,5 @@ #!/usr/bin/env node - +const fs = require("fs") const AmazonScraper = require('../lib'); const startScraper = async (argv) => { @@ -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', @@ -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; diff --git a/lib/Amazon.js b/lib/Amazon.js index c8a98a5..8e294d7 100755 --- a/lib/Amazon.js +++ b/lib/Amazon.js @@ -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'); @@ -36,6 +37,7 @@ class AmazonScraper { asyncTasks, reviewFilter, referer, + path }) { this.asyncTasks = asyncTasks; this.asyncPage = 1; @@ -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; } /** @@ -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 } : {}), @@ -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: