Skip to content

Commit

Permalink
Merge pull request #13 from maddox/fix-title-search
Browse files Browse the repository at this point in the history
Fix title search
  • Loading branch information
maddox committed Jun 25, 2012
2 parents fd4c1f7 + 139da57 commit a370d94
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
49 changes: 28 additions & 21 deletions lib/imdb_party/imdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ class Imdb
include HTTParty
include HTTParty::Icebox
cache :store => 'file', :timeout => 120, :location => Dir.tmpdir

base_uri 'app.imdb.com'
format :json

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

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("&"))

Expand All @@ -28,34 +29,40 @@ def build_url(path, params={})
uri = URI::HTTP.build(:scheme => 'https', :host => base_host, :path => the_path, :query => query_param_array.join("&"))
uri.to_s
end

def find_by_title(title)
default_find_by_title_params = {"json" => "1", "nr" => 1, "tt" => "on", "q" => 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"]

results = self.class.get("http://www.imdb.com/xml/find", :query => default_find_by_title_params).parsed_response
puts results.inspect

keys = ["title_exact", "title_popular", "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



end

movie_results
end

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

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

Expand All @@ -69,6 +76,6 @@ def popular_shows
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

end
end
34 changes: 17 additions & 17 deletions test/search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,84 @@

class SearchTest < Test::Unit::TestCase
context "imdb" do
setup do
setup do
@imdb = ImdbParty::Imdb.new
end

context "search for title" do
setup do
@results = @imdb.find_by_title("ratatouille")
end

should "have at least 15 results" do
assert (@results.size >= 15)
end
end

context "search for title with spaces in the name" do
setup do
@results = @imdb.find_by_title("the truman show")
end

should "have at least 1 result" do
assert (@results.size >= 1)
end
end

context "search for bad title with no results" do
setup do
@results = @imdb.find_by_title("sdkljlkkl123j4lk23kl3")
end

should "have 0 results" do
assert_equal 0, @results.size
end
end

context "find movie by id" do
setup do
@movie = @imdb.find_movie_by_id("tt0382932")
end

should "be a ImdbParty::Movie" do
assert_equal ImdbParty::Movie, @movie.class
end
end

context "find top 250 movies" do
setup do
@movies = @imdb.top_250
end

should "be an Array of Hashes" do
assert_equal Array, @movies.class
assert_equal Hash, @movies.first.class
end
end

context "find popular shows" do
setup do
@shows = @imdb.popular_shows
end

should "be an Array of Hashes" do
assert_equal Array, @shows.class
assert_equal Hash, @shows.first.class
end
end

end

context "anonymous imdb" do
setup do
setup do
@imdb = ImdbParty::Imdb.new(:anonymize => true)
end

context "find popular shows" do
setup do
@shows = @imdb.popular_shows
end

should "be an Array of Hashes" do
assert_equal Array, @shows.class
assert_equal Hash, @shows.first.class
Expand Down

0 comments on commit a370d94

Please sign in to comment.