-
Notifications
You must be signed in to change notification settings - Fork 0
/
contactfinder.js
79 lines (71 loc) · 1.82 KB
/
contactfinder.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
const request = require('superagent');
var Promise = this.Promise || require('promise');
const URL = require('url');
var agent = require('superagent-promise')(request, Promise);
const cheerio = require('cheerio');
var whois = require('whois-ux');
module.exports = checkContact;
function checkContact(url) {
return Promise.all([
getDNSEmail(url2domain(url)),
getHTMLContacts(url)
]).then(function (data) {
return mergeObjs(data);
})
.catch(function (err) {
console.log(err);
});
}
function getHTMLContacts(htmlUrl) {
return new Promise(function (accept, reject) {
agent
.get("http://" + htmlUrl)
.end()
.then(function(res) {
const $ = cheerio.load(res.text);
accept({
'contactUrl': getContact($),
'emailFromHtml': getEmail(res.text)
});
})
.catch(function (err) {
reject(err);
});
});
}
function getEmail(html) {
let email = /[\w-]+@([\w-]+\.)+[\w-]+/.exec(html);
if (email !== null && email !== 0) {
return email[0];
} else {
return null;
}
}
function getContact($) {
if ($("a[href*='contact']").length !== 0) {
return $("a[href*='contact']")[0].attribs.href;
} else {
return null;
}
}
function getDNSEmail(domain) {
return new Promise(function (accept, reject) {
whois.whois(domain, function (err, data){
var email = data['Registrant Email'] || null;
accept({ 'emailFromDns': email });
});
});
}
function url2domain(fullUrl) {
return fullUrl.includes('://') ? (new URL.URL(fullUrl)).hostname : fullUrl;
}
function mergeObjs(objs) {
return objs.reduce(function(result, currentObject) {
for(var key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
}
}
return result;
}, {});
}