-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
60 lines (51 loc) · 1.44 KB
/
scrape.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const allScrapingCompleteSymbol = require('./allScrapingCompleteSymbol');
const apiUrl = require('./apiUrl');
const $ = require('cheerio');
const {
writeFile,
} = require('fs-extra');
const getArgs = require('./getArgs');
const handleIfdbResponse = require('./handleIfdbResponse');
const {
get,
} = require('https');
module.exports = async ({
search,
...args
}) => {
let argStr = `?searchfor=${search || ''}`;
const rest = getArgs({ ...args });
argStr += search && rest ? ` ${rest}` : rest;
const baseUrl = `${apiUrl}/search/${argStr}`;
let ongoing = true;
let page = 1;
let outputs = [];
while (ongoing) {
if (args.verbose) {
console.log(`Scraping ${encodeURI(baseUrl)}&pg=${page}...`);
}
await new Promise((resolve, reject) => get(
`${encodeURI(baseUrl)}&pg=${page}`,
(res) => handleIfdbResponse(res, args.deep, args.verbose).then(
(data) => {
if (data === allScrapingCompleteSymbol) {
ongoing = false;
} else {
outputs.push(data);
page += 1;
}
return resolve();
},
(err) => {
ongoing = false;
return reject(err);
},
),
));
}
const flattened = outputs.reduce((arr, item) => arr.concat(item), []);
if (args.outFile) {
writeFile(args.outFile, JSON.stringify(flattened, null, 2));
}
return flattened;
};