From 93ee3cb08b026c9aabf4d236884a9de23a9d763b Mon Sep 17 00:00:00 2001 From: Scritches Date: Tue, 10 Apr 2018 15:35:57 -0400 Subject: [PATCH] lastfm and spotify fixes ### lastfm module removed ~taste and taste compare functions from lastfm - their API no longer supports these calls Improved error reporting for ~suggestion and also parallelized the youtube/spotify queries ### spotify module removed link matching - the Link module provides the exact same information as directly querying spotify so there's no real point --- modules/lastfm/lastfm.js | 71 +++++++++++++++++++++++++++----------- modules/spotify/spotify.js | 46 ------------------------ 2 files changed, 51 insertions(+), 66 deletions(-) diff --git a/modules/lastfm/lastfm.js b/modules/lastfm/lastfm.js index 4571714..91e6a5b 100644 --- a/modules/lastfm/lastfm.js +++ b/modules/lastfm/lastfm.js @@ -92,6 +92,7 @@ var lastfm = function(dbot) { }); }, + /* 'tasteCompare': function(user, oUser, callback) { request.get(this.ApiRoot, { 'qs': { @@ -105,6 +106,7 @@ var lastfm = function(dbot) { }, 'json': true }, function(err, res, body) { + console.log(body); if(_.has(body, 'error') && body.error == 6 || body.error == 7) { callback('no_user', user, null); } else if(_.has(body, 'comparison') && _.has(body.comparison, 'result')) { @@ -114,6 +116,7 @@ var lastfm = function(dbot) { } }); }, + */ 'getInfo': function(lfm, callback) { request.get(this.ApiRoot, { @@ -220,29 +223,54 @@ var lastfm = function(dbot) { 'artist': track.artist.name }); var term = track.name + ' ' + track.artist.name; - - 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,"https://youtu.be/" + link); + } else { + cb(null, undefined); + } + } + }); + }, + spotify: function(cb) { + dbot.api.spotify.spotifySearch(term, function(body, url, uri) { + if(body) { + if (!dbot.modules.minify) { + cb(null, { url: url, uri:uri }); + } else { + dbot.modules.minify.api.minify(url, "bitly", function(mini) { + cb(null, { url:mini || url, uri:uri }); + }); + } + } else { + cb(null, undefined); + } + }); } - - dbot.api.spotify.spotifySearch(term, function(body, t) { - if(body) { - output += ' - ' + t; - } - - 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 { - event.reply('something broke'); + event.reply('Couldn\'t get any suggested tracks.'); + console.log(err); } }); } else { - event.reply('something broke'); + event.reply('Couldn\'t find any similar artists to what you\'re listening to.'); + console.log(err); } }.bind(this)); } else { @@ -262,7 +290,7 @@ var lastfm = function(dbot) { user = event.res[0].user; lfm = event.res[0].lfm; } - + this.api.getListening(lfm, function(err, track) { if(!err) { var term = track.name + ' ' + track.artist['#text'], @@ -327,10 +355,12 @@ var lastfm = function(dbot) { } else if(err == 'no_listen') { event.reply(dbot.t('no_listen', { 'user': user.currentNick })); } + console.log(err); } }); }, + /* '~taste': function(event) { var u1 = event.rUser, lfm1 = event.rProfile.lastfm, @@ -431,6 +461,7 @@ var lastfm = function(dbot) { }.bind(this)); } }, + */ '~artists': function(event) { var u1 = event.rUser, @@ -456,7 +487,7 @@ var lastfm = function(dbot) { }); } }; - this.commands['~taste'].regex = [/^taste ([\d\w[\]{}^|\\`_-]+?)/, 2]; + //this.commands['~taste'].regex = [/^taste ([\d\w[\]{}^|\\`_-]+?)/, 2]; this.commands['~artists'].regex = [/^artists ([\d\w[\]{}^|\\`_-]+?)/, 2]; _.each(this.commands, function(command) { diff --git a/modules/spotify/spotify.js b/modules/spotify/spotify.js index 3829326..10db540 100644 --- a/modules/spotify/spotify.js +++ b/modules/spotify/spotify.js @@ -6,16 +6,6 @@ var request = require('request'), _ = require('underscore')._; var spotify = function(dbot) { - /* Examples: - * http://open.spotify.com/track/42SYMWISn7xUpTNPLw9V5E - * spotify:track:42SYMWISn7xUpTNPLw9V5E - * http://open.spotify.com/artist/3yY2gUcIsjMr8hjo51PoJ8 - * spotify:artist:3yY2gUcIsjMr8hjo51PoJ8 - * http://open.spotify.com/album/30g571JKoxs8AnsgAViV2J - * spotify:album:30g571JKoxs8AnsgAViV2J - */ - this.spotifyRegex = /(\b(https?:\/\/open.spotify.com\/(artist|track|album)\/\w*|spotify:(artist|track|album):\w*)\b)/ig; - this.spotifyLookup = 'http://ws.spotify.com/lookup/1/.json'; this.spotifySearch = 'https://api.spotify.com/v1/search'; this.youtubeRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; this.spotifyText = '\u00039spotify\u000f'; @@ -40,37 +30,6 @@ var spotify = function(dbot) { }); }; - this.lookup = function(link, callback) { - this.authenticate(function(token) { - request({ - 'url': this.spotifyLookup, - 'qs': { 'uri': link }, - 'json': true, - 'headers': { 'Authorization': "Bearer " + token } - }, function(error, response, body) { - if(!error && response.statusCode == 200) { - if(_.has(body, 'track')) { - callback(dbot.t('track', { - 'artist': _.map(body.track.artists, - function(a) { return a.name }).join(', '), - 'album': body.track.album.name, - 'track': body.track.name - })); - } else if(_.has(body, 'album')) { - callback(dbot.t('album', { - 'artist': body.album.artist, - 'album': body.album.name - })); - } else if(_.has(body, 'artist')) { - callback(dbot.t('artist', { - 'artist': body.artist.name - })); - } - } - }); - }.bind(this)); - }; - this.api = { 'spotifySearch': function(query, callback) { this.authenticate(function(token) { @@ -157,11 +116,6 @@ var spotify = function(dbot) { commands['~spotify'].regex = [/^spotify (.*)/, 2]; this.commands = commands; - this.onLoad = function() { - dbot.api.link.addHandler(this.name, this.spotifyRegex, function(matches, name, callback) { - this.lookup(matches[0], callback); - }.bind(this)); - }.bind(this); }; exports.fetch = function(dbot) {