Class: ImdbParty::Imdb

Inherits:
Object
  • Object
show all
Includes:
HTTParty, HTTParty::Icebox
Defined in:
lib/imdb_party/imdb.rb

Instance Method Summary collapse

Methods included from HTTParty::Icebox

included

Constructor Details

#initialize(options = {}) ⇒ Imdb

Returns a new instance of Imdb.



9
10
11
# File 'lib/imdb_party/imdb.rb', line 9

def initialize(options={})
  self.class.base_uri 'anonymouse.org/cgi-bin/anon-www.cgi/http://app.imdb.com' if options[:anonymize]
end

Instance Method Details

#build_url(path, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/imdb_party/imdb.rb', line 13

def build_url(path, params={})
  default_params = {"api" => "v1", "appid" => "iphone1_1", "apiPolicy" => "app1_1", "apiKey" => "2wex6aeu6a8q9e49k7sfvufd6rhh0n", "locale" => "en_US", "timestamp" => Time.now.to_i}

  query_params = default_params.merge(params)
  query_param_array = []
  
  host = self.class.base_uri.gsub('http://', '').to_s

  query_params.each_pair{|key, value| query_param_array << "#{key}=#{URI.escape(value.to_s)}" }
  uri = URI::HTTP.build(:scheme => 'https', :host => host, :path => path, :query => query_param_array.join("&"))

  query_param_array << "sig=app1-#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha1'), default_params["apiKey"], uri.to_s)}"

  uri = URI::HTTP.build(:scheme => 'https', :host => host, :path => path, :query => query_param_array.join("&"))
  uri.to_s
end

#find_by_title(title) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/imdb_party/imdb.rb', line 30

def find_by_title(title)
  movie_results = []
  url = build_url('/find', :q => title)
  
  results = self.class.get(url).parsed_response
  
  if results["data"] && results["data"]["results"]
    results["data"]["results"].each do |result_section|
      result_section["list"].each do |r|
        next unless r["tconst"] && r["title"]
        h = {:title => r["title"], :year => r["year"], :imdb_id => r["tconst"], :kind => r["type"]}
        h.merge!(:poster_url => r["image"]["url"]) if r["image"] && r["image"]["url"]
        movie_results << h
      end
    end
  end
  
  movie_results
end

#find_movie_by_id(imdb_id) ⇒ Object



50
51
52
53
54
55
# File 'lib/imdb_party/imdb.rb', line 50

def find_movie_by_id(imdb_id)
  url = build_url('/title/maindetails', :tconst => imdb_id)
  
  result = self.class.get(url).parsed_response
  Movie.new(result["data"]) if result["data"]
end


64
65
66
67
68
69
# File 'lib/imdb_party/imdb.rb', line 64

def popular_shows
  url = build_url('/chart/tv')

  results = self.class.get(url).parsed_response
  results["data"]["list"].map { |r| {:title => r["title"], :imdb_id => r["tconst"], :year => r["year"], :poster_url => (r["image"] ? r["image"]["url"] : nil)} }
end

#top_250Object



57
58
59
60
61
62
# File 'lib/imdb_party/imdb.rb', line 57

def top_250
  url = build_url('/chart/top')

  results = self.class.get(url).parsed_response
  results["data"]["list"]["list"].map { |r| {:title => r["title"], :imdb_id => r["tconst"], :year => r["year"], :poster_url => (r["image"] ? r["image"]["url"] : nil)} }
end