Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 147 lines (123 sloc) 4.377 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
# Description:
# Grabs movie scores from Rotten Tomatoes
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_ROTTEN_TOMATOES_API_KEY
#
# Commands:
# hubot rotten [me] <movie>
# hubot what's in theaters?
# hubot what's coming out in theaters?
# hubot what's coming out on (dvd|bluray)? - there is not a distinction between dvd and bluray
#
# Author:
# mportiz08

class Rotten
  constructor: (@robot) ->
    @api_url = "http://api.rottentomatoes.com/api/public/v1.0"
    @api_key = process.env.HUBOT_ROTTEN_TOMATOES_API_KEY

  _links: (match, callback) =>
    return callback @__links[match] if @__links
    @send "#{@api_url}/lists.json", {},
      (err, res, body) =>
        @__links = JSON.parse(body)['links']
        callback @__links[match]

  _movie_links: (match, callback) =>
    return callback @__movie_links[match] if @__movie_links
    @_links 'movies',
      (link) =>
        @send link, {},
          (err, res, body) =>
            @__movie_links = JSON.parse(body)['links']
            callback @__movie_links[match]

  _dvd_links: (match, callback) =>
    return callback @__dvd_links[match] if @__dvd_links
    @_links 'dvds',
      (link) =>
        @send link, {},
          (err, res, body) =>
            @__dvd_links = JSON.parse(body)['links']
            callback @__dvd_links[match]

  in_theaters: (callback) =>
    @_movie_links 'in_theaters',
      (match) =>
        @send match,
          page_limit: 20
          country: 'us'
          (err, res, body) ->
            movies = JSON.parse(body)['movies']

            unless err? or movies?
              return callback("Couldn't find anything, sorry.")

            callback null, (new RottenMovie(movie) for movie in movies)

  upcoming: (type, callback) =>
    link_list = switch type
      when 'movies'
        @_movie_links
      when 'dvds'
        @_dvd_links
      else
        @_movie_links

    link_list 'upcoming',
      (match) =>
        @send match,
          page_limit: 20
          country: 'us'
          (err, res, body) ->
            movies = JSON.parse(body)['movies']
           
            unless err? or movies?
              return callback("Couldn't find anything, sorry.")

            callback null, (new RottenMovie(movie) for movie in movies)

  search: (query, callback) =>
    @send "#{@api_url}/movies.json",
      q: query
      page_limit: 1
      (err, res, body) ->
        movie = JSON.parse(body)['movies'][0]
           
        unless err? or movie?
          return callback("Couldn't find anything, sorry.")

        callback null, new RottenMovie(movie)

    return

  send: (url, options, callback) =>
    options.apikey = @api_key
    @robot.http(url).query(options).get()(callback)

class RottenMovie
  constructor: (@info) ->

  toDetailedString: ->
    "#{@info['title']} (#{@info['year']})\n" +
    "#{@info['runtime']} min, #{@info['mpaa_rating']}\n\n" +
    "Critics:\t" + "#{@info['ratings']['critics_score']}%" +
      "\t\"#{@info['ratings']['critics_rating']}\"\n" +
    "Audience:\t" + "#{@info['ratings']['audience_score']}%" +
      "\t\"#{@info['ratings']['audience_rating']}\"\n\n" +
    "#{@info['critics_consensus']}"

  toReleaseString: ->
    "#{@info['title']}, #{@info['release_dates']['dvd'] || @info['release_dates']['theater']} (#{@info['ratings']['audience_score']}%)"

  toString: ->
    "#{@info['title']} (#{@info['ratings']['audience_score']}%)"

module.exports = (robot) ->
  rotten = new Rotten robot

  robot.respond /rotten (me )?(.*)$/i, (message) ->
    message.send "Well, let's see..."
    rotten.search message.match[2], (err, movie) ->
      unless err?
        message.send movie.toDetailedString()
      else
        message.send err

  robot.respond /what(\')?s in theaters(\?)?$/i, (message) ->
    message.send "Well, let's see..."
    rotten.in_theaters (err, movies) ->
      unless err?
        message.send (movie.toString() for movie in movies).join("\n")
      else
        message.send err

  robot.respond /what(\')?s coming out ((on (dvd|blu(-)?ray))|(in theaters))(\?)?$/i, (message) ->
    message.send "Well, let's see..."
    type = if message.match[2] is 'in theaters' then 'movies' else 'dvds'
    rotten.upcoming type, (err, movies) ->
      unless err?
        message.send (movie.toReleaseString() for movie in movies).join("\n")
      else
        message.send err
Something went wrong with that request. Please try again.