Class: ImdbParty::Imdb
- Inherits:
-
Object
- Object
- ImdbParty::Imdb
- Includes:
- HTTParty
- Defined in:
- lib/imdb_party/imdb.rb
Instance Method Summary collapse
- #build_url(path, params = {}) ⇒ Object
- #find_by_title(title) ⇒ Object
- #find_movie_by_id(imdb_id) ⇒ Object
-
#initialize(options = {}) ⇒ Imdb
constructor
A new instance of Imdb.
- #popular_shows ⇒ Object
- #top_250 ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Imdb
Returns a new instance of Imdb.
10 11 12 |
# File 'lib/imdb_party/imdb.rb', line 10 def initialize(={}) self.class.base_uri 'anonymouse.org/cgi-bin/anon-www.cgi/https://app.imdb.com' if [:anonymize] end |
Instance Method Details
#build_url(path, params = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/imdb_party/imdb.rb', line 14 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 = [] base_uri = URI.parse(self.class.base_uri) base_host = base_uri.host the_path = base_uri.path + path query_params.each_pair{|key, value| query_param_array << "#{key}=#{URI.escape(value.to_s)}" } uri = URI::HTTP.build(:scheme => 'https', :host => base_host, :path => the_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 => base_host, :path => the_path, :query => query_param_array.join("&")) uri.to_s end |
#find_by_title(title) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/imdb_party/imdb.rb', line 33 def find_by_title(title) default_find_by_title_params = {"json" => "1", "nr" => 1, "tt" => "on", "q" => title} movie_results = [] results = self.class.get("http://www.imdb.com/xml/find", :query => default_find_by_title_params).parsed_response keys = ["title_popular", "title_exact", "title_approx", "title_substring"] keys.each do |key| if results[key] results[key].each do |r| next unless r["id"] && r["title"] year = r["title_description"].match(/^(\d\d\d\d)/) h = {:title => r["title"], :year => year, :imdb_id => r["id"], :kind => r["key"]} movie_results << h end end end movie_results end |
#find_movie_by_id(imdb_id) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/imdb_party/imdb.rb', line 58 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"]) end |
#popular_shows ⇒ Object
72 73 74 75 76 77 |
# File 'lib/imdb_party/imdb.rb', line 72 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_250 ⇒ Object
65 66 67 68 69 70 |
# File 'lib/imdb_party/imdb.rb', line 65 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 |