-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
359 lines (341 loc) · 11.8 KB
/
app.js
File metadata and controls
359 lines (341 loc) · 11.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* Created by Shane on 14/07/2017.
*/
// Libraries/Includes
var Knwl = require('knwl.js');
var cheerio = require('cheerio');
var http = require('http');
var request = require('request');
var fs = require('fs');
// var hb = require('handlebars');
var express = require('express');
var app = express();
var expressHbs = require('express-handlebars');
var googleMapsClient = require('@google/maps').createClient({
key: 'INSERT HERE'
});
// Variables
var inputEmail = process.argv[2]; // Input email is taken from commandline arguments
var domain; // Domain take from email address
var knwlInstance = new Knwl('english'); // Knwl instance for using Knwl functions
var siteDir; // Folder directory of the site where data is to be stored
var words; // All words on website scraped by Knwl
var bodyG; // Global body variable
var finalEmails = []; // Final processed emails
var finalPhoneNumbers = []; // Final processed phonenumbers
var scrapeObj = {}; // Final object storing scraped data
var homePageTitle; // Title of home page
var hyperLinks = []; // Array of hyperlinks on the domain
var address; // Address of company from GoogleMaps API
var place; // GoogleMaps API place variable
var companyName; // Name of company from GoogleMaps API
var companyRating; // Rating of company from GoogleMaps API
var googleMapsResults; // JSON GoogleMaps API query result
var googleMapsOpenNow; // Asks GoogleMaps API if company is open now, true or false
var port = '8000'; // The port that the webserver will run on
//Load Knwl plugins
knwlInstance.register('emails', require('knwl.js/default_plugins/emails'));
knwlInstance.register('internationalPhones', require('knwl.js/experimental_plugins/internationalPhones'));
knwlInstance.register('places', require('knwl.js/default_plugins/places'));
/**
* Uses commandline arguments to grab domain from email
*/
if (process.argv[2] != null) {
domain = grabDomain(inputEmail);
} else {
console.log('Missing email arguments please refer to README.MD');
process.exit();
}
// Initialize express and start web server
app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs'}));
app.set('view engine', 'hbs');
startWebServer();
/**
* Main promise chain that steps through in the following order:
* 1. Makes request to domain - Returns body
* - Creates directory for storing scraped data
* - Grabs words from body using Knwl
* 2. Loads body in to cheerio - returns $ (Cheerio API enabled HTML body)
* 3. Finds all hyperlinks on page - returns link array
* - Uses links/$ to find phone numbers and save to array
* - Uses links/body to find email addresses and save to array
* 4. Use Google API to locate the address of the company
* 5. Outputs the scraped data to console and json file in corresponding domain folder
* - Updates the webpage hosted on localhost:8000 with the scraped data
*
* @type {Promise}
*/
var body = makeRequest(); //sets the first promise in the sequence
body.then(function (body) {
createDir();
words = getWords(body);
bodyG = body;
return loadToCheerio(body);
}).then(function ($) {
homePageTitle = $('title').text();
return findHyperLinks($);
}).then(function (links) {
findPhoneNumber(links);
findEmails(bodyG);
hyperLinks = links;
return findOnGoogleMaps(domain);
}).then(function () {
}).then(function () {
scrapeObj = {
'website': domain,
'URL': 'http://www.' + domain,
'homepageName': homePageTitle,
'companyName': companyName,
'companyRating': companyRating.toString(),
'openNow': googleMapsOpenNow.toString(),
'address': address,
'emails': finalEmails,
'telephone': finalPhoneNumbers,
};
var json = JSON.stringify(scrapeObj, null, 4);
var domainS = domain.split('.')[0]; // Take away the domain suffix, so domain name can be used as filename
fs.writeFile(siteDir + domainS + '.json', json, 'utf8');
console.dir(scrapeObj, { // Display json in console
depth: null,
colors: true,
});
updateWebpage();
console.log('Webserver running, results are displayed at http://localhost:' + port
+ ' press Ctrl+C twice to stop');
}).catch(function (error) {
// console.log(error);
});
/**
* Starts up the webserver that will display the scraped data on localhost:8000
*/
function startWebServer(){
app.get('/', function(req, res){
res.render('index', scrapeObj);
});
app.listen(8000);
}
/**
* Updates the webpage after the data has been scraped
*/
function updateWebpage(){
app.get('/', function(req, res){
res.render('index', scrapeObj);
});
}
/**
* Grabs the domain part of any given email by splitting it at the "@" symbol
* Also creates a directory for the given domain if one does not already exist
*
* @param ie - Email entered in arguments
* @returns {*} - Domain
*/
function grabDomain(ie) {
domain = ie.split('@')[1];
return domain;
}
/**
* Simple function that creates a directory, for each domain, where the scraped
* data is stored. A check is done to make sure the directory doesn't already
* exist before creation.
*/
function createDir() {
siteDir = './scraped sites/' + domain + '/';
if (!fs.existsSync(siteDir)) {
fs.mkdirSync(siteDir);
}
}
/**
* Makes the request to the domain to get the body
*
* @returns {Promise}
* @resolve {variable} - Resolves if body of site is obtained successfully
* @reject {err} - Rejects promise if error is given
*/
function makeRequest() {
return new Promise(function (resolve, reject) {
request({
'rejectUnauthorized': false,
'url': 'http://www.' + grabDomain(inputEmail),
'method': 'GET',
}, function (error, response, body) {
if (!error) {
if (response.statusCode === 200) {
console.log('StatusCode OK');
resolve(body);
} else {
console.log('Bad response code: ' + response.statusCode);
reject(response.statusCode);
}
} else {
console.log(error);
console.log('Unable to scrape given domain, exiting program (see error above)');
}
});
});
}
/**
* Loads the body generated by the request into Cheerio
*
* @param body - The HTML body returned from the original request
* @returns {Promise}
* @resolve {variable} - Cheerio loaded HTML body
*/
function loadToCheerio(body) {
return new Promise(function (resolve, reject) {
var $ = cheerio.load(body, {
normalizeWhitespace: true,
xmlMode: true,
decodeEntities: true,
withDomLvl1: true,
});
resolve($);
});
}
/**
* Checks if the variable found already exists in a given array
*
* @param entry - Proposed variable
* @param array - Array to check for duplicate in
* @returns {boolean} - Returns true if duplicate found
*/
function checkDuplicate(entry, array) {
for (var i = 0; i < array.length; i++) {
if (array[i] == entry) {
return true;
}
}
return false;
}
/**
* Finds all phone numbers on websites using two methods:
* Method 1 - Searches links array for hyperlinks containing "tel:"
* Method 2 - Uses Knwl to find all international phone numbers on website
*
* Using both methods has a higher success rate than using one or the other
*
* @param links
*/
function findPhoneNumber(links) {
links.forEach(function (link) { // Method 1
if (link.indexOf('tel:') !== -1) {
link = link.split(':')[1];
link = link.replace(/\s/g, '');
if (link.charAt(0) == 0) {
link = link.replace(link.charAt(0), '+44');
}
if (link.length > 5) {
if (!checkDuplicate(link, finalPhoneNumbers)) {
finalPhoneNumbers.push(link);
}
}
}
});
var phones = knwlInstance.get('internationalPhones'); // Method 2
phones.forEach(function (phone) {
if (phone) {
if (!checkDuplicate(phone['number'], finalPhoneNumbers)) {
finalPhoneNumbers.push(phone['number']);
}
}
});
}
/**
* Grabs all emails on the website using Knwl and pushes them to the finalEmails array
*
* @returns {boolean} - True if emails are found, false if not
*/
function findEmails() {
var emails = knwlInstance.get('emails');
var anyEmails = false;
emails.forEach(function (email) {
if (email) {
if (!checkDuplicate(email['address'], finalEmails)) {
finalEmails.push(email['address']);
}
anyEmails = true;
}
});
return anyEmails;
}
/**
* Grabs all hyperlinks on the website using cheerios stored HREF tags
*
* @param $ - Cheerio API loaded HTML body
* @returns {Promise}
* @resolve {array} - Resolves and returns array of hyperlinks
*/
function findHyperLinks($) {
return new Promise(function (resolve, reject) {
anchors = $('a'); //Grabs the anchors from cheerio
links = [];
$(anchors).each(function (i, anchor) { //For each anchor on the website grab the href.
links.push($(anchor).attr('href'));
});
resolve(links);
});
}
/**
* Uses Knwl to grab all words on the given website
*
* @param body - The HTML body returned from the original request
*/
function getWords(body) {
knwlInstance.init(body);
return (knwlInstance.words.get('linkWordsCasesensitive'));
}
/**
* This function uses the google maps API to find information about the company
* who owns the given domain the following information is pulled from the API:
*
* Company Name
* Formatted address
* Company Rating
* If they are open or not right now
*
* @param domain
* @returns {Promise}
* @resolve {string} - Resolves formatted address from Google Maps API
* @reject {error} - Rejected if an error occurs when accessing Google API
*/
function findOnGoogleMaps(domain) {
return new Promise(function (resolve, reject) {
place = googleMapsClient.places({
query: domain,
language: 'en',
location: 'United Kingdom',
}, function (err, response) {
if (!err) {
googleMapsResults = response.json.results[0];
if (response.json.results[0].formatted_address) {
address = response.json.results[0].formatted_address;
}else{
address = "Not given";
}
if(response.json.results[0].name) {
companyName = response.json.results[0].name;
}else{
companyName = "Not given";
}
if(response.json.results[0].rating) {
companyRating = response.json.results[0].rating;
}else{
companyRating = "Not given";
}
if(response.json.results[0].opening_hours) {
googleMapsOpenNow = response.json.results[0].opening_hours.open_now;
}else{
googleMapsOpenNow = "Not given";
}
resolve(address);
} else {
console.log(response);;
console.log(err);
reject(err);
}
});
});
}
/**
* To-Do list
* - Minor tidying
*/