get a youtube link for last fm ~listening. note: clean up yt api later

This commit is contained in:
reality 2013-10-24 09:47:56 +00:00
parent 1c1ba2ef02
commit 614520443f
2 changed files with 32 additions and 12 deletions

View File

@ -29,19 +29,31 @@ var lastfm = function(dbot) {
event.reply('Unknown Last.FM user.');
} else if(_.has(body, 'recenttracks') && !_.isUndefined(body.recenttracks.track[0])) {
var track = body.recenttracks.track[0];
term = track.name + ' ' + track.artist['#text'],
output = '';
if(_.has(track, '@attr') && _.has(track['@attr'], 'nowplaying') && track['@attr'].nowplaying == 'true') {
event.reply(dbot.t('now_listening', {
output = dbot.t('now_listening', {
'user': event.user,
'track': track.name,
'artist': track.artist['#text']
}));
});
} else {
event.reply(dbot.t('last_listened', {
output = dbot.t('last_listened', {
'user': event.user,
'track': track.name,
'artist': track.artist['#text']
}));
});
}
dbot.api.youtube.search(term, function(body) {
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) {
var v = body.feed.entry[0];
link = v.link[0].href.match(dbot.modules.youtube.LinkRegex);
if(link) {
output += ' - http://youtu.be/' + link[2];
}
}
event.reply(output);
});
} else {
event.reply(dbot.t('no_listen', { 'user': event.user }));
}

View File

@ -13,6 +13,21 @@ var youtube = function(dbot) {
};
this.LinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
this.api = {
'search': function(term, callback) {
var qs = _.clone(this.params);
request.get(this.ApiRoot + '/videos', {
'qs': _.extend(qs, {
'q': term,
'max-results': 1
}),
'json': true
}, function(error, response, body) {
callback(body);
}.bind(this));
}
};
this.internalAPI = {
'formatLink': function(v) {
var seconds = v['media$group']['yt$duration'].seconds,
@ -50,14 +65,7 @@ var youtube = function(dbot) {
this.commands = {
'~youtube': function(event) {
var qs = _.clone(this.params);
request.get(this.ApiRoot + '/videos', {
'qs': _.extend(qs, {
'q': event.input[1],
'max-results': 1
}),
'json': true
}, function(error, response, body) {
this.api.search(event.input[1], function(body) {
if(_.isObject(body) && _.has(body, 'feed') && _.has(body.feed, 'entry')) {
event.reply(this.internalAPI.formatLink(body.feed.entry[0]));
} else {