-
Notifications
You must be signed in to change notification settings - Fork 1
/
web-content-analysis.js
278 lines (255 loc) · 11 KB
/
web-content-analysis.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
angular.module('WebContentAnalysis', ['ui.router', 'SocketIoNgService', 'UiRouterMenuService'])
.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}])
.config(['$stateProvider', '$urlRouterProvider', '$urlMatcherFactoryProvider', function($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) {
// defines a custom parameter type to prevent the escaping of url slashes
var uriTypePattern = /.*/;
function valToString(val) { return val !== null ? val.toString() : val; }
function regexpMatches(val) { return uriTypePattern.test(val); }
$urlMatcherFactoryProvider.type('uriType', {
encode: valToString, decode: valToString, is: regexpMatches, pattern: uriTypePattern
});
// routing
$stateProvider
// define a root state with the MenuController which will handle the menu
.state('root', {
abstract: true,
url: '/',
templateUrl: 'templates/root.html',
controller: 'MenuController as menuCtrl'
})
.state('root.content', {
url: 'content/{contentUrl:uriType}',
resolve: {
page: ['menuService', function(menuService) {
return menuService.setViewName('content');
}],
url: ['$stateParams', '$q', function($stateParams, $q) {
return $q.when($stateParams.contentUrl);
}]
},
views: {
content: {
templateUrl: 'templates/content.html',
controller: 'ContentController as contentCtrl'
}
}
})
.state('root.sitemap', {
url: 'sitemap/{sitemapUrl:uriType}',
resolve: {
page: ['menuService', function(menuService) {
return menuService.setViewName('sitemap');
}],
url: ['$stateParams', '$q', function($stateParams, $q) {
return $q.when($stateParams.sitemapUrl);
}]
},
views: {
content: {
templateUrl: 'templates/sitemap.html',
controller: 'SitemapController as sitemapCtrl'
}
}
})
;
$urlRouterProvider.otherwise('/content/');
}])
.factory('contentService', ['$q', 'socketService', function($q, socketService) {
function isStringNotEmpty(text) {
return 'string' === typeof text && text.length > 0;
}
var contentService = {
getContent: function(contentUrl) {
// returns the content for the given url
if (isStringNotEmpty(contentUrl)) {
return socketService.qemit('getContent', contentUrl);
}
// no content for empty urls
else {
return $q.when();
}
},
getSitemap: function(sitemapUrl) {
// returns the sitemap for the given url
if (isStringNotEmpty(sitemapUrl)) {
return socketService.qemit('getSitemap', sitemapUrl);
}
// no content for empty urls
else {
return $q.when();
}
},
verifyUrls: function(urls) {
if (urls && urls.length > 0) {
socketService.emit('verifyUrls', urls);
}
},
onVerifiedUrl: function(scope, callback) {
socketService.on('verifiedUrl', callback, scope);
},
stopVerifying: function() {
socketService.emit('stopVerifying');
}
};
return contentService;
}])
.controller('MenuController', ['menuService', function(menuService) {
this.page = menuService.page;
}])
// manages the sitemap analysis view
.controller('SitemapController', ['$state', '$scope', 'url', 'contentService', function($state, $scope, url, contentService) {
var sitemapCtrl = this;
sitemapCtrl.url = url;
sitemapCtrl.entryfilter = {};
sitemapCtrl.sitemap = [];
// reloads the state with the defined url
sitemapCtrl.scrap = function() {
if (url === sitemapCtrl.url) {
// shows the progress bar and starts retrieving the sitemap
sitemapCtrl.loading = true;
contentService.getSitemap(sitemapCtrl.url).then(function(sitemap) {
// hides the progress bar
sitemapCtrl.loading = false;
if (sitemap) {
// displays the error if any
if (sitemap.isErr) {
sitemapCtrl.failure = 'Failed to retrieve sitemap from ' + url;
sitemapCtrl.error = sitemap.error;
}
// empty sitemap
else if (!sitemap.content || sitemap.content.length < 1) {
sitemapCtrl.failure = 'no sitemap at this url';
}
// filters out duplicate urls
else {
// initializes the array of sitemap entries (objects) and duplicate urls (strings)
sitemapCtrl.sitemap = [];
sitemapCtrl.duplicates = [];
var processedLocations = [];
// filters unique and duplicate locations
sitemap.content.forEach(function(sitemapEntry) {
// flags duplicate sitemap locations
if (processedLocations.indexOf(sitemapEntry.loc) > 0) {
if (sitemapCtrl.duplicates.indexOf(sitemapEntry.loc) < 0) {
sitemapCtrl.duplicates.push(sitemapEntry.loc);
}
}
// flags processed sitemap locations
else {
sitemapCtrl.sitemap.push(sitemapEntry);
processedLocations.push(sitemapEntry.loc);
}
});
}
}
}, function() {
sitemapCtrl.loading = false;
sitemapCtrl.failure = 'Failed to retrieve sitemap from ' + url;
});
}
else {
$state.go('root.sitemap', {
sitemapUrl: sitemapCtrl.url
});
}
}
// starts scrapping the sitemap
sitemapCtrl.scrap();
// de/selects all entries
$scope.$watch('sitemapCtrl.selectAll', function(newValue) {
if (sitemapCtrl.sitemap) {
sitemapCtrl.sitemap.forEach(function(sitemapEntry) {
if (!sitemapEntry.verifying) {
sitemapEntry.verify = newValue;
}
});
}
});
// verifies the selected urls (http code, page title)
sitemapCtrl.verify = function() {
// collects the urls of the selected sitemap entries not being verified
var urlsToVerify = [];
sitemapCtrl.sitemap.reduce(function(urlsToVerify, sitemapEntry) {
if (sitemapEntry.verify && !sitemapEntry.verifying) {
urlsToVerify.push(sitemapEntry.loc);
sitemapEntry.verifying = true;
}
return urlsToVerify;
}, urlsToVerify);
contentService.verifyUrls(urlsToVerify);
};
// handles url verification events
contentService.onVerifiedUrl($scope, function(verifiedUrl) {
// finds the sitemap url related to the verification
var sitemapEntry = sitemapCtrl.sitemap.find(function(sitemapUrl) {
return sitemapUrl.loc === verifiedUrl.url;
});
if (sitemapEntry) {
if (verifiedUrl.isErr) {
sitemapEntry.title = verifiedUrl.error;
sitemapEntry.status = 'error';
}
else {
sitemapEntry.title = verifiedUrl.content.title;
sitemapEntry.status = verifiedUrl.content.status;
sitemapEntry.redirect = verifiedUrl.content.redirect;
}
sitemapEntry.verifying = false;
sitemapEntry.verify = false;
}
});
// stops verifying urls when leaving
$scope.$on('$destroy', contentService.stopVerifying);
}])
// manages the content scrapping view
.controller('ContentController', ['$state', 'url', 'contentService', function($state, url, contentService) {
var contentCtrl = this,
tagWeights = {
title: 15,
h1: 10,
h2: 9,
h3: 8,
h4: 7,
h5: 6,
h6: 5,
a: 4,
strong: 3,
b: 3,
i: 2
};
contentCtrl.url = url;
contentCtrl.scrap = function() {
contentCtrl.loading = true;
$state.go('root.content', {
contentUrl: contentCtrl.url
});
}
contentCtrl.loading = true;
contentService.getContent(url).then(function(content) {
contentCtrl.loading = false;
if ('object' === typeof content) {
if (content.isErr) {
contentCtrl.failure = 'Failed to retrieve content from ' + url;
contentCtrl.error = content.error;
}
else {
var wordsCache = content.content;
contentCtrl.words = [];
Object.keys(wordsCache).map(function(word) {
wordWeight = Object.keys(wordsCache[word]).reduce(function(weight, tag) {
tagWeight = tagWeights[tag] || 1;
return weight + (tagWeight * wordsCache[word][tag]);
}, 0);
contentCtrl.words.push({ label: word, weight: wordWeight, tags: wordsCache[word] });
});
}
}
}, function() {
contentCtrl.loading = false;
contentCtrl.failure = 'Failed to retrieve content from ' + url;
}
);
}])
;