Async parallel to retrieve artist's top albums as well

This commit is contained in:
Sahat Yalkabov
2013-12-10 00:32:23 -05:00
parent 96a1892234
commit b72599151a
2 changed files with 63 additions and 26 deletions

View File

@ -190,27 +190,56 @@ exports.getLastfm = function(req, res) {
secret: '4ae76d10d76cf680cebf4f0c8dea1aa4' secret: '4ae76d10d76cf680cebf4f0c8dea1aa4'
}); });
lastfm.request("artist.getInfo", { async.parallel({
artist: 'Epica', artistInfo: function(done) {
handlers: { lastfm.request("artist.getInfo", {
success: function(data) { artist: 'Epica',
var artist = { handlers: {
name: data.artist.name, success: function(data) {
image: data.artist.image.slice(-1)[0]['#text'], done(null, data);
tags: data.artist.tags.tag, },
bio: data.artist.bio.summary, error: function(error) {
stats: data.artist.stats, done(error);
similar: data.artist.similar.artist }
}; }
res.render('api/lastfm', { });
title: 'Last.fm API', },
artist: artist artistTopAlbums: function(done) {
}); lastfm.request("artist.getTopAlbums", {
}, artist: 'Epica',
error: function(error) { handlers: {
res.send(error.message); success: function(data) {
} var albums = [];
_.each(data.topalbums.album, function(album) {
albums.push(album.image.slice(-1)[0]['#text']);
});
done(null, albums);
},
error: function(error) {
done(error);
}
}
});
} }
},
function(err, results) {
if (err) return res.send(err);
console.log('===')
console.log(err);
console.log(results);
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
});
}); });
}; };

View File

@ -23,23 +23,31 @@ block content
li li
small.text-primary small.text-primary
i.fa.fa-users i.fa.fa-users
strong Listeners: | Listeners: #{artist.stats.listeners}
| #{artist.stats.listeners}
li li
small.text-primary small.text-primary
i.fa.fa-headphones i.fa.fa-headphones
strong Scrobbles: | Scrobbles: #{artist.stats.playcount}
| #{artist.stats.playcount}
p p
img(src='#{artist.image}') img(src='#{artist.image}')
p p
for tag in artist.tags for tag in artist.tags
span.label.label-primary span.label.label-primary
i.fa.fa-tag i.fa.fa-tag
| #{tag.name} | #{tag.name}
|  | 
p!= artist.bio
strong Similar Artists .panel.panel-default
.panel-heading Biography
.panel-body
!= artist.bio
h4 Top Albums
for album in artist.topAlbums
album
h4 Similar Artists
ul.list ul.list
for similarArtist in artist.similar for similarArtist in artist.similar
li li