Merge remote-tracking branch 'gcochard/add-twitter-follow'

This commit is contained in:
Thaddee Tyl 2015-12-04 00:41:27 +01:00
commit 808e285755
3 changed files with 86 additions and 0 deletions

View File

@ -4255,6 +4255,45 @@ 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,
'https://twitter.com/' + user + '/followers'
];
badgeData.text[1] = '';
request(options, function(err, res, buffer) {
if (err != null) {
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] = '';
} else {
badgeData.text[1] = metric(data.followers_count);
}
} catch(e) {
console.error(e);
}
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)$/,
@ -4827,3 +4866,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);
}
});
}
}

View File

@ -14,9 +14,11 @@
</linearGradient>
<g stroke="#d5d5d5">
<rect stroke="none" fill="#fcfcfc" x="0.5" y="0.5" width="{{=it.widths[0]}}" height="19" rx="2"/>
{{?(it.text[1] && it.text[1].length)}}
<rect y="0.5" x="{{=it.widths[0]+6.5}}" width="{{=it.widths[1]}}" height="19" rx="2" fill="#fafafa"/>
<rect x="{{=it.widths[0]+6}}" y="7.5" width="0.5" height="5" stroke="#fafafa"/>
<path d="M{{=it.widths[0]+6.5}} 6.5 l-3 3v1 l3 3" stroke="d5d5d5" fill="#fafafa"/>
{{?}}
</g>
{{?it.logo}}
<image x="5" y="3" width="{{=it.logoWidth}}" height="14" xlink:href="{{=it.logo}}"/>
@ -24,10 +26,12 @@
<g fill="#333" text-anchor="middle" font-family="Helvetica Neue,Helvetica,Arial,sans-serif" font-weight="700" font-size="11px" line-height="14px">
<text x="{{=(it.widths[0]+it.logoWidth+it.logoPadding)/2}}" y="15" fill="#fff">{{=it.escapeXml(it.capitalize(it.text[0]))}}</text>
<text x="{{=(it.widths[0]+it.logoWidth+it.logoPadding)/2}}" y="14">{{=it.escapeXml(it.capitalize(it.text[0]))}}</text>
{{?(it.text[1] && it.text[1].length)}}
<text x="{{=it.widths[0]+it.widths[1]/2+6}}" y="15" fill="#fff">{{=it.escapeXml(it.text[1])}}</text>
<a xlink:href="{{=it.links[1]}}">
<text id="rlink" x="{{=it.widths[0]+it.widths[1]/2+6}}" y="14">{{=it.escapeXml(it.text[1])}}</text>
</a>
{{?}}
</g>
<a xlink:href="{{=it.links[0]}}">
<rect id="llink" stroke="#d5d5d5" fill="url(#a)" x="0.5" y="0.5" width="{{=it.widths[0]}}" height="19" rx="2"/>

View File

@ -502,6 +502,10 @@ Pixel-perfect &nbsp; Retina-ready &nbsp; Fast &nbsp; Consistent &nbsp; Hackable
<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>
</tbody></table>
<h3 id="miscellaneous"> Miscellaneous </h3>