/src/scripts/tvshow.coffee

https://github.com/stuartf/hubot-scripts · CoffeeScript · 75 lines · 59 code · 1 blank · 15 comment · 13 complexity · 8877489eb0644cf74ac6b2e9499ac7ba MD5 · raw file

  1. # Description:
  2. # None
  3. #
  4. # Dependencies:
  5. # "xml2js": "0.1.14"
  6. #
  7. # Configuration:
  8. # None
  9. #
  10. # Commands:
  11. # hubot tvshow me <show> - Show info about <show>
  12. #
  13. # Author:
  14. # victorbutler
  15. xml2js = require "xml2js"
  16. module.exports = (robot) ->
  17. robot.respond /tvshow(?: me)? (.*)/i, (msg) ->
  18. query = encodeURIComponent(msg.match[1])
  19. msg.http("http://services.tvrage.com/feeds/full_search.php?show=#{query}")
  20. .get() (err, res, body) ->
  21. if res.statusCode is 200 and !err?
  22. parser = new xml2js.Parser()
  23. parser.parseString body, (err, result) ->
  24. if result.show?
  25. if result.show.length?
  26. show = result.show[0]
  27. else
  28. show = result.show
  29. if show.status == "Canceled/Ended"
  30. response = "#{show.name} aired for #{show.seasons} season"
  31. response += "s" if show.seasons > 1
  32. response += " from #{show.started} till #{show.ended}"
  33. response += " on #{show.network['#']}" if show.network? and show.network['#']
  34. response += " #{show.link}"
  35. msg.reply response
  36. else
  37. # get more info
  38. msg.http("http://services.tvrage.com/feeds/episode_list.php?sid=#{show.showid}")
  39. .get() (err, res, details) ->
  40. if res.statusCode is 200 and !err?
  41. parser = new xml2js.Parser()
  42. parser.parseString details, (err, showdetails) ->
  43. now = new Date();
  44. ecb = (season_arr) ->
  45. for episode in season_arr.episode
  46. edate = new Date()
  47. edate.setTime(Date.parse(episode.airdate+' '+show.airtime))
  48. if edate.getTime() > now.getTime()
  49. return episode
  50. if showdetails.Episodelist.Season.length?
  51. unaired = ecb season for season in showdetails.Episodelist.Season
  52. else
  53. unaired = ecb showdetails.Episodelist.Season
  54. if unaired
  55. response = "#{show.name} is a #{show.status} which started airing #{show.started}. The next show, titled \"#{unaired.title}\" is scheduled for #{unaired.airdate}"
  56. response += " #{show.day}" if show.day?
  57. response += " at #{show.airtime}" if show.airtime?
  58. response += " on #{show.network['#']}" if show.network? and show.network['#']
  59. response += " #{unaired.link}"
  60. msg.reply response
  61. else
  62. response = "#{show.name} is a #{show.status} with #{show.seasons} (or more) season"
  63. response += "s" if show.seasons > 1
  64. response += " beginning #{show.started}"
  65. response += " at #{show.airtime}" if show.airtime?
  66. response += " on #{show.network['#']}" if show.network? and show.network['#']
  67. response += " #{show.link}"
  68. msg.reply response
  69. else
  70. msg.reply "Sorry, there was an error looking up your show"
  71. else
  72. msg.reply "I couldn't find TV show " + msg.match[1]
  73. else
  74. msg.reply "Sorry, there was an error looking up your show"