-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_collector.js
171 lines (104 loc) · 3.29 KB
/
link_collector.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const request = require("request");
const cheerio = require("cheerio");
const psl = require('psl');
const regex = /https?:\/\/(www\.)?([-a-zA-Z\d@:%._+~#=]{1,256}\.[a-zA-Z\d()]{1,6}\b)([-a-zA-Z\d()@:%_+.~#?&/=]*)/;
const fs = require('fs');
const path = require('path');
const AWS = require('aws-sdk');
const dataPath = path.join(__dirname, 'data/');
const pushPath = path.join(__dirname, 'push/');
const combineFiles = require('combine-files');
require('dotenv').config();
const s3 = new AWS.S3({
accessKeyId: process.env.AKID,
secretAccessKey: process.env.SAC
});
function uploadFile2(pushFilePath,pushFileName) {
const fileStream = fs.createReadStream(pushFilePath);
const uploadParams = {
Bucket: process.env.bucket,
Body: fileStream,
Key: 'raw/' + pushFileName,
};
return s3.upload(uploadParams).promise();
}
let c = 0;
const filterList = [
'wiktionary.org',
'wikipedia.org',
'wikimedia.org',
'wikidata.org',
'mediawiki.org',
'wikimediafoundation.org',
'archive.org',
'google.com'
]
async function push_data(domains) {
if (typeof domains == "undefined" || domains.length < 1) {
getDomains();
return false;
}
let now = Date.now().toString();
const file = fs.createWriteStream(dataPath + now + '.txt' );
file.on('error', (err) => {
console.log(err);
});
await domains.forEach((v) => {
file.write(v + '\n');
});
file.end();
return true;
}
function garbageCollector () {
fs.readdir(dataPath, async function (err, files) {
console.log('gargabe collector started');
let files_map = files.map(x => (dataPath + x).toString());
if (err) {
return console.log('Unable to scan directory: ' + err);
}
let now = Date.now().toString();
let pushFileName = now + '.txt'
let pushFilePath = pushPath + now + '.txt';
combineFiles(files_map, pushFilePath);
await files_map.forEach(function (deleteFilePath) {
try {
fs.unlinkSync(deleteFilePath);
} catch (err) {
console.error(err);
}
});
await uploadFile2(pushFilePath,pushFileName);
return true;
});
}
const getDomains = function () {
request('https://en.wikipedia.org/wiki/Special:Random', function (error, response, html) {
c++;
if (error || response.statusCode !== 200) {
return false;
}
let $ = cheerio.load(html);
let links = $('a');
let domains = [];
$(links).each(function(i, link){
let m;
if ((m = regex.exec($(link).attr('href'))) !== null) {
// console.log(m)
let parsed = psl.parse(m[2]);
if (parsed.domain && !domains.includes(parsed.domain) && !filterList.includes(parsed.domain) ) {
domains = domains.concat(parsed.domain)
}
}
});
push_data(domains).then( function (){
if (c % 10 === 0 ) {
garbageCollector();
getDomains();
}else {
getDomains();
}
}
);
});
};
getDomains();