Skip to content

Commit

Permalink
More avatar types
Browse files Browse the repository at this point in the history
  • Loading branch information
Loren West committed Oct 22, 2013
1 parent cb0222a commit b1b2b02
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 230 deletions.
4 changes: 2 additions & 2 deletions app.js
Expand Up @@ -2,9 +2,9 @@ var express = require('express');
var http = require('http');
var path = require('path');
var config = require('config');
var Monitor = require('monitor-min');
var BBFProbe = require('./lib/BBFProbe');
var ConfigMonitor = require('config-monitor');
var Monitor = require('monitor-min').start();
var app = express();


Expand All @@ -18,7 +18,7 @@ var appServer = http.createServer(app).listen(config.BBF.serverPort, function(){
' / _ / / _ `/ / _ / // / __/ __/ _ \\/ _ \\ / _// _ `/ __/ __/ _ \\/ __/ // / \n' +
'/____/_/\\_, / /____/\\_,_/\\__/\\__/\\___/_//_/ /_/ \\_,_/\\__/\\__/\\___/_/ \\_, / \n' +
' /___/ /___/');
console.log('Express server listening on port ' + config.BBF.serverPort);
console.log('Serving buttons on port ' + config.BBF.serverPort);

// Add the monitor-min service to the existing app server
var monitorService = new Monitor.Server({server:appServer});
Expand Down
79 changes: 78 additions & 1 deletion lib/BBFProbe.js
Expand Up @@ -7,6 +7,8 @@
Probe = Monitor.Probe,
_ = Monitor._,
log = Monitor.getLogger('BBFProbe'),
crypto = require('crypto'),
exec = require('child_process').exec,
CONFIG = require('config');

// Constants
Expand Down Expand Up @@ -67,6 +69,9 @@
for (var trayNum = 0; trayNum < NUM_TRAYS; trayNum++) {
t.newTray(trayNum);
}

// Expose this probe to global
global.BBFProbe = t;
},

/**
Expand Down Expand Up @@ -108,8 +113,9 @@
* @param options {object} Options
* @param options.buttonId {String} ID of the button pushed
* @param options.photoUrl {String} The URL of the person that pushed the button
* @param [callback] {function(err)} Callback
*/
buttonPushed_control: function(options) {
buttonPushed_control: function(options, callback) {
var t = this,
trayId = options.buttonId.substr(1,1);

Expand All @@ -131,8 +137,79 @@
t.newTray(trayId);
}

callback();
}, 100);
},

/**
* This attempts to convert a name into a photo URL for that person
*
* The 'github' type is the github name. 'gravatar' type is the gravatar
* email address, and 'twitter' is their twitter name without the '@'.
*
* @param options {object} Options
* @param options.type {String} One of "github", "gravatar", or "twitter"
* @param options.name {String} The URL of the person that pushed the button
* @param callback {Function(err, url)} The callback function
* @param callback.err {Object} The error object if an error occurred
* @param callback.url {String} The photo URL for that person
*/
findPhotoUrl_control: function(options, callback) {
var t = this,
type = options.type,
name = options.name,
url = null;

// Gravatar: Return the MD5 hash of their lowercased, trimmed email
if (type === 'gravatar') {
var md5 = crypto.createHash('md5');
md5.update(name.toLowerCase().trim());
url = 'http://www.gravatar.com/avatar/' + md5.digest('hex') + '.jpg';
return callback(null, url);
}

// Twitter name
else if (type === 'twitter') {
url = 'https://twitter.com/' + name;
t.getAvatarImgLine(url, 'avatar size73', function(err, avatarUrl) {
if (err) {
return callback(err);
}

// Parse the http portion
var part = avatarUrl.substr(avatarUrl.indexOf('http'));
part = part.substr(0, part.indexOf('"'));
return callback(err, part);
});
}

// Github name
else if (type === 'github') {
url = 'https://github.com/' + name;
t.getAvatarImgLine(url, '<img class=.avatar. ', function(err, avatarUrl) {
if (err) {
return callback(err);
}

// Parse the http portion
var part = avatarUrl.substr(avatarUrl.indexOf('http'));
part = part.substr(0, part.indexOf('"'));
return callback(err, part);
});
}

else {
return callback('Unknown name type: ' + type);
}

},

// Get the entire HTML line that contains the avatar image
getAvatarImgLine: function(url, grepFor, callback) {
var cmd = 'curl -Ls "' + url + '" | grep "' + grepFor + '"';
var child = exec(cmd, function(error, stdout, stderr){
callback(error, stdout);
});
}

});
Expand Down

0 comments on commit b1b2b02

Please sign in to comment.