-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
46 lines (41 loc) · 1.1 KB
/
parser.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
var _ = require('lodash'),
urls = require('url'),
sharing = require('./sharing'),
Q = require('q'),
HtmlParser = require('./parsers/generic-html');
exports.parsers = {
'bitcoinwarrior.net': new HtmlParser({
linkSelector: '.entry-title a',
contentSelector: 'article.post'
})
};
exports.parse = function (url, opts) {
var parsed;
opts = _.defaults(opts, {
append: false,
social: false,
previous: []
});
if (url && (parsed = urls.parse(url)) && exports.parsers[parsed.hostname]) {
return exports.parsers[parsed.hostname].parse(url)
.then(function (result) {
if (opts.append && opts.previous.length > 0) {
return _.uniq(result.concat(opts.previous),'link')
}
return result;
})
.then(function (result) {
if (opts.social) {
return Q.all(result.map(function (r) {
return sharing.queryStats(r.link).then(function (stats) {
r.sharings = stats;
return r;
})
}));
} else {
return result
}
})
}
throw new Error('bad url')
};