-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetExchangeVersion.js
92 lines (81 loc) · 3.03 KB
/
getExchangeVersion.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
80
81
82
83
84
85
86
87
88
89
90
91
92
const { join } = require('path');
const hubspot = require(join(__dirname, 'HubSpot.js'))
var os = require('os');
var request = require('request-promise');
const key = process.env.HUBSPOT_KEY
const parseDomain = require('parse-domain')
const cheerio = require('cheerio')
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
const getDomain = (company) => `${parseDomain(company.properties.website.value).domain}.${parseDomain(company.properties.website.value).tld}`
const getExchangeVersion = (html) => {
const $ = cheerio.load(html)
const icon = $('link[rel="shortcut icon"]').prop('href')
if (icon) {
if (icon.startsWith("/owa/auth/")) {
const exchangeVersion = icon.split('/')[3]
return exchangeVersion
} else if (icon.startsWith('/owa/')) {
const exchangeVersion = icon.split('/')[2]
return exchangeVersion
}
}
}
const crawlOWA = (domain, subdomain = "mail") => request({
uri: `https://${subdomain}.${domain}/owa`,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
'Accept-Language': 'en-US, en;q=0.8',
Accept: 'text / html, application/xhtml+xml,application/xml;q=0.9, image/webp,*;q=0.8'
},
timeout: 1500
}).then(getExchangeVersion)
const updateExchangeVersion = (company, url, exchangeVersion) => request({
method: 'PUT',
uri: `https://api.hubapi.com/companies/v2/companies/${company.companyId}?hapikey=${key}`,
body: {
"properties": [
{
"name": "exchange_version",
"value": exchangeVersion
},
{
"name": "exchange_owa_url",
"value": url
}
]
},
json: true
})
const getSetExchangeVersion = (company) => {
const domain = getDomain(company)
crawlOWA(domain, 'mail').then((exchangeVersion) => {
updateExchangeVersion(company, `mail.${domain}`, exchangeVersion)
}, (mailErr) => {
crawlOWA(domain, 'webmail').then((webmailExchangeVersion) => {
updateExchangeVersion(company, `webmail.${domain}`, webmailExchangeVersion)
}, (webmailerr) => {
crawlOWA(domain, 'smtp').then((smtpExchangeVersion) => {
updateExchangeVersion(company, `smtp.${domain}`, smtpExchangeVersion)
}, (smtpErr) => {
console.log(`${domain}: Exchange OWA URL not found`)
})
})
})
}
const run = () => {
hubspot.getCompanies(["website", "email_provider", "spam_filter_service", "mx_records"]).then((companies) => {
const companiesWithMx = companies.filter((company) => Boolean(company.properties.mx_records) && !company.properties.email_provider.value)
companiesWithMx.forEach((company, idx) => {
setTimeout(() => {
getSetExchangeVersion(company)
}, idx * 500)
})
})
}
//run()
crawlOWA("bazless.com", 'mail').then((exchangeVersion) => {
console.log(exchangeVersion)
//updateExchangeVersion(company, `mail.${domain}`, exchangeVersion)
}, (err) => {
console.log(err)
})