Skip to content

Commit

Permalink
Add twitter follow badge. Fixes badges#527
Browse files Browse the repository at this point in the history
This adds a twitter follow badge. It also updates the social template
to include a `nobubble` option which will ignore the data in text[1]
and not draw the bubble to the right of the badge. Simply add
&nobubble=true to the URL of your badge.
  • Loading branch information
gcochard authored and Yannick Galatol committed Dec 23, 2015
1 parent d272b64 commit 463f453
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
87 changes: 87 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4323,6 +4323,54 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Twitter follow badge.
camp.route(/^\/twitter\/follow\/@?([^\/]+)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var user = match[1]; // eg, shields_io
var format = match[2];
var options = {
url: 'http://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=' + user
};
var badgeData = getBadgeData('Follow', data);
badgeData.text[0] = 'Follow ' + user;
badgeData.colorscheme = '55ACEE';
badgeData.logo = badgeData.logo || logos.twitter;
badgeData.links = [
'https://twitter.com/intent/follow?screen_name=' + user
];
if (data.nobubble) {
badgeData.nobubble = data.nobubble;
sendBadge(format, badgeData);
return;
}
if (badgeData.template === 'social') {
badgeData.links[1] = 'https://twitter.com/' + user + '/followers';
badgeData.text[1] = '';
request(options, function(err, res, buffer) {
if (err != null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
var data = JSON.parse(buffer);
// the data is formatted as an array
data = data[0];
// data.followers_count could be zero...don't just check if falsey
if(!data.hasOwnProperty('followers_count')){
badgeData.text[1] = 'invalid';
} else {
badgeData.text[1] = metric(data.followers_count);
}
} catch(e) {
console.error(e);
badgeData.text[1] = 'invalid';
}
sendBadge(format, badgeData);
});
}
}));

// Snap CI build integration.
// https://snap-ci.com/snap-ci/snap-deploy/branch/master/build_image
camp.route(/^\/snap(-ci?)\/([^\/]+\/[^\/]+)(?:\/(.+))\.(svg|png|gif|jpg|json)$/,
Expand Down Expand Up @@ -4898,3 +4946,42 @@ function phpStableVersion(version) {
// normal or patch
return (versionData.modifier === 3) || (versionData.modifier === 4);
}

// This searches the serverSecrets for a twitter consumer key
// and secret, and exchanges them for a bearer token to use for all requests.
function fetchTwitterToken() {
if(serverSecrets.twitter_consumer_key && serverSecrets.twitter_consumer_secret){
// fetch a bearer token good for this app session
// construct this bearer request with a base64 encoding of key:secret
// docs for this are here: https://dev.twitter.com/oauth/application-only
var twitter_bearer_credentials = escape(serverSecrets.twitter_consumer_key) + ':' + escape(serverSecrets.twitter_consumer_secret);
var options = {
url: 'https://api.twitter.com/oauth2/token',
headers: {
// is this the best way to base 64 encode a string?
Authorization: 'Basic '+(new Buffer(twitter_bearer_credentials)).toString('base64'),
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
form: 'grant_type=client_credentials',
method: 'POST'
};
console.log('Fetching twitter bearer token...');
request(options,function(err,res,buffer){
if(err){
console.error('Error fetching twitter bearer token, error: ', err);
return;
}
try{
var data = JSON.parse(buffer);
if(data.token_type === 'bearer'){
serverSecrets.twitter_bearer_token = data.access_token;
console.log('Fetched twitter bearer token');
return;
}
console.error('Error fetching twitter bearer token, data: %j', data);
} catch(e) {
console.error('Error fetching twitter bearer token, buffer: %s, ', buffer, e);
}
});
}
}
4 changes: 4 additions & 0 deletions templates/social-template.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,14 @@ <h3 id="social"> Social </h3>
<td><img src='/twitter/url/http/shields.io.svg?style=social' alt=''/></td>
<td><code>https://img.shields.io/twitter/url/http/shields.io.svg?style=social</code></td>
</tr>
<tr><th> Twitter Follow: </th>
<td><img src='/twitter/follow/shields_io.svg?style=social' alt=''/></td>
<td><code>https://img.shields.io/twitter/follow/shields_io.svg?style=social</code></td>
</tr>
<tr><th> Twitter Follow (with no count): </th>
<td><img src='/twitter/follow/shields_io.svg?style=social&nobubble=true' alt=''/></td>
<td><code>https://img.shields.io/twitter/follow/shields_io.svg?style=social&amp;nobubble=true</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Miscellaneous </h3>
Expand Down

0 comments on commit 463f453

Please sign in to comment.