-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
140 lines (126 loc) · 4.32 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
var request = require('request');
var Promise = require('promise');
module.exports = new (function () {
"use strict";
var root = this;
var services = {
twitter: {
url: 'https://cdn.api.twitter.com/1/urls/count.json?url=',
format: function (body) {
var data = JSON.parse(body);
return data.count;
}
},
facebook: {
url: 'https://api.facebook.com/method/links.getStats?format=json&urls=',
format: function (body) {
var data = JSON.parse(body)[0];
return data.share_count || 0;
}
},
pinterest: {
url: 'https://api.pinterest.com/v1/urls/count.json?callback=_&url=',
format: function (body) {
var data = JSON.parse(body.match(/_\((.+)\)/)[1]);
return data.count;
}
},
linkedin: {
url: 'https://www.linkedin.com/countserv/count/share?url=',
format: function (body) {
var data = JSON.parse(body.match(/IN\.Tags\.Share\.handleCount\((.+)\)/)[1]);
return data.count;
}
},
stumbleupon: {
url: 'http://badge.stumbleupon.com/badge/embed/5/?url=',
format: function (body) {
// Yes friends, we can all agree parsing HTML with regex is a bad idea
// I'm glad we're on the same page about that.
var matches = body.match(/>([0-9]+)<\/a><\/li><\/ul>/);
var data = matches? parseInt(matches[1]) : 0;
return data;
}
},
buffer: {
url: 'https://api.bufferapp.com/1/links/shares.json?url=',
format: function (body) {
// Gonna be honest, I don't know what buffer is.
var data = JSON.parse(body);
return data.shares;
}
},
reddit: {
url: 'http://www.reddit.com/submit.json?url=',
format: function (body) {
var data, base;
data = JSON.parse(body);
if (!data.kind && !data[0].kind) {
return 0;
}
base = data.length? data[0] : data;
return base.data.children[0].data.score || base.data.children[0].data.ups - base.data.children[0].data.downs;
}
}
}
var getter = function (service) {
return function (url) {
if (typeof url !== "string") {
throw "URL must be a string";
return;
}
var promise = new Promise(function (resolve, reject) {
request(service.url + url, function (err, req, body) {
resolve(service.format(body));
});
});
return promise;
}
};
var after = function (trigger, func) {
var current = 0;
this.step = function (data) {
current += 1;
if (current == trigger) {
func(data);
}
}
};
root.get = function (url) {
var promise = new Promise(function (resolve, reject) {
var results = {};
var done = new after(Object.keys(services).length, function () {
resolve(results);
}).step;
Object.keys(services).forEach(function (service) {
root.get[service](url).then(function (count) {
results[service] = count;
done(results);
});
});
});
return promise;
};
for (var service in services) {
root.get[service] = getter(services[service]);
}
});
/*
{
google_plus: 'https://clients6.google.com/rpc?key=YOUR_API_KEY' // Google
[{
"method":"pos.plusones.get",
"id":"p",
"params":{
"nolog":true,
"id":"https://www.codedevelopr.com/",
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1"
}]
}
*/