Skip to content
This repository was archived by the owner on Oct 19, 2019. It is now read-only.

Commit d89eb45

Browse files
committed
added kissanime support; added runnerOptions
1 parent 2af0df4 commit d89eb45

File tree

7 files changed

+371
-124
lines changed

7 files changed

+371
-124
lines changed

lib/LinkScrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const Scrapper = require('./Scrapper.js');
22

33
class LinkScrapper extends Scrapper {
4-
constructor({name, domain, runner, exec}={}) {
5-
super({name: name, type: "link", domain: domain, runner: runner, exec: exec});
4+
constructor({name, domain, runner, runnerOptions={}, exec}={}) {
5+
super({name: name, type: "link", domain: domain, runner: runner, runnerOptions: runnerOptions, exec: exec});
66
}
77
}
88

lib/Scrapper.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const runners = require('./runners.js');
99

1010

1111
class Scrapper {
12-
constructor({name, type, domain, runner, exec}={}) {
12+
constructor({name='Scrapper', type, domain, runner, runnerOptions={}, exec}={}) {
1313
Object.defineProperties(this, {
1414
type: {
1515
get: () => type,
@@ -32,6 +32,7 @@ class Scrapper {
3232
this.type = type;
3333
this.domain = domain;
3434
this.runner = runner;
35+
this.runnerOptions = runnerOptions;
3536
this.exec = exec;
3637
}
3738
isApplicable(url) {
@@ -48,7 +49,11 @@ class Scrapper {
4849
if (!(this.runner in runners))
4950
throw new TypeError('runner is not supported');
5051
try {
51-
info = await runners[this.runner].run(url, this.exec);
52+
info = await runners[this.runner].run({
53+
url: url,
54+
scrapper: this.exec,
55+
options: this.runnerOptions
56+
});
5257
} catch(err) {
5358
console.log(err);
5459
info = null; //new Error(1);

lib/StreamScrapper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const Scrapper = require('./Scrapper.js');
22

33
class StreamScrapper extends Scrapper {
4-
constructor({name, domain, runner, exec}={}) {
5-
super({name: name, type: 'stream', domain: domain, runner: runner, exec: exec});
4+
constructor({name, domain, runner, runnerOptions={}, exec}={}) {
5+
super({name: name, type: 'stream', domain: domain, runner: runner, runnerOptions: runnerOptions, exec: exec});
66
}
77
}
88

lib/runners.js

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require("./extensions.js");
22
const typeOf = require("typeof");
3+
const objectMerge = require('object-merge');
34

45
const Runner = require('./Runner.js');
56

@@ -8,24 +9,52 @@ const config = require('./config.js');
89

910
let runners = {};
1011

11-
const puppeteerRunner = new Runner('puppeteer', async (url, scrapper) => {
12+
const puppeteerRunner = new Runner('puppeteer', async ({url, scrapper, options={}}={}) => {
1213
const puppeteer = require('puppeteer');
1314

14-
const browser = await puppeteer.launch(config.puppeteer);
15+
let _options = {
16+
config: {
17+
headless: false
18+
},
19+
requestInterception: {
20+
active: true,
21+
block: ({request}) => request.resourceType === 'font'
22+
}
23+
};
24+
_options = objectMerge(_options, options.puppeteer);
25+
_options.config = objectMerge(_options.config, config.puppeteer);
26+
27+
const browser = await puppeteer.launch(_options.config);
1528
const page = await browser.newPage();
1629

1730
await page.emulateMedia('screen');
1831

19-
await page.setRequestInterception(true);
32+
await page.setRequestInterception(_options.requestInterception.active);
2033
page.on('request', request => {
21-
if (//request.resourceType === 'image' ||
22-
//request.resourceType === 'stylesheet' ||
23-
request.resourceType === 'font')
34+
const block = _options.requestInterception.block && _options.requestInterception.block({
35+
request: request,
36+
resourceType: request.resourceType(),
37+
url: request.url(),
38+
page: page,
39+
browser: browser,
40+
puppeteer: puppeteer
41+
});
42+
43+
if (typeOf(block) === 'undefined')
44+
return;
45+
if (block)
2446
request.abort();
2547
else
2648
request.continue();
2749
});
2850

51+
if (_options.init)
52+
_options.init({
53+
page: page,
54+
browser: browser,
55+
puppeteer: puppeteer
56+
});
57+
2958
/*await page.evaluateOnNewDocument(async () => {
3059
HTMLVideoElement.prototype.canPlayType = function () { return "probably"; };
3160
});*/
@@ -44,37 +73,45 @@ const puppeteerRunner = new Runner('puppeteer', async (url, scrapper) => {
4473

4574
return ret;
4675
});
47-
const htmlRunner = new Runner('html', async (url, scrapper) => {
76+
const htmlRunner = new Runner('html', async ({url, scrapper, options={}}={}) => {
4877
const axios = require('axios');
4978

50-
return await axios.get(url, {
51-
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
52-
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
53-
}).then(async response => await
54-
scrapper({
79+
let config = {
80+
url: url,
81+
method: 'get',
82+
headers: {
83+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
84+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"
85+
}
86+
};
87+
if (options.axios && options.axios.config)
88+
config = objectMerge(config, options.axios.config);
89+
90+
const response = await axios.request(config);
91+
return await scrapper({
5592
url: url,
5693
html: response.data,
94+
response: response,
5795
scrapper: scrapper,
5896
runners: runners
59-
}));
97+
});
6098
});
61-
const domRunner = new Runner('dom', async (url, scrapper) => {
62-
const {default: HTMLStringParser} = require("htmlstringparser");
63-
64-
return await htmlRunner.run(url, async ({html}) => {
65-
const dom = new HTMLStringParser(html);
66-
67-
return await scrapper({
68-
url: url,
69-
html: html,
70-
parser: HTMLStringParser,
71-
dom: dom,
72-
scrapper: scrapper,
73-
runners: runners
74-
});
99+
const domRunner = new Runner('dom', async ({url, scrapper, options={}}={}) => {
100+
const { 'default': HTMLStringParser } = require("htmlstringparser");
101+
102+
return await htmlRunner.run({
103+
url: url,
104+
scrapper: async (args) => {
105+
const dom = new HTMLStringParser(args.html);
106+
107+
args.parser = HTMLStringParser;
108+
args.dom = dom;
109+
return await scrapper(args);
110+
},
111+
options: options
75112
});
76113
});
77-
const urlRunner = new Runner('url', async (url, scrapper) => {
114+
const urlRunner = new Runner('url', async ({url, scrapper, options={}}={}) => {
78115
return await scrapper({
79116
url: url,
80117
scrapper: scrapper,

0 commit comments

Comments
 (0)