forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalexa.js
More file actions
82 lines (63 loc) · 1.84 KB
/
alexa.js
File metadata and controls
82 lines (63 loc) · 1.84 KB
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
// Copyright 2016 William Budington
// Copyright 2017 AJ Jordan
// AGPLv3+
'use strict';
const request = require('request'),
unzip = require('unzip');
// (Heavily) modified from code by @Hainish in utils/labeller. Thanks, @Hainish!
// TODO make this return Promises
// TODO test this file
const DAY = 1000 * 60 * 60 * 24,
obj = {data: null};
function retrieveAlexa(cb) {
const alexa = [];
const csvRegex = /^[0-9]+,(.+)/;
request.get('https://s3.amazonaws.com/alexa-static/top-1m.csv.zip')
.on('error', err => {
cb(err);
})
// Dumb ESLint. It's not my fault this person named it like that!?
.pipe(unzip.Parse()) // eslint-disable-line new-cap
.on('entry', entry => {
// TODO this use of readline is super confusing??
const lineReader = require('readline').createInterface({
input: entry
});
lineReader.on('line', line => {
const domain = line.match(csvRegex)[1];
alexa.push(domain);
});
lineReader.on('close', () => {
cb(null, alexa);
});
});
}
module.exports = function getAlexa(log, cb) {
// If the data has already been retrieved, just return it
if (obj.data) {
cb(null, obj);
return;
}
// If it hasn't, invoke the retrieval function and schedule
// refreshes if we don't run into problems
retrieveAlexa((err, data) => {
if (err) {
cb(err, null);
return;
}
log('Retrieved Alexa rankings.');
// We return an object so that setInterval can change the Array
// the `data` property points to
obj.data = data;
setInterval(retrieveAlexa, DAY, (_err, _data) => {
// This is the callback function passed to retrieveAlexa()
if (_err) {
cb(_err, null);
return;
}
obj.data = _data;
log('Refreshed Alexa rankings.');
});
cb(null, obj);
});
};