Skip to content

Instantly share code, notes, and snippets.

@phawk
Last active August 28, 2015 21:59
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 phawk/cdf480f779dff0dcf0dc to your computer and use it in GitHub Desktop.
Save phawk/cdf480f779dff0dcf0dc to your computer and use it in GitHub Desktop.
Hubot spotify-mac-api client
module.exports = (robot) ->
api_url = process.env.HUBOT_SPOTIFY_MAC_API_URL
api_secret = process.env.HUBOT_SPOTIFY_MAC_API_SECRET
options = {}
options.commands = [
"play (track|artist|album) <name> - Play a given track, artist or album",
"play queue <1,2,3> - Play a given search result",
"pause - Pause the current song",
"toggle - Play/pause the current song",
"next - Play the next song from the playlist",
"previous - Play the previous song from the playlist",
"current song - Shows what song I'm currently playing",
"mute - Mute/unmute the sound",
"search (track|album|artist) <query> - Search for a track on Spotify and queue it to be played"
]
# starting volume in spotify
options.volume = 100
robot.respond /spotify$/i, (msg) ->
msg.send options.commands.join("\n")
# controls
robot.respond /toggle$/i, (msg) ->
msg.send "Okay, toggling play/pause in Spotify"
robot.http(api_url+ "/toggle")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
# your code here
robot.respond /play$/i, (msg) ->
msg.send "Playing the current song in Spotify"
robot.http(api_url+ "/play")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
# your code here
robot.respond /(pause|stop)$/i, (msg) ->
msg.send "Pausing the current song in Spotify"
robot.http(api_url+ "/pause")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
# your code here
robot.respond /(next|play next|play the next song)$/i, (msg) ->
robot.http(api_url+ "/next")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send "And now I'm playing "+ body
robot.respond /(previous|prev|play previous|play the previous song)$/i, (msg) ->
robot.http(api_url+ "/prev")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send "Playing this song again: "+ body
robot.respond /mute$/i, (msg) ->
robot.http(api_url+ "/mute")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send body
robot.respond /unmute$/i, (msg) ->
robot.http(api_url+ "/mute")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send body
robot.respond /(current|song|track|current song|current track)$/i, (msg) ->
robot.http(api_url+ "/playing")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send "The current song I'm playing is "+ body
robot.respond /search ?(track|song|album|artist)? (.*)$/i, (msg) ->
query = msg.match[2]
if msg.match[1]?
switch msg.match[1]
when "track" or "song" then type = "track"
when "album" then type = "album"
when "artist" then type = "artist"
else
type = "track"
data = 'secret='+api_secret+'&type='+encodeURIComponent(type)+'&query='+encodeURIComponent(query)
robot.http(api_url+ "/search")
.header('Content-Type', 'application/x-www-form-urlencoded')
.post(data) (err, res, body) ->
msg.send body
robot.respond /play ?(track|album|artist|queue)? (.*)$/i, (msg) ->
play_type = encodeURIComponent(msg.match[1])
query = encodeURIComponent(msg.match[2])
url = String(api_url + "/play/" + play_type + "/" + query)
robot.http(url)
.header('Content-Type', 'application/x-www-form-urlencoded')
.post("secret="+api_secret) (err, res, body) ->
msg.send body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment