Created ArgsParser.
This commit is contained in:
parent
ee159c081a
commit
80cb8836a7
33
src/classes/utils/ArgsParser.js
Normal file
33
src/classes/utils/ArgsParser.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
const parseArgs = require('minimist');
|
||||||
|
const { InvalidArgumentError } = require('../../errors');
|
||||||
|
|
||||||
|
class ArgsParser {
|
||||||
|
constructor(minimistOptions = ArgsParser.DEFAULT_OPTIONS, requiredArgs = []) {
|
||||||
|
this.options = minimistOptions;
|
||||||
|
this.required = requiredArgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
parse(argv) {
|
||||||
|
const parsedArgs = parseArgs(argv, this.options);
|
||||||
|
this.validate(parsedArgs);
|
||||||
|
|
||||||
|
return parsedArgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
validate(parsedArgs) {
|
||||||
|
this.required.forEach((arg) => {
|
||||||
|
if (!parsedArgs[arg]) {
|
||||||
|
throw new InvalidArgumentError(`--${arg} is required!`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArgsParser.DEFAULT_OPTIONS = {
|
||||||
|
boolean: ['display'],
|
||||||
|
default: {
|
||||||
|
display: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ArgsParser;
|
9
src/errors/InvalidArgumentError.js
Normal file
9
src/errors/InvalidArgumentError.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class InvalidArgumentError extends Error {
|
||||||
|
constructor(message) {
|
||||||
|
super(message);
|
||||||
|
|
||||||
|
this.name = 'InvalidArgumentError';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = InvalidArgumentError;
|
@ -1,9 +1,11 @@
|
|||||||
const FatalError = require('./FatalError');
|
const FatalError = require('./FatalError');
|
||||||
const ScraperError = require('./ScraperError');
|
const ScraperError = require('./ScraperError');
|
||||||
const JDownloaderError = require('./JDownloaderError');
|
const JDownloaderError = require('./JDownloaderError');
|
||||||
|
const InvalidArgumentError = require('./InvalidArgumentError');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
FatalError,
|
FatalError,
|
||||||
ScraperError,
|
ScraperError,
|
||||||
JDownloaderError
|
JDownloaderError,
|
||||||
|
InvalidArgumentError
|
||||||
};
|
};
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
const puppeteer = require('puppeteer-extra');
|
const puppeteer = require('puppeteer-extra');
|
||||||
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
|
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
|
||||||
const AdBlockerPlugin = require('puppeteer-extra-plugin-adblocker');
|
const AdBlockerPlugin = require('puppeteer-extra-plugin-adblocker');
|
||||||
|
const ArgsParser = require('./classes/utils/ArgsParser');
|
||||||
const KissAsianScraper = require('./classes/scraping/KissAsianScraper');
|
const KissAsianScraper = require('./classes/scraping/KissAsianScraper');
|
||||||
const credentials = require('../data/credentials.json');
|
const credentials = require('../data/credentials.json');
|
||||||
|
|
||||||
|
const args = new ArgsParser().parse(process.argv.slice(2));
|
||||||
|
|
||||||
puppeteer.use(StealthPlugin());
|
puppeteer.use(StealthPlugin());
|
||||||
puppeteer.use(AdBlockerPlugin({ blockTrackers: true }));
|
puppeteer.use(AdBlockerPlugin({ blockTrackers: true }));
|
||||||
|
|
||||||
const main = async() => {
|
const main = async() => {
|
||||||
const browser = await puppeteer.launch({ headless: false, slowMo: 250 });
|
const browser = await puppeteer.launch({ headless: !args.display, slowMo: 250 });
|
||||||
const page = await browser.newPage();
|
const page = await browser.newPage();
|
||||||
const scraper = new KissAsianScraper(page);
|
const scraper = new KissAsianScraper(page);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user