3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-25 12:12:35 +01:00

lastfm ~listening: make youtube and spotify api calls in parallel

This commit is contained in:
Scritches 2018-03-24 19:24:57 -04:00
parent 72a4552742
commit 25d572e8cb

View File

@ -280,22 +280,41 @@ var lastfm = function(dbot) {
'artist': track.artist['#text']
});
}
dbot.api.youtube.search(term, function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
var link = body.items[0].id.videoId
if(link) {
output += ' - http://youtu.be/' + link;
}
async.parallel({
youtube: function(cb) {
dbot.api.youtube.search(term, function(body) {
if(_.isObject(body) && _.has(body, 'items') && body.items.length > 0) {
var link = body.items[0].id.videoId
if(link) {
cb(null,"http://youtu.be/" + link);
} else {
cb(null, undefined);
}
}
});
},
spotify: function(cb) {
dbot.api.spotify.spotifySearch(term, function(body, url, uri) {
if(body) {
cb(null, { url:url, uri:uri });
} else {
cb(null, undefined);
}
});
}
dbot.api.spotify.spotifySearch(term, function(body, url, uri) {
if(body) {
output += ' - ' + url + ' - ' + uri;
}
event.reply(output);
});
}, function(err, results) {
if (results.youtube || results.spotify) output += " - "
if (results.youtube) output += results.youtube;
if (results.spotify) {
if (results.youtube) output += " | ";
output += results.spotify.url + " - " + results.spotify.uri;
}
event.reply(output);
});
} else {
if(err == 'no_user') {
event.reply('Unknown LastFM user.');