-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.js
315 lines (243 loc) · 7.21 KB
/
index.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
315
var util = require('util');
var request = require('request');
var events = require('events');
var cheerio = require('cheerio');
var URI = require('uri-js');
var _ = require('lodash');
var debug;
if (/\bmetainspector\b/.test(process.env.NODE_DEBUG)) {
debug = function() {
console.error('METAINSPECTOR %s', util.format.apply(util, arguments));
};
} else {
debug = function() {};
}
function withDefaultScheme(url) {
return URI.parse(url).scheme ? url : "http://" + url;
}
var MetaInspector = function(url, options) {
this.url = URI.normalize(withDefaultScheme(url));
this.parsedUrl = URI.parse(this.url);
this.scheme = this.parsedUrl.scheme;
this.host = this.parsedUrl.host;
this.rootUrl = this.scheme + "://" + this.host;
this.options = options || {};
//default to a sane limit, since for meta-inspector usually 5 redirects should do a job
//more over beyond this there could be an issue with event emitter loop detection with new nodejs version
//which prevents error event from getting fired
this.options.maxRedirects = this.options.maxRedirects || 5;
//some urls are timing out after one minute, hence need to specify a reasonable default timeout
this.options.timeout = this.options.timeout || 20000; //Timeout in ms
this.options.strictSSL = !!this.options.strictSSL;
this.options.headers = this.options.headers || { 'User-Agent' : 'MetaInspector/1.0' };
};
//MetaInspector.prototype = new events.EventEmitter();
// MetaInspector.prototype.__proto__ = events.EventEmitter.prototype;
util.inherits(MetaInspector, events.EventEmitter);
module.exports = MetaInspector;
MetaInspector.prototype.getTitle = function() {
debug("Parsing page title");
if(!this.title)
{
this.title = this.parsedDocument('head > title').text();
}
return this;
};
MetaInspector.prototype.getOgTitle = function() {
debug("Parsing page Open Graph title");
if(!this.ogTitle)
{
this.ogTitle = this.parsedDocument("meta[property='og:title']").attr("content");
}
return this;
};
MetaInspector.prototype.getOgDescription = function() {
debug("Parsing page Open Graph description");
if(this.ogDescription === undefined)
{
this.ogDescription = this.parsedDocument("meta[property='og:description']").attr("content");
}
return this;
};
MetaInspector.prototype.getOgType = function() {
debug("Parsing page's Open Graph Type");
if(this.ogType === undefined)
{
this.ogType = this.parsedDocument("meta[property='og:type']").attr("content");
}
return this;
};
MetaInspector.prototype.getOgUpdatedTime = function()
{
debug("Parsing page's Open Graph Updated Time");
if(this.ogUpdatedTime === undefined)
{
this.ogUpdatedTime = this.parsedDocument("meta[property='og:updated_time']").attr("content");
return this;
}
};
MetaInspector.prototype.getOgLocale = function() {
debug("Parsing page's Open Graph Locale");
if(this.ogLocale === undefined)
{
this.ogLocale = this.parsedDocument("meta[property='og:locale']").attr("content");
}
return this;
};
MetaInspector.prototype.getLinks = function() {
debug("Parsing page links");
var _this = this;
if(!this.links)
{
this.links = this.parsedDocument('a').map(function(i ,elem) {
return _this.parsedDocument(this).attr('href');
});
}
return this;
};
MetaInspector.prototype.getMetaDescription = function() {
debug("Parsing page description based on meta elements");
if(!this.description)
{
this.description = this.parsedDocument("meta[name='description']").attr("content");
}
return this;
};
MetaInspector.prototype.getSecondaryDescription = function() {
debug("Parsing page secondary description");
var _this = this;
if(!this.description) {
var minimumPLength = 120;
this.parsedDocument("p").each(function(i, elem) {
if(_this.description) {
return;
}
var text = _this.parsedDocument(this).text();
// If we found a paragraph with more than
if(text.length >= minimumPLength) {
_this.description = text;
}
});
}
return this;
};
MetaInspector.prototype.getDescription = function() {
debug("Parsing page description based on meta description or secondary description");
this.getMetaDescription();
this.getSecondaryDescription();
return this;
};
MetaInspector.prototype.getKeywords = function() {
debug("Parsing page keywords from apropriate metatag");
if(!this.keywords) {
var keywordsString = this.parsedDocument("meta[name='keywords']").attr("content");
if(keywordsString) {
this.keywords = keywordsString.split(',');
} else {
this.keywords = [];
}
}
return this;
};
MetaInspector.prototype.getAuthor = function() {
debug("Parsing page author from apropriate metatag");
if(!this.author) {
this.author = this.parsedDocument("meta[name='author']").attr("content");
}
return this;
};
MetaInspector.prototype.getCharset = function() {
debug("Parsing page charset from apropriate metatag");
if(!this.charset) {
this.charset = this.parsedDocument("meta[charset]").attr("charset");
}
return this;
};
MetaInspector.prototype.getImage = function() {
debug("Parsing page image based on the Open Graph image");
if(!this.image) {
var img = this.parsedDocument("meta[property='og:image']").attr("content");
if (img) {
this.image = this.getAbsolutePath(img);
}
}
return this;
};
MetaInspector.prototype.getImages = function() {
debug("Parsing page body images");
var _this = this;
if(this.images === undefined) {
this.images = this.parsedDocument('img').map(function(i ,elem) {
var src = _this.parsedDocument(this).attr('src');
return _this.getAbsolutePath(src);
});
}
return this;
};
MetaInspector.prototype.getFeeds = function() {
debug("Parsing page feeds based on rss or atom feeds");
if(!this.feeds) {
this.feeds = this.parseFeeds("rss") || this.parseFeeds("atom");
}
return this;
};
MetaInspector.prototype.parseFeeds = function(format) {
var _this = this;
var feeds = this.parsedDocument("link[type='application/" + format + "+xml']").map(function(i ,elem) {
return _this.parsedDocument(this).attr('href');
});
return feeds;
};
MetaInspector.prototype.initAllProperties = function() {
// title of the page, as string
this.getTitle()
.getAuthor()
.getCharset()
.getKeywords()
.getLinks()
.getDescription()
.getImage()
.getImages()
.getFeeds()
.getOgTitle()
.getOgDescription()
.getOgType()
.getOgUpdatedTime()
.getOgLocale();
};
MetaInspector.prototype.getAbsolutePath = function(href) {
if((/^(http:|https:)?\/\//i).test(href)) { return href; }
if(!(/^\//).test(href)) {
href = '/' + href;
}
return this.rootUrl + href;
};
MetaInspector.prototype.fetch = function() {
var _this = this;
var totalChunks = 0;
var r = request(_.assign({uri : this.url, gzip: true}, this.options), function(error, response, body) {
if(!error && response.statusCode === 200) {
_this.document = body;
_this.parsedDocument = cheerio.load(body);
_this.response = response;
_this.initAllProperties();
_this.emit("fetch");
}
else{
_this.emit("error", error);
}
});
if(_this.options.limit) {
_this.__stoppedAtLimit = false;
r.on('data', function(chunk) {
totalChunks += chunk.length;
if(totalChunks > _this.options.limit) {
if(!_this.__stoppedAtLimit) {
_this.emit("limit");
_this.__stoppedAtLimit = true;
}
r.abort();
}
});
}
};