From a00a5dafed99f26b7440c8e330e37a96c868fc46 Mon Sep 17 00:00:00 2001 From: Thomas Menari Date: Tue, 12 Feb 2013 19:31:34 +0000 Subject: [PATCH] fix spotify search crash --- modules/spotify/spotify.js | 18 ++++++++++-------- modules/spotify/strings.json | 3 +++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/modules/spotify/spotify.js b/modules/spotify/spotify.js index a550241..b018cd9 100644 --- a/modules/spotify/spotify.js +++ b/modules/spotify/spotify.js @@ -21,15 +21,15 @@ var spotify = function(dbot) { json: true }, function(error, response, body) { if (!error && response.statusCode == 200) { - var s = "\u00039spotify\u000f"; - if (body.hasOwnProperty('track')) { - event.reply(dbot.t("track", {s: s, artist: _.map(body.track.artists, function(a) { return a.name }).join(', '), album: body.track.album.name, track: body.track.name})); + var spotify = "\u00039spotify\u000f"; + if (_.has(body, 'track')) { + event.reply(dbot.t("track", {s: spotify, artist: _.map(body.track.artists, function(a) { return a.name }).join(', '), album: body.track.album.name, track: body.track.name})); } - else if (body.hasOwnProperty('album')) { - event.reply(dbot.t("album", {s: s, artist: body.album.artist, album: body.album.name})); + else if (_.has(body, 'album')) { + event.reply(dbot.t("album", {s: spotify, artist: body.album.artist, album: body.album.name})); } - else if (body.hasOwnProperty('artist')) { - event.reply(dbot.t("artist", {s: s, artist: body.artist.name})); + else if (_.has(body, 'artist')) { + event.reply(dbot.t("artist", {s: spotify, artist: body.artist.name})); } } }); @@ -44,11 +44,13 @@ var spotify = function(dbot) { json: true }, function(error, response, body) { if (!error && response.statusCode == 200) { - if (body.hasOwnProperty('tracks')) { + if (_.has(body, 'tracks') && body.tracks[0] && _.has(body.tracks[0], 'href')) { var t = body.tracks[0].href; t = t.replace(/:/g, '/'); t = t.replace(/spotify/, 'http://open.spotify.com'); event.reply(t); + } else { + event.reply(dbot.t("not-found", {s: "\u00039spotify\u000f"})); } } }); diff --git a/modules/spotify/strings.json b/modules/spotify/strings.json index 5deef8b..e929996 100644 --- a/modules/spotify/strings.json +++ b/modules/spotify/strings.json @@ -7,5 +7,8 @@ }, "track": { "english": "{s} [track] {artist} - {track} (from {album})" + }, + "not-found": { + "english": "{s} No results." } }