last-fm.coffee 991 B

123456789101112131415161718192021222324252627282930313233343536
  1. #
  2. # Description:
  3. # Last (or current) played song by a user in Last.fm
  4. #
  5. # Dependencies:
  6. # None
  7. #
  8. # Configuration:
  9. # HUBOT_LASTFM_APIKEY
  10. #
  11. # Commands:
  12. # hubot what's <last FM user> playing - Returns song name and artist
  13. # hubot what am I playing - only works if last.fm nick = username who typed it
  14. #
  15. # Author:
  16. # windhamdavid
  17. getSong = (msg, usr) ->
  18. user = usr ? msg.match[2]
  19. apiKey = process.env.HUBOT_LASTFM_APIKEY
  20. msg.http('http://ws.audioscrobbler.com/2.0/?')
  21. .query(method: 'user.getrecenttracks', user: user, api_key: apiKey, format: 'json')
  22. .get() (err, res, body) ->
  23. results = JSON.parse(body)
  24. if results.error
  25. msg.send results.message
  26. return
  27. song = results.recenttracks.track[0]
  28. msg.send "#{song.name} by #{song.artist['#text']}"
  29. module.exports = (robot) ->
  30. robot.respond /what(')?s (.*) playing/i, (msg) ->
  31. getSong(msg)
  32. robot.respond /what am I playing/i, (msg) ->
  33. getSong(msg, msg.message.user.name)