-
Notifications
You must be signed in to change notification settings - Fork 5
/
scrape-data.js
58 lines (43 loc) · 1.14 KB
/
scrape-data.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
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const iplocation = require('iplocation');
const EventSource = require('eventsource');
const isIp = require('is-ip');
const adapter = new FileSync('./data.json');
const database = low(adapter);
const ipAPIURL = 'https://ipapi.co/json';
const wikimediaStreamURL = 'https://stream.wikimedia.org/v2/stream/recentchange';
database.defaults({entries: []}).write();
async function onMessage(event) {
let data;
let location;
try {
data = JSON.parse(event.data);
} catch (error) {
console.log('Error parsing data', error);
return;
}
const ipAddress = data.user;
if (!data || !isIp(ipAddress)) {
return;
}
try {
location = await iplocation(ipAddress, [ipAPIURL]);
} catch (error) {
console.log('IP Location Error:', error);
return;
}
// We receive around 10k edits per hour
const item = {
data,
location
};
console.log(item);
database.get('entries').push(item).write();
}
function init() {
console.log('Connecting to', wikimediaStreamURL);
const es = new EventSource(wikimediaStreamURL);
es.addEventListener('message', onMessage);
}
init();