Class: Lita::Handlers::OnewheelFilmplot

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_filmplot.rb

Instance Method Summary collapse

Instance Method Details

#get_plot(response) ⇒ Object



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
# File 'lib/lita/handlers/onewheel_filmplot.rb', line 20

def get_plot(response)

  title = response.matches[0][0]
  url_root = 'https://www.rottentomatoes.com'

  if title.match? '\s'
    # search

    search_term = CGI.escape(title)
    search_url = "https://www.rottentomatoes.com/search/?search=#{search_term}"
    Lita.logger.debug("Search mode!  #{search_url}")
    r = RestClient.get(search_url)
    if match = r.match(/RT.PrivateApiV2FrontendHost, '.*', ({.*})/)
      movie_json = JSON.parse match[1]
      slug = movie_json['movies'][0]['url']
      get_url = "#{url_root}#{slug}"
      Lita.logger.debug("Searching for #{get_url}")
    end
  else
    movie_slug = title.gsub ' ', '_'
    get_url = "#{url_root}/m/#{movie_slug}"
    Lita.logger.debug("Getting #{get_url}")
  end

  begin
    r = RestClient.get(get_url)
    noko_doc = Nokogiri::HTML(r)
    node = noko_doc.css('div#movieSynopsis')
    Lita.logger.debug("Replying with #{node.text.strip}")
    response.reply node.text.strip
  rescue RestClient::ResourceNotFound => e
    response.reply "#{movie_slug} not found."
  end
end