Files
old-domene-svedska/crawler/helper/links.js
2017-12-11 18:22:06 +01:00

64 lines
2.0 KiB
JavaScript

const config = require('../config');
const https = require("https");
const punycode = require('punycode');
var fs = require('fs');
module.exports = {
getDomainList : function(url, callback){
getRawDomainList(url,(raw)=>{
let result = [];
raw.split('\n').map(domain=>{
let unicodeDomain = punycode.toUnicode(domain);
let dot = unicodeDomain.indexOf('.');
let tab = unicodeDomain.indexOf('\t');
if (dot !== -1){
let domainName = unicodeDomain.substring(0,dot);
let tld = unicodeDomain.substring(dot+1,tab);
if (domainName.match(config.swedishLettersOnly)){
//domain name contains only letters
//line in domain list is formatted as follows : [domain name]\t[expiration date]
result.push({domainName: domainName, tld:tld ,expirationDate: domain.split('\t')[1]});
}
}
});
applyFilter(result, callback);
});
}
};
var applyFilter = function (domains, callback){
//get domain names that only match whole words
let result = [];
domains.map(domain=>{
let index = config.words.indexOf(domain.domainName);
if (index !== -1){
result.push(domain);
}
});
callback(result);
}
var getRawDomainList = function (url, callback) {
if (url[0]==='/'){
//it's local file
fs.readFile(url,'utf8',(err,data)=>{
if (err){
console.log("err : " + err);
}else{
callback(data);
}
});
}else{
https.get(url, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
callback(body);
});
});
}
}