-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
314 lines (263 loc) · 7.69 KB
/
server.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
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
const express = require('express')
let app = express()
let http = require('http').createServer(app)
app.set('view engine', 'ejs')
app.use('/public', express.static(__dirname + '/public'))
let io = require('socket.io')(http, {
cors: {
origin: '*',
},
})
const requestModule = require('request')
const cheerio = require('cheerio')
const formidableMiddleware = require('express-formidable')
app.use(formidableMiddleware())
let mongodb = require('mongodb')
let mongoClient = mongodb.MongoClient
let htmlspecialchars = require('htmlspecialchars')
let HTMLParser = require('node-html-parser')
let database = null
function getTagContent(querySelector, content) {
let tags = content.querySelectorAll(querySelector)
let innerHTMLs = []
for (let a = 0; a < tags.length; a++) {
let content = ''
let anchorTag = tags[a].querySelector('a')
if (anchorTag != null) {
content = anchorTag.innerHTML
} else {
content = tags[a].innerHTML
}
content = content.replace(/\s+/g, ' ').trim()
if (content.length > 0) {
innerHTMLs.push(content)
}
}
return innerHTMLs
}
function crawlPage(url, callBack = null) {
let pathArray = url.split('/')
let protocol = pathArray[0]
let host = pathArray[2]
let baseUrl = protocol + '//' + host
io.emit('crawl_update', 'Crawling page: ' + url)
requestModule(url, async function (error, response, html) {
if (!error && response.statusCode == 200) {
let $ = cheerio.load(html)
let page = await database.collection('pages').findOne({
url: url,
})
if (page == null) {
let html = $.html()
let htmlContent = HTMLParser.parse(html)
let allAnchors = htmlContent.querySelectorAll('a')
let anchors = []
for (let a = 0; a < allAnchors.length; a++) {
let href = allAnchors[a].getAttribute('href')
let title = allAnchors[a].innerHTML
let hasAnyChildTag =
allAnchors[a].querySelector('div') != null ||
allAnchors[a].querySelector('img') != null ||
allAnchors[a].querySelector('p') != null ||
allAnchors[a].querySelector('span') != null ||
allAnchors[a].querySelector('svg') != null ||
allAnchors[a].querySelector('strong') != null
if (hasAnyChildTag) {
continue
}
if (href != null) {
if (href == '#' || href.search('javascript:void(0)') != -1) {
continue
}
let first4Words = href.substr(0, 4)
if (href.search(url) == -1 && first4Words != 'http') {
if (href[0] == '/') {
href = baseUrl + href
} else {
href = baseUrl + '/' + href
}
}
anchors.push({
href: href,
text: title,
})
}
}
io.emit(
'crawl_update',
htmlspecialchars('<a>') + ' tags has been crawled',
)
let titles = await getTagContent('title', htmlContent, url)
let title = titles.length > 0 ? titles[0] : ''
io.emit(
'crawl_update',
htmlspecialchars('<title>') + ' tag has been crawled',
)
let h1s = await getTagContent('h1', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h1>') + ' tags has been crawled',
)
let h2s = await getTagContent('h2', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h2>') + ' tags has been crawled',
)
let h3s = await getTagContent('h3', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h3>') + ' tags has been crawled',
)
let h4s = await getTagContent('h4', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h4>') + ' tags has been crawled',
)
let h5s = await getTagContent('h5', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h5>') + ' tags has been crawled',
)
let h6s = await getTagContent('h6', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<h6>') + ' tags has been crawled',
)
let ps = await getTagContent('p', htmlContent, url)
io.emit(
'crawl_update',
htmlspecialchars('<p>') + ' tags has been crawled',
)
let object = {
url: url,
anchors: anchors,
title: title,
h1s: h1s,
h2s: h2s,
h3s: h3s,
h4s: h4s,
h5s: h5s,
h6s: h6s,
ps: ps,
time: new Date().getTime(),
}
try {
await database.collection('pages').insertOne(object)
} catch (e) {
console.log(e)
}
io.emit('page_crawled', object)
io.emit('crawl_update', 'Page crawled.')
} else {
io.emit('crawl_update', 'Page already crawled.')
}
if (callBack != null) {
callBack()
}
}
})
}
let months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
http.listen(3000, function () {
console.log('Server started running at port: 3000')
require('dotenv').config()
const uri = process.env.URI
mongoClient.connect(
uri,
{
useUnifiedTopology: true,
},
function (error, client) {
if (error) {
throw error
}
database = client.db('web_crawler')
console.log('Database connected')
app.post('/reindex', async function (request, result) {
let url = request.fields.url
await database.collection('pages').deleteOne({
url: url,
})
io.emit('page_deleted', url)
crawlPage(url, function () {
let backURL = request.header('Referer') || '/'
result.redirect(backURL)
})
})
app.post('/delete-page', async function (request, result) {
let url = request.fields.url
await database.collection('pages').deleteOne({
url: url,
})
io.emit('page_deleted', url)
let backURL = request.header('Referer') || '/'
result.redirect(backURL)
})
app.get('/page/:url', async function (request, result) {
let url = request.params.url
let page = await database.collection('pages').findOne({
url: url,
})
if (page == null) {
result.render('404', {
message: 'This page has not been crawled',
})
return false
}
result.render('page', {
page: page,
})
})
app.post('/crawl-page', async function (request, result) {
let url = request.fields.url
crawlPage(url)
result.json({
status: 'success',
message: 'Page has been crawled',
url: url,
})
})
app.get('/', async function (request, result) {
let pages = await database
.collection('pages')
.find({})
.sort({
time: -1,
})
.toArray()
for (let index in pages) {
let date = new Date(pages[index].time)
let time =
date.getDate() +
' ' +
months[date.getMonth() + 1] +
', ' +
date.getFullYear() +
' - ' +
date.getHours() +
':' +
date.getMinutes() +
':' +
date.getSeconds()
pages[index].time = time
}
result.render('index', {
pages: pages,
})
})
},
)
})