2019-06-07 23:17:39 +03:00
|
|
|
const config = require('./config.json');
|
2019-06-13 04:13:15 +03:00
|
|
|
const escapeStringRegexp = require('escape-string-regexp');
|
2019-05-18 02:25:45 +03:00
|
|
|
const fs = require('fs');
|
|
|
|
const fetch = require('node-fetch');
|
2019-06-13 04:13:15 +03:00
|
|
|
const replace = require('replace-in-file');
|
|
|
|
|
|
|
|
const IGNORED_DOMAINS = config.ignoredDomains || [];
|
2019-06-14 19:20:07 +03:00
|
|
|
let DOMAINS_REGEXP_CACHE = [];
|
2019-06-13 04:13:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list without the domains specific in config.ignoredDomains
|
|
|
|
* @param {String} listFile
|
|
|
|
* @returns a String path to the CSV file
|
|
|
|
*/
|
|
|
|
const removeIgnoredDomains = function (listFile) {
|
2019-06-13 20:55:57 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-06-13 04:13:15 +03:00
|
|
|
// Modify the website list, if we have any ignoredDomains.
|
|
|
|
if (IGNORED_DOMAINS.length) {
|
2019-06-14 19:20:07 +03:00
|
|
|
if (!DOMAINS_REGEXP_CACHE.length){
|
|
|
|
DOMAINS_REGEXP_CACHE = IGNORED_DOMAINS.map((value, index) => {
|
|
|
|
// create an escaped regexp out of each domain we want to ignore
|
|
|
|
// the CSV format will look like one of the following (why tho):
|
|
|
|
// 1,example.com\r\n
|
|
|
|
// 1,example.com\n
|
|
|
|
return IGNORED_DOMAINS[index] = new RegExp(`\\d{1,3},${escapeStringRegexp(value)}\\r?\\n`);
|
|
|
|
});
|
|
|
|
}
|
2019-06-13 04:13:15 +03:00
|
|
|
console.log(`Skipping domains per config.ignoredDomains`);
|
|
|
|
replace({
|
|
|
|
countMatches: true,
|
|
|
|
files: listFile,
|
2019-06-14 19:20:07 +03:00
|
|
|
from: DOMAINS_REGEXP_CACHE,
|
2019-07-02 01:29:10 +03:00
|
|
|
to: '',
|
2019-06-13 04:13:15 +03:00
|
|
|
}).then(results => {
|
|
|
|
if (!results[0].hasChanged) {
|
|
|
|
console.warn('Warning: config.ignoredDomains set, but the list was not modified.');
|
|
|
|
}
|
|
|
|
resolve(listFile);
|
2019-06-13 20:55:57 +03:00
|
|
|
}).catch(error => reject(error));
|
2019-06-13 04:13:15 +03:00
|
|
|
} else {
|
|
|
|
resolve(listFile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2019-05-18 02:25:45 +03:00
|
|
|
|
2019-06-25 02:51:30 +03:00
|
|
|
/**
|
|
|
|
* Returns the list ID for the specified date or, if that cannot be found, the
|
|
|
|
* most recent one available. If a date is not specified, returns the latest
|
|
|
|
* available list.
|
|
|
|
* @param {Date} date the date of the requested list ID
|
|
|
|
* @returns the String of the list ID
|
|
|
|
*/
|
|
|
|
const fetchListID = async (date) => {
|
|
|
|
const LATEST_LIST_URL = 'https://tranco-list.eu/top-1m-id';
|
|
|
|
let ID_URL = LATEST_LIST_URL;
|
|
|
|
if (date) {
|
|
|
|
ID_URL = `https://tranco-list.eu/daily_list_id?date=${parseDate(date)}`;
|
|
|
|
} else {
|
|
|
|
date = new Date();
|
|
|
|
}
|
|
|
|
return fetch(ID_URL)
|
|
|
|
.then(res => {
|
|
|
|
if (res.ok &&
|
|
|
|
res.headers.get('content-type') === 'text/plain; charset=utf-8') {
|
|
|
|
return res.text();
|
|
|
|
}
|
|
|
|
else if (res.status === 503) {
|
|
|
|
const newDate = new Date(date);
|
2019-06-25 03:20:02 +03:00
|
|
|
// Future dates are unlikely to be available yet, but also ones
|
|
|
|
// from long ago may have never been available. Try to converge
|
|
|
|
// towards the present.
|
|
|
|
if (date > new Date()) {
|
|
|
|
newDate.setDate(newDate.getDate() - 1);
|
|
|
|
} else {
|
|
|
|
newDate.setDate(newDate.getDate() + 1);
|
|
|
|
}
|
2019-06-25 02:51:30 +03:00
|
|
|
console.warn(`Retrying with date ${newDate}`);
|
|
|
|
return fetchListID(newDate);
|
|
|
|
}
|
|
|
|
throw new Error(`Request for ${ID_URL} returned status ${res.status}!`);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-05-25 02:26:48 +03:00
|
|
|
const fetchList = async (size = 500, directory = "data/", date) => {
|
2019-06-13 04:13:15 +03:00
|
|
|
const listSize = size + IGNORED_DOMAINS.length;
|
2019-05-18 02:25:45 +03:00
|
|
|
|
|
|
|
// Create the data directory.
|
|
|
|
await new Promise((resolve, reject) => {
|
2019-05-25 02:26:48 +03:00
|
|
|
fs.mkdir(directory, { recursive: true }, (err) => {
|
2019-05-18 02:25:45 +03:00
|
|
|
if (err) reject(err);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-25 02:51:30 +03:00
|
|
|
// Fetch the list ID for the requested date.
|
|
|
|
const LIST_ID = await fetchListID(date);
|
|
|
|
if (!date) {
|
2019-05-25 02:26:48 +03:00
|
|
|
date = new Date();
|
|
|
|
}
|
|
|
|
const file = `${directory}list-${parseDate(date)}.csv`;
|
2019-05-18 02:25:45 +03:00
|
|
|
|
2019-05-25 02:26:48 +03:00
|
|
|
// Check for an already downloaded list.
|
|
|
|
const listIsCached = await fs.promises.access(file, fs.constants.R_OK | fs.constants.W_OK)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
|
|
|
if (listIsCached) {
|
|
|
|
console.log("Found cached Tranco list");
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the list.
|
2019-06-07 23:17:39 +03:00
|
|
|
const LIST_URL = `https://tranco-list.eu/download/${LIST_ID}/${listSize}`;
|
2019-06-13 04:13:15 +03:00
|
|
|
return fetch(LIST_URL).then(res => {
|
|
|
|
if (!res.ok ||
|
|
|
|
res.headers.get('content-type') !== 'text/csv; charset=utf-8') {
|
|
|
|
throw new Error(`List ${LIST_ID} not found!`);
|
|
|
|
}
|
2019-06-13 20:55:57 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-06-13 04:13:15 +03:00
|
|
|
const dest = fs.createWriteStream(file);
|
|
|
|
res.body.pipe(dest);
|
|
|
|
dest.on('finish', () => {
|
|
|
|
console.log(`Downloaded Tranco list with ID ${LIST_ID} for date ${parseDate(date)}`);
|
2019-06-13 20:55:57 +03:00
|
|
|
removeIgnoredDomains(file).then((newFile) => resolve(newFile), error => reject(error));
|
2019-05-18 02:25:45 +03:00
|
|
|
});
|
|
|
|
});
|
2019-06-13 04:13:15 +03:00
|
|
|
});
|
2019-05-25 02:26:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the specified date formatted as yyyy-mm-dd in UTC.
|
|
|
|
* @param {Date} date
|
|
|
|
* @returns a String representation of the date as yyyy-mm-dd
|
|
|
|
*/
|
|
|
|
function parseDate(date) {
|
|
|
|
return date.toISOString().split("T")[0];
|
2019-05-18 02:25:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-07-02 01:29:10 +03:00
|
|
|
fetchList,
|
2019-05-18 02:25:45 +03:00
|
|
|
}
|