Skip to content

Instantly share code, notes, and snippets.

@jtblin
Last active August 29, 2015 14:02
Show Gist options
  • Save jtblin/2f6e63788dcc4fc174dc to your computer and use it in GitHub Desktop.
Save jtblin/2f6e63788dcc4fc174dc to your computer and use it in GitHub Desktop.
Hubot Rap Genius script
# Description
# Rap Genius client
#
# Author:
# jtblin
rg = require "rapgenius-js"
module.exports = (robot) ->
robot.respond /(rapgenius|rg) (artist|song|explain)? (.*)/i, (msg) ->
type = msg.match[2].toLowerCase()
name = msg.match[3]
switch type
when 'artist'
rg.searchArtist name, (err, artist) ->
msg.send "Rap artist found [name=#{artist.name}, link=#{artist.link}, popular-songs=#{artist.popularSongs.length}]"
when 'song'
rg.searchSong name, (err, songs) ->
msg.send songs.length + " songs found"
if songs.length then msg.send "First one is #{songs[0].name}, artists: #{songs[0].artists}, url: #{songs[0].link}"
when 'explain'
explainMe msg, name
else msg.send "Try again."
robot.hear /(what is |can you )?(the meaning of|explain) (life|death|love)/i, (msg) ->
if msg.match[3]
name = msg.match[3]
else
name = msg.match[2]
explainMe msg, name
explainMe = (msg, name) ->
rg.searchSong name, (err, songs) ->
if songs.length
rg.searchLyricsAndExplanations songs[0].link, (err, data) ->
if err then return msg.send "Something's gone awry"
explanations = data.explanations
keys = Object.keys(explanations)
explanation = explanations[keys[Math.floor(Math.random() * (keys.length-1))]]
msg.send explanation if explanation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment