-
Notifications
You must be signed in to change notification settings - Fork 21
/
social-buttons-server.js
158 lines (129 loc) · 4.15 KB
/
social-buttons-server.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
var async = require('async');
var cors = require('cors');
var express = require('express');
var request = require('request');
// How many minutes should we cache the results for a given request
var CACHE_TIME = process.env.CACHE_TIME || 4 * 60;
var PORT = process.env.PORT || 5000;
var app = express();
app.use(express.logger());
var whitelist = [
'http://localhost:4000',
'https://localhost:4000',
'http://social-buttons-server.herokuapp.com',
'http://dev.stopwatching.us',
'https://dev.stopwatching.us',
'http://rally.stopwatching.us',
'https://rally.stopwatching.us',
'http://2.stopwatching.us',
'https://2.stopwatching.us',
'http://thedaywefightback.org',
'https://thedaywefightback.org',
'https://d28jjwuneuxo3n.cloudfront.net'
];
var corsOptions = {
origin: function (origin, cb) {
cb(null, whitelist.indexOf(origin) !== -1);
}
};
// Block all hosts not in the whitelist
//app.use(function (req, res, next) {
// if (whitelist.indexOf(req.headers.origin) === -1) {
// return res.send({ blocked: true });
// }
//
// next();
//});
// Setup caching headers (works well with cloudfront)
app.use(function (req, res, next) {
res.set('Cache-Control', 'max-age=' + CACHE_TIME);
next();
});
app.use(cors(corsOptions));
app.options('*', cors(corsOptions));
var networkCallbacks = {
twitter: function (url, callback) {
// Twitter is nice and easy
var apiUrl = 'http://urls.api.twitter.com/1/urls/count.json?url=' + url;
request.get({ url: apiUrl, json: true }, function (err, res, body) {
if (err) {
return callback(null, 0);
}
callback(null, body.count);
});
},
facebook: function (url, callback) {
// This query string gets the total number of likes, shares and comments to
// create the final count
var apiUrl = 'https://graph.facebook.com/fql?q=SELECT%20url,' +
'%20normalized_url,%20share_count,%20like_count,%20comment_count,' +
'%20total_count,commentsbox_count,%20comments_fbid,' +
'%20click_count%20FROM%20link_stat%20WHERE%20url="' + url + '"';
request.get({ url: apiUrl, json: true }, function (err, res, body) {
if (err) {
return callback(null, 0);
}
var count = 0;
if (body.data.length > 0) {
count = body.data[0].total_count;
}
callback(null, count);
});
},
googleplus: function (url, callback) {
// This is a hacky method found on the internet because google doesn't have
// an API for google plus counts
var apiUrl = 'https://plusone.google.com/_/+1/fastbutton?url=' + url;
request.get(apiUrl, function (err, res, body) {
if (err) {
return callback(null, 0);
}
var result = /__SSR \= \{c\: (.*?)\.0/g.exec(body);
var count = 0;
if (result) {
count = result[1] * 1;
}
callback(null, count);
});
}
};
app.get('/', function (req, res) {
var url;
// Check to see if any networks were specified in the query
if (!req.param('networks')) {
return res.send({
error: 'You have to specify which networks you want stats for ' +
'(networks=facebook,twitter,googleplus)'
});
}
// Check to see if a url was specified in the query else attempt to use the
// referer url
if (req.param('url')) {
url = req.param('url');
} else {
url = req.header('Referer');
if (!url) {
return res.send({
error: 'You asked for the referring urls stats but there is no ' +
'referring url, specify one manually (&url=http://1984day.com)'
});
}
}
// Create an object of callbacks for each of the requested networks It is
// then passed to the async library to executed in parallel All results will
// be sent to the browser on completion.
var networksToRequest = {};
req.param('networks').split(',').forEach(function (network) {
if (typeof networkCallbacks[network] !== 'undefined') {
networksToRequest[network] = function (callback) {
networkCallbacks[network](url, callback);
};
}
});
async.parallel(networksToRequest, function (err, results) {
res.jsonp(results);
});
});
app.listen(PORT, function () {
console.log('Listening on ' + PORT);
});