-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (40 loc) · 1.3 KB
/
index.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
'use strict';
const pQueue = require('p-queue');
const request = require('request');
const argv = require('minimist')(process.argv.slice(2));
const linebyline = require('linebyline');
const input = linebyline(__dirname + '/' + argv.input);
const output = argv.output;
const fs = require('fs');
const queue = new pQueue({ concurrency: 10 });
const promises = [];
input.on('line', function (line) {
if (!line.length)
return;
let tweet = line;
tweet = tweet.replace(/#/g, '');
promises.push(queue.add(() => {
return requestAPI(tweet);
}));
});
input.on('end', () => {
Promise.all(promises)
.then(() => {
console.log('done');
})
.catch((err) => {
console.log(JSON.stringify(err));
});
});
function requestAPI(text) {
const apiEndPoint = `http://api1.webpurify.com/services/rest/?api_key=b4efc5cfca9391527dd907ac6e69b5b4&method=webpurify.live.check&text=${text}&semail=1&sphone=1&slink=1&format=json`;
return new Promise((resolve, reject) => {
request(apiEndPoint, (req, res) => {
if (!res || res.statusCode !== 200) {
return reject({req: text, rsp: res});
}
fs.appendFileSync(output, text + ',' + JSON.parse(res.body).rsp.found + '\n');
return resolve();
});
});
}