Skip to content

Commit

Permalink
added some fields to movies and use different api to access imdb
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien committed Mar 24, 2011
1 parent 145e2e0 commit f8bec16
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 36 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'http://rubygems.org'
gem 'rails', '3.0.5'
gem 'json'
gem 'nokogiri'
gem 'jquery-rails'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
Expand Down
58 changes: 53 additions & 5 deletions app/models/film.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,71 @@
require 'nokogiri'

class Film < ActiveRecord::Base
attr_accessible :imdb_url
attr_accessible :skip_imdb_url
attr_accessor :skip_imdb_url

after_save :get_imdb_data

def get_imdb_data
if imdb_url_changed? && !@skip_imdb_url
if !@skip_imdb_url && imdb_url_changed? && imdb_url.present?
puts "[imdb_url] changed"
update_imdb_data
end
end

def imdb_id
return nil unless imdb_url?
imdb_url.gsub("http://www.imdb.com/title/", "").gsub("/", "")
end

def update_imdb_data
@skip_imdb_url = true
return false if imdb_id.nil?
movie_hash = MovieImport.by_imdb_id(imdb_id)
return false if movie_hash.nil?
update_attributes(
:imdb_rating => direct_imdb_rating, # movie_hash[:Rating].to_f, # inacurate
:genre => movie_hash[:Genre],
:synopsis => movie_hash[:Plot],
:director => movie_hash[:Director],
:writer => movie_hash[:Writer],
:thumbnail_url => movie_hash[:Poster]
)
end

# Not using the API for this one because not accurate
#
def direct_imdb_rating
return nil if imdb_url.nil?
doc = Nokogiri::HTML(open(imdb_url))
rating = doc.css(".rating-rating").first.content.split("/").first.to_f
puts "rating : #{rating}"
end

def init_imdb_data
@skip_imdb_url = true
update_attribute(:imdb_rating, rating)
movie_hash = MovieImport.by_imdb_title(title)
return false if movie_hash.nil?
update_attributes(
:imdb_url => "http://www.imdb.com/title/"+movie_hash[:ID]+"/",
:imdb_rating => movie_hash[:Rating].to_f,
:country => movie_hash[:Country],
:genre => movie_hash[:Genre],
:synopsis => movie_hash[:Plot],
:director => movie_hash[:Director],
:writer => movie_hash[:Writer],
:thumbnail_url => movie_hash[:Poster]
)
end

class << self
def fetch_imdb_data
# where(["imdb_url IS NULL"]).each do |film|
# puts "Initializing : #{film.title}"
# film.init_imdb_data
# end
where(["imdb_url IS NOT NULL AND (genre IS NULL OR director is NULL OR synopsis IS NULL)"]).each do |film|
puts "Upating : #{film.title}"
film.update_imdb_data
end
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20110324213119_add_director_to_films.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddDirectorToFilms < ActiveRecord::Migration
def self.up
add_column :films, :director, :string
add_column :films, :writer, :string
end

def self.down
remove_column :films, :writer
remove_column :films, :director
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110322190727) do
ActiveRecord::Schema.define(:version => 20110324213119) do

create_table "films", :force => true do |t|
t.string "title"
Expand All @@ -25,6 +25,8 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "festival_url"
t.string "director"
t.string "writer"
end

end
46 changes: 16 additions & 30 deletions lib/movie_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,24 @@
class MovieImport
class << self

def fetch
uri = "http://www.deanclatworthy.com/imdb/?q="

file = File.new(File.join(Rails.root, "cphpix_2011.txt"))
file.readlines.each do |movie_name|
movie_name.strip!
existing_movie = Film.find_by_title(movie_name)
if movie_name.first == "#" || existing_movie.present?
# puts "skipping movie #{movie_name}"
IMDB_API = "http://www.imdbapi.com/"

def by_imdb_title(title)
current_uri = IMDB_API + "?t=" + CGI::escape(title)
data = Net::HTTP.get_response(URI.parse(current_uri)).body
movie_hash = HashWithIndifferentAccess.new(JSON.parse(data))
if movie_hash["Response"] && movie_hash["Response"] == "Parse Error"
puts "[MovieImport] Couldn't find: #{title}"
return nil
else
current_uri = uri + movie_name.gsub(/[\s]/, '+')
data = Net::HTTP.get_response(URI.parse(current_uri)).body
movie = HashWithIndifferentAccess.new(JSON.parse(data))
if movie.has_key? :error
raise "Exceeded API usage limit" if movie[:error] == "Exceeded API usage limit"
puts movie
puts "Coudln't find: #{movie_name} puts #{current_uri}"
else
if Film.find_by_title(movie[:title]).blank?
Film.create!(
:title => movie[:title],
:imdb_url => movie[:imdburl],
:imdb_rating => movie[:rating],
:country => movie[:country],
:genre => movie[:genres]
)
else
puts "Already in db: #{movie_name} as (#{movie[:title]})"
end
end
return movie_hash
end
end
end

def by_imdb_id(imdb_id)
current_uri = IMDB_API + "?i=" + imdb_id
data = Net::HTTP.get_response(URI.parse(current_uri)).body
HashWithIndifferentAccess.new(JSON.parse(data))
end

def fetch_from_youtube
Expand Down

0 comments on commit f8bec16

Please sign in to comment.