-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 1.07 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
(function() {
'use strict';
var Q, TwitterTags, cheerio, request;
request = require('request');
cheerio = require('cheerio');
Q = require('q');
TwitterTags = {};
TwitterTags.parseHtml = function(html) {
var $, metaTags, result;
result = {};
$ = cheerio.load(html);
metaTags = $('meta').filter(function() {
if (!this.attribs.name) {
return false;
}
return this.attribs.name.match('twitter:');
});
metaTags.each(function(i, element) {
var attrs;
attrs = element.attribs;
return result[attrs.name.replace('twitter:', '').toLowerCase()] = attrs.content;
});
return result;
};
TwitterTags.fetch = function(url) {
var deferred;
deferred = Q.defer();
request(url, function(error, response, data) {
if (!error && response.statusCode === 200) {
return deferred.resolve(TwitterTags.parseHtml(data));
} else {
return deferred.reject({
error: error
});
}
});
return deferred.promise;
};
module.exports = TwitterTags;
}).call(this);