Skip to content

Instantly share code, notes, and snippets.

@catkins
Created May 21, 2015 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catkins/bab74091a7fe77248a45 to your computer and use it in GitHub Desktop.
Save catkins/bab74091a7fe77248a45 to your computer and use it in GitHub Desktop.
Hubot Spotify Script
# Description:
# Messing around with the Spotify API. https://developer.spotify.com/web-api/
#
# Commands:
# hubot spotify (album|track|artist|playlist) <query> - Searches Spotify for the query and returns the embed link
#
# Author:
# catkins (2015)
#
SPOTIFY_API_SEARCH_ENDPOINT = 'https://api.spotify.com/v1/search'
TYPE_KEYS =
album: 'albums'
track: 'tracks'
artist: 'artists'
playlist: 'playlists'
module.exports = (robot) ->
robot.respond /spotify (album|track|artist|playlist) (.*)/i, (msg) ->
[_, type, query] = msg.match
robot.http(SPOTIFY_API_SEARCH_ENDPOINT)
.header('Accept', 'application/json')
.query(q: query, type: type, limit: 1)
.get() (err, res, body) ->
if err
msg.send err
return
data = JSON.parse body
result = extractSearchResult data, type
if result
msg.send buildResponse(result)
else
msg.send "No search results for #{query}"
buildResponse = (result) ->
response = result.name
response += " - #{extractArtists(result)}" if result.artists # for tracks
response += " - #{extractAlbum(result)}" if result.album # for tracks
response += " - #{extractOwner(result)}" if result.owner # for playlists
response += "\n#{extractUrl(result)}"
extractName = (result) ->
result.name
extractUrl = (result) ->
result.external_urls.spotify
extractAlbum = (result) ->
result.album.name
extractOwner = (result) ->
result.owner.id
extractArtists = (result) ->
result.artists.map((artist) -> artist.name).join ', '
# just returns the first result for the time being
extractSearchResult = (data, type) ->
typeKey = TYPE_KEYS[type]
items = data[typeKey].items
items[0]
var SPOTIFY_API_SEARCH_ENDPOINT, TYPE_KEYS;
SPOTIFY_API_SEARCH_ENDPOINT = 'https://api.spotify.com/v1/search';
TYPE_KEYS = {
album: 'albums',
track: 'tracks',
artist: 'artists',
playlist: 'playlists'
};
module.exports = function(robot) {
return robot.respond(/spotify (album|track|artist|playlist) (.*)/i, function(msg) {
var type, query;
type = msg.match[1];
query = msg.match[2];
robot.http(SPOTIFY_API_SEARCH_ENDPOINT).header('Accept', 'application/json').query({
q: query,
type: type,
limit: 1
}).get()(function(err, res, body) {
var data, result;
if (err) {
msg.send(err);
return;
}
data = JSON.parse(body);
result = extractSearchResult(data, type);
if (result) {
msg.send(buildResponse(result));
} else {
msg.send("No search results for " + query);
}
});
});
};
function buildResponse(result) {
var response;
response = result.name;
if (result.artists) {
response += " - " + (extractArtists(result));
}
if (result.album) {
response += " - " + (extractAlbum(result));
}
if (result.owner) {
response += " - " + (extractOwner(result));
}
return response += "\n" + (extractUrl(result));
};
function extractName(result) {
return result.name;
};
function extractUrl(result) {
return result.external_urls.spotify;
};
function extractAlbum(result) {
return result.album.name;
};
function extractOwner(result) {
return result.owner.id;
};
function extractArtists(result) {
return result.artists.map(function(artist) {
return artist.name;
}).join(', ');
};
function extractSearchResult(data, type) {
var items, typeKey;
typeKey = TYPE_KEYS[type];
items = data[typeKey].items;
return items[0];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment