Steam API code refactoring, added ownedGames API call

This commit is contained in:
Sahat Yalkabov
2014-02-05 19:29:46 -05:00
parent a2f4ffbf2b
commit 7fa3a401e6

View File

@ -32,31 +32,31 @@ exports.getApi = function(req, res) {
exports.getFoursquare = function(req, res, next) { exports.getFoursquare = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'foursquare' }); var token = _.findWhere(req.user.tokens, { kind: 'foursquare' });
async.parallel({ async.parallel({
trendingVenues: function(callback) { trendingVenues: function(callback) {
foursquare.Venues.getTrending('40.7222756', '-74.0022724', { limit: 50 }, token.accessToken, function(err, results) { foursquare.Venues.getTrending('40.7222756', '-74.0022724', { limit: 50 }, token.accessToken, function(err, results) {
callback(err, results); callback(err, results);
}); });
},
venueDetail: function(callback) {
foursquare.Venues.getVenue('49da74aef964a5208b5e1fe3', token.accessToken, function(err, results) {
callback(err, results);
});
},
userCheckins: function(callback) {
foursquare.Users.getCheckins('self', null, token.accessToken, function(err, results) {
callback(err, results);
});
}
}, },
venueDetail: function(callback) { function(err, results) {
foursquare.Venues.getVenue('49da74aef964a5208b5e1fe3', token.accessToken, function(err, results) { if (err) return next(err);
callback(err, results); res.render('api/foursquare', {
title: 'Foursquare API',
trendingVenues: results.trendingVenues,
venueDetail: results.venueDetail,
userCheckins: results.userCheckins
}); });
},
userCheckins: function(callback) {
foursquare.Users.getCheckins('self', null, token.accessToken, function(err, results) {
callback(err, results);
});
}
},
function(err, results) {
if (err) return next(err);
res.render('api/foursquare', {
title: 'Foursquare API',
trendingVenues: results.trendingVenues,
venueDetail: results.venueDetail,
userCheckins: results.userCheckins
}); });
});
}; };
/** /**
@ -90,25 +90,25 @@ exports.getFacebook = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'facebook' }); var token = _.findWhere(req.user.tokens, { kind: 'facebook' });
graph.setAccessToken(token.accessToken); graph.setAccessToken(token.accessToken);
async.parallel({ async.parallel({
getMe: function(done) { getMe: function(done) {
graph.get(req.user.facebook, function(err, me) { graph.get(req.user.facebook, function(err, me) {
done(err, me); done(err, me);
}); });
},
getMyFriends: function(done) {
graph.get(req.user.facebook + '/friends', function(err, friends) {
done(err, friends.data);
});
}
}, },
getMyFriends: function(done) { function(err, results) {
graph.get(req.user.facebook + '/friends', function(err, friends) { if (err) return next(err);
done(err, friends.data); res.render('api/facebook', {
title: 'Facebook API',
me: results.getMe,
friends: results.getMyFriends
}); });
}
},
function(err, results) {
if (err) return next(err);
res.render('api/facebook', {
title: 'Facebook API',
me: results.getMe,
friends: results.getMyFriends
}); });
});
}; };
/** /**
@ -185,53 +185,53 @@ exports.getNewYorkTimes = function(req, res, next) {
exports.getLastfm = function(req, res, next) { exports.getLastfm = function(req, res, next) {
var lastfm = new LastFmNode(secrets.lastfm); var lastfm = new LastFmNode(secrets.lastfm);
async.parallel({ async.parallel({
artistInfo: function(done) { artistInfo: function(done) {
lastfm.request("artist.getInfo", { lastfm.request("artist.getInfo", {
artist: 'Epica', artist: 'Epica',
handlers: { handlers: {
success: function(data) { success: function(data) {
done(null, data); done(null, data);
}, },
error: function(err) { error: function(err) {
done(err); done(err);
}
} }
} });
}); },
artistTopAlbums: function(done) {
lastfm.request("artist.getTopAlbums", {
artist: 'Epica',
handlers: {
success: function(data) {
var albums = [];
_.each(data.topalbums.album, function(album) {
albums.push(album.image.slice(-1)[0]['#text']);
});
done(null, albums.slice(0, 4));
},
error: function(err) {
done(err);
}
}
});
}
}, },
artistTopAlbums: function(done) { function(err, results) {
lastfm.request("artist.getTopAlbums", { if (err) return next(err.message);
artist: 'Epica', var artist = {
handlers: { name: results.artistInfo.artist.name,
success: function(data) { image: results.artistInfo.artist.image.slice(-1)[0]['#text'],
var albums = []; tags: results.artistInfo.artist.tags.tag,
_.each(data.topalbums.album, function(album) { bio: results.artistInfo.artist.bio.summary,
albums.push(album.image.slice(-1)[0]['#text']); stats: results.artistInfo.artist.stats,
}); similar: results.artistInfo.artist.similar.artist,
done(null, albums.slice(0,4)); topAlbums: results.artistTopAlbums
}, };
error: function(err) { res.render('api/lastfm', {
done(err); title: 'Last.fm API',
} artist: artist
}
}); });
}
},
function(err, results) {
if (err) return next(err.message);
var artist = {
name: results.artistInfo.artist.name,
image: results.artistInfo.artist.image.slice(-1)[0]['#text'],
tags: results.artistInfo.artist.tags.tag,
bio: results.artistInfo.artist.bio.summary,
stats: results.artistInfo.artist.stats,
similar: results.artistInfo.artist.similar.artist,
topAlbums: results.artistTopAlbums
};
res.render('api/lastfm', {
title: 'Last.fm API',
artist: artist
}); });
});
}; };
/** /**
@ -258,7 +258,7 @@ exports.getTwitter = function(req, res, next) {
/** /**
* GET /api/paypal * GET /api/paypal
* PayPal SDK example * PayPal SDK example.
*/ */
exports.getPayPal = function(req, res, next) { exports.getPayPal = function(req, res, next) {
@ -272,16 +272,18 @@ exports.getPayPal = function(req, res, next) {
'return_url': secrets.paypal.returnUrl, 'return_url': secrets.paypal.returnUrl,
'cancel_url': secrets.paypal.cancelUrl 'cancel_url': secrets.paypal.cancelUrl
}, },
'transactions': [{ 'transactions': [
'description': 'Node.js Boilerplate', {
'amount': { 'description': 'Node.js Boilerplate',
'currency': 'USD', 'amount': {
'total': '2.99' 'currency': 'USD',
'total': '2.99'
}
} }
}] ]
}; };
paypal.payment.create(payment_details, function (error, payment) { paypal.payment.create(payment_details, function(error, payment) {
if(error){ if (error) {
console.log(error); console.log(error);
} else { } else {
req.session.payment_id = payment.id; req.session.payment_id = payment.id;
@ -299,14 +301,14 @@ exports.getPayPal = function(req, res, next) {
/** /**
* GET /api/paypal/success * GET /api/paypal/success
* PayPal SDK example * PayPal SDK example.
*/ */
exports.getPayPalSuccess = function(req, res, next) { exports.getPayPalSuccess = function(req, res, next) {
var payment_id = req.session.payment_id; var payment_id = req.session.payment_id;
var payment_details = { 'payer_id': req.query.PayerID }; var payment_details = { 'payer_id': req.query.PayerID };
paypal.payment.execute(payment_id, payment_details, function(error, payment){ paypal.payment.execute(payment_id, payment_details, function(error, payment) {
if(error){ if (error) {
res.render('api/paypal', { res.render('api/paypal', {
result: true, result: true,
success: false success: false
@ -322,7 +324,7 @@ exports.getPayPalSuccess = function(req, res, next) {
/** /**
* GET /api/paypal/cancel * GET /api/paypal/cancel
* PayPal SDK example * PayPal SDK example.
*/ */
exports.getPayPalCancel = function(req, res, next) { exports.getPayPalCancel = function(req, res, next) {
@ -339,51 +341,40 @@ exports.getPayPalCancel = function(req, res, next) {
*/ */
exports.getSteam = function(req, res) { exports.getSteam = function(req, res) {
var defaultSteamId = '76561197992403307'; var steamId = '76561197982488301';
var steamId = _.findWhere(req.user.tokens, { kind: 'steam' }).steamId || defaultSteamId;
var query = { l: 'english', steamid: steamId, key: secrets.steam.apiKey }; var query = { l: 'english', steamid: steamId, key: secrets.steam.apiKey };
async.parallel([ async.parallel({
function (callback) { playerAchievements: function(done) {
builtQuery = querystring.stringify(query); query.appid = '49520';
request.get({url:'http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?' + builtQuery, json:true}, function(err, request, body) { var qs = querystring.stringify(query);
if (err) return next(err); request.get({ url: 'http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?' + qs, json: true }, function(error, request, body) {
callback(null, body); done(error, body.playerstats);
}); });
}, },
function (callback) { playerSummaries: function(done) {
query.appid = '570'; // Dota 2 query.steamids = steamId;
builtQuery = querystring.stringify(query); var qs = querystring.stringify(query);
request.get({url:'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?' + builtQuery, json:true}, function(err, request, body) { request.get({ url: 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' + qs, json: true }, function(error, request, body) {
if (err) return next(err); done(error, body);
delete query.appid;
callback(null, body);
}); });
}, },
function (callback) { ownedGames: function(done) {
query.appid = '219640'; //Chivalry: Medieval Warfare var qs = querystring.stringify(query);
builtQuery = querystring.stringify(query); request.get({ url: 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?' + qs, json: true }, function(error, request, body) {
request.get({url:'http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?' + builtQuery, json:true}, function(err, request, body) { done(error, body);
if (err) return next(err);
delete query.appid;
callback(null, body);
}); });
}, }
function (callback) { },
delete query.steamid; function(err, results) {
query.steamids = steamId; //this request can be supplied a set of comma seperated steam ids
builtQuery = querystring.stringify(query);
request.get({url:'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' + builtQuery, json:true}, function(err, request, body) {
if (err) return next(err);
callback(null, body);
});
},
],
function (err, results) {
console.log(require('util').inspect(results, false, null));
if (err) return next(err); if (err) return next(err);
else res.render('api/steam', { console.log(results.ownedGames);
console.log(results.playerSummaries);
res.render('api/steam', {
title: 'Steam Web API', title: 'Steam Web API',
items: results }); ownedGames: results.ownedGames,
playerAchievemments: results.playerAchievements,
playerSummaries: results.playerSummaries
});
}); });
}; };