-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-crawler.js
180 lines (149 loc) · 4.53 KB
/
web-crawler.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
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
const puppeteer = require('puppeteer');
const $ = require('cheerio');
const path = require('path');
const Page = require('./model/Page');
const Word = require('./model/Word');
const PageWord = require('./model/PageWord');
async function getDataFromURL(urls) {
let start = Date.now();
let badlinks = [];
while (urls.length > 0) {
let url = path.normalize(urls.shift());
while (
(await isURLinDatabase(url)) ||
path.extname(url) == '.pdf' ||
url in badlinks
) {
if (urls.length == 0) {
let time = (Date.now() - start) / 1000;
console.log(`Index completed in ${time} seconds.`);
return;
}
url = path.normalize(urls.shift());
}
const fetchedData = await downloadURL(url).catch(error => {
console.log(`[Timeout]: ${url}`);
badlinks.push(url);
});
if (fetchedData == null || fetchedData.header.status == '404') continue;
const data = extractInfo(fetchedData, url);
await addToDatabase(data[0], data[2]);
urls = urls.concat(data[1]);
console.log(`Completed Index of [${url}]`);
}
let time = (Date.now() - start) / 1000;
console.log(`Index completed in ${time} seconds.`);
}
async function downloadURL(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const response = await page.goto(url);
const html = await page.content();
const header = response.headers();
await browser.close();
return { header, html };
}
function extractInfo(fetchedData, site) {
const html = fetchedData.html;
const header = fetchedData.header;
let { url, title, description, lastModified, lastIndexed, timeToIndex } = {};
let start = Date.now();
url = site;
title = $('title', html).text();
description = $('meta[name="description"]', html).attr('content');
if (description != null && description.length > 200)
description = description.substring(0, 196) + '...';
else if (description == null) {
description = 'Description is unavailable';
}
timeToIndex = (Date.now() - start) / 1000;
if (header.hasOwnProperty('last-modified')) {
lastModified = new Date(header['last-modified'].split(',')[1]);
lastModified = lastModified
.toISOString()
.slice(0, 19)
.replace('T', ' ');
}
links = scrapLinks(html, url);
let words = scrapWords(html);
return [
{ url, title, description, lastModified, lastIndexed, timeToIndex },
links,
words
];
}
async function addToDatabase(pageData, wordData) {
const pageRow = await Page.create(pageData);
Object.keys(wordData).forEach(async key => {
let wordRow = await Word.findAll({
where: {
wordName: `"${key}"`
}
});
if (wordRow.length == 0) {
wordRow = await Word.create({ wordName: key });
PageWord.create({
pageId: pageRow.toJSON().id,
wordId: wordRow.toJSON().id,
freq: wordData[key]
});
} else {
PageWord.create({
pageId: pageRow.toJSON().id,
wordId: wordRow[0].toJSON().id,
freq: wordData[key]
});
}
});
}
async function isURLinDatabase(link) {
const values = await Page.findAll({
where: {
url: link
}
});
let result = values.length > 0;
if (result) result = (new Date() - values[0].lastIndexed) / 1000 < 86400;
return result;
}
function scrapLinks(html, url) {
let links = [];
let baseurl = url.split('/')[0] + '/' + url.split('/')[1];
$('a[href]', html).each((i, element) => {
let link = element.attribs['href'];
if (path.isAbsolute(link) && link != '/' && !(baseurl in links)) {
links.push(path.join(baseurl, link));
}
});
return links;
}
function addToObject(words, wordList) {
wordList.forEach(element => {
if (element in words) words[element] = words[element] + 1;
else words[element] = 1;
});
}
function extractTextFromElement(element, words) {
let wordList = [];
element.children.forEach(element => {
if (element.type == 'text')
element.data.split(/[\s\-\/]/).forEach(element => {
let data = element.replace(/[\'\.\,\"\:\?]/g, '');
data = data.replace(/\//g, ' ');
if (data.trim() != '' && data.substr(0, 4) != 'http')
wordList.push(data);
});
});
addToObject(words, wordList);
}
function scrapWords(html) {
let words = {};
let elementToCheck = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li'];
elementToCheck.forEach(element => {
$(element, html).each((index, element) =>
extractTextFromElement(element, words)
);
});
return words;
}
module.exports = getDataFromURL;