-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrawler.js
More file actions
304 lines (258 loc) · 8.46 KB
/
Copy pathcrawler.js
File metadata and controls
304 lines (258 loc) · 8.46 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
var request = require('request');
var cheerio = require('cheerio');
var maxEntries = 30;
var mainList = [];
var pageToVisit = '';
function setParameters(maxEntries, pageToVisit) {
this.pageToVisit = pageToVisit;
this.maxEntries = maxEntries;
}
function crawl(maxEntries, pageToVisit) {
setParameters(maxEntries, pageToVisit);
console.log("Visiting page " + pageToVisit);
request(pageToVisit, function (error, response, body) {
if (error) {
console.log("Error: " + error);
}
// Check status code (200 is HTTP OK)
console.log("Status code: " + response.statusCode);
if (response.statusCode === 200) {
// Parse the document body
var $ = cheerio.load(body);
console.log("Page title: " + $('title').text());
setAllAttributesToMainList($);
printAll30Entries();
performMoreThanFiveWordsProcess();
performLessThanSixWordsProcess();
}
});
}
function setAllAttributesToMainList($) {
setListFromAtributte($, '.rank', 'rank');
setListFromAtributte($, '.storylink', 'title');
setListFromAtributte($, '.score', 'score');
convertScoreInNumbers();
setItemsIdInlistItems($, '.athing');
var commentsList = getCommentsListIfContainWords($);
setCommentsinMainList(commentsList);
}
function printAll30Entries() {
console.log("The 30th first items in the page are:")
console.log(mainList);
}
function performMoreThanFiveWordsProcess() {
console.log("Entries with more than five words in the title ordered by amount of comments: ");
moreThanFiveWordsList = getEntriesWithMoreThanFiveWordsInTitle();
moreThanFiveWordsList.sort(compareByNumberOfComments);
console.log(moreThanFiveWordsList);
}
function performLessThanSixWordsProcess() {
console.log("Entries with less than or equal to five words in the title ordered by points: ");
lessThanSixWordsList = getEntriesWithLessThanWordsInTitle();
lessThanSixWordsList.sort(compareByScore);
console.log(lessThanSixWordsList);
}
function compareByNumberOfComments(a, b) {
if (a.numberOfComments < b.numberOfComments)
return -1;
if (a.numberOfComments > b.numberOfComments)
return 1;
return 0;
}
function compareByScore(a, b) {
if (a.score < b.score)
return -1;
if (a.score > b.score)
return 1;
return 0;
}
function convertScoreInNumbers() {
mainList.forEach(function (element) {
var score;
var finalIndex = element.score.indexOf('points');
score = element.score.substring(0, finalIndex - 1);
element.score = Number(score);
}.bind(this));
}
function getEntriesWithMoreThanFiveWordsInTitle() {
var list = [];
mainList.forEach(function (element) {
var words = countWords(element.title);
if (words > 5) {
list.push(element);
}
}.bind(this));
return list;
}
function getEntriesWithLessThanWordsInTitle() {
var list = [];
mainList.forEach(function (element) {
var words = countWords(element.title);
if (words <= 5) {
list.push(element);
}
}.bind(this));
return list;
}
////
// Set the id in each object of an array
// number of entries and set the corresponding attribute and value to each object in the mainList array
//
//@param {$} cheerio object to be read.
//@param {element} could be an tag name, id, class, property
//
////
function setItemsIdInlistItems($, element) {
$(element).slice(0, maxEntries).each(function (i, elem) {
mainList[i].id = $(this).attr('id');
});
}
////
// Given an tag name, property or id gets all the elements of that type, then slice the array result until the max
// number of entries and set the corresponding attribute and value to each object in the mainList array
//
//@param {$} cheerio object to be read.
//@param {element} could be an tag name, id, class, property
//@param {attribute} the name of the attribute to be set on each object
//
////
function setListFromAtributte($, element, attribute) {
$(element).slice(0, maxEntries).each(function (i, elem) {
if (!mainList[i]) {
mainList[i] = {};
}
mainList[i][attribute] = $(this).text();
});
}
////
// Extract the id of the relative direction of a link
//
//@param {$} cheerio object to be read.
//@param {link} relative path that has the id of entry
//
//@return {object} Object with the result of the comparison.
//
////
function getIdsFromComments($, link) {
var relativeDirection = $(link).attr('href');
var startNumber = relativeDirection.indexOf('=') + 1;
return relativeDirection.substring(startNumber, relativeDirection.length);
}
////
// CGet all the links of a page
//
//@param {$} cheerio object to be read.
//
//@return {array} Array of links of the page.
//
////
function getLinksOfPage($) {
var links = $('a'); //jquery get all hyperlinks
return links;
}
////
// Compare a string with the comment pattern to see if it is a comments line
//
//@param {$} cheerio object to be read.
//@param {link} relative path that has the id of entry
//
//@return {object} Object with the result of the comparison.
//
////
function compareIfMatchCommentFormat($, link) {
var commentPattern = /^[0-9].+comments$/;
var comment = $(link).text();
var matchResult = comment.match(commentPattern);
return matchResult;
}
////
// Push an item of type comment to the list that will contain all the comments
// of comments
//
//@param {$} cheerio object to be read.
//@param {list} array of objects
//@param {comment} string with the number of comments
//@param {link} relative path that has the id of entry
//@param {numberOfComments} number of comments
//
//@return {array} Description.
//
////
function addCommentToList($, list, comment, link, numberOfComments) {
list.push(
{comments: comment, id: getIdsFromComments($, link), numberOfComments: Number(numberOfComments)}
);
}
////
// Search all the links in file then search if there is a comment or a discuss and set the value of the comment in a list
// of comments
//
//@param {$} cheerio object to be read.
//
//@return {array} Description.
//
////
function getCommentsListIfContainWords($) {
var list = [];
var numberOfComments = 0;
$(getLinksOfPage($)).each(function (i, link) {
var matchResult = compareIfMatchCommentFormat($, link);
var comment = matchResult ? matchResult.input : '';
if (matchResult) {
var finalIndex = comment.indexOf('comments');
numberOfComments = comment.substring(0, finalIndex - 1);
addCommentToList($, list, comment, link, numberOfComments)
}
else if (comment === 'discuss') {
addCommentToList($, list, comment, link, numberOfComments);
}
});
return list;
}
////
// Set the comment and the value of this in the corresponding item in the mainList
//
//@param {arrayList} list of comments to set in mainList.
//
////
function setCommentsinMainList(commentsList) {
var commentsLenght = commentsList.length;
var mainListLenght = mainList.length;
for (var i = 0; i < commentsLenght; i++) {
for (var j = 0; j < mainListLenght; j++) {
if (mainList[j].id === commentsList[i].id) {
mainList[j].comments = commentsList[i].comments;
mainList[j].numberOfComments = commentsList[i].numberOfComments;
}
}
}
}
//Count words in a string
countWords: function countWords(str) {
var count = 0;
var words = str.split(" ");
for (var i = 0; i < words.length; i++) {
// inner loop -- do the count
if (words[i] !=="")
count += 1;
}
return count;
}
module.exports = {
addCommentToList:addCommentToList,
countWords: countWords,
crawl:crawl,
getEntriesWithMoreThanFiveWordsInTitle:getEntriesWithMoreThanFiveWordsInTitle,
getEntriesWithLessThanWordsInTitle:getEntriesWithLessThanWordsInTitle,
convertScoreInNumbers: convertScoreInNumbers,
compareByNumberOfComments: compareByNumberOfComments,
compareByScore:compareByScore,
setItemsIdInlistItems: setItemsIdInlistItems,
setCommentsinMainList: setCommentsinMainList,
setParameters: setParameters,
getIdsFromComments:getIdsFromComments,
getLinksOfPage:getLinksOfPage,
compareIfMatchCommentFormat:compareIfMatchCommentFormat,
setListFromAtributte:setListFromAtributte,
getCommentsListIfContainWords: getCommentsListIfContainWords,
};