Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

caching seems to work

  • Loading branch information...
commit 3c31f344d677b5ad3c9ad647b5d9045d0e5f77b5 1 parent b107285
@yalcin authored
View
4 .gitignore
@@ -1,3 +1,7 @@
.bundle
pkg
ruby-imdb*.gem
+.idea
+.yardoc
+doc
+
View
8 lib/imdb.rb
@@ -5,6 +5,14 @@
require "json"
require "mongo_mapper"
+
+class Array
+ def to_hash(default=nil)
+ Hash[ *inject([]) { |a, value| a.push value, default || yield(value) } ]
+ end
+end
+
+
require "configuration"
require "imdb/skeleton"
require "imdb/search"
View
48 lib/imdb/movie.rb
@@ -1,30 +1,50 @@
module IMDB
+ # Get movie information with IMDB movie id.
+ # @example Get Yahsi Bati movie title and cast listing [http://www.imdb.com/title/tt1567448/]
+ # m = IMDB::Movie.new('1567448')
+ # puts m.title
class Movie < IMDB::Skeleton
- attr_accessor :link, :id
+ attr_accessor :link, :imdb_id
- def initialize(id)
- super(:poster, :title, :cast, :photos)
- @id = id
- @link = "http://www.imdb.com/title/tt#{id}"
+
+ def initialize(id_of)
+ super("Movie", {:imdb_id => String,
+ :poster => String,
+ :title => String,
+ :cast => Array,
+ :photos => Array}, [:imdb_id])
+
+ @imdb_id = id_of
+
+ @link = "http://www.imdb.com/title/tt#{@imdb_id}"
end
-
+
+ # Get movie poster address
+ # @return [String]
def poster
doc.at("a[@name='poster'] img")['src'][/http:.+/] + '.jpg' rescue nil
end
-
+
+ # Get movie title
+ # @return [String]
def title
doc.at("//head/meta[@name='title']")["content"].split(/\(\d+\)/)[0].strip!
end
+ # Get movie cast listing
+ # @return [Array]
def cast
doc.search("table.cast tr").map do |link|
picture = link.children[0].search("img")[0]["src"] rescue nil
name = link.children[1].content rescue nil
+ profile_id = link.children[1].search('a[@href^="/name/nm"]').first["href"].split("nm")[1].gsub(/\//,'') rescue nil
char = link.children[3].content rescue nil
- IMDB::Person.new(name, char, picture, picture) if name and char and picture != nil
+ IMDB::Person.new(@imdb_id, name, char, profile_id, picture) unless name.nil? and char.nil? and picture.nil? and profile_id.nil?
end
end
-
+
+ # Get movie photos
+ # @return [Array]
def photos
begin
doc.search("img").map do |img|
@@ -32,21 +52,21 @@ def photos
img["src"]
end
end
- rescue
+ rescue
nil
end
end
-
+
private
-
+
def doc
if caller[0] =~ /`([^']*)'/ and $1 == "cast"
@doc = Nokogiri::HTML(open("#{@link}/fullcredits"))
else
@doc = Nokogiri::HTML(open("#{@link}"))
- end
+ end
end
-
+
end # Movie
end # IMDB
View
64 lib/imdb/person.rb
@@ -1,29 +1,45 @@
module IMDB
class Person < IMDB::Skeleton
+ def initialize(imdb_id, name, char, profile, picture)
+ super("Person",{:imdb_id => String,
+ :name => String,
+ :char => String,
+ :profile => String,
+ :picture => String}, [:imdb_id, :profile])
+ @imdb_id = imdb_id
+ @name = name
+ @char = char
+ @profile = profile
+ @picture = picture
+ end
- def initialize(name, char, profile, picture)
- super(:name, :char, :profile, :picture)
- @name = name
- @char = char
- @profile = profile
- @picture = picture
- end
-
- def name
- return @name
- end
-
- def char
- return @char
- end
-
- def profile
- return @profile
- end
-
- def picture
- return @picture
- end
- end # Person
+ def imdb_id
+ @imdb_id
+ end
+
+ def name
+ @name
+ end
+
+ def char
+ @char
+ end
+
+ def profile
+ @profile
+ end
+
+ def picture
+ @picture
+ end
+
+ def profile
+ @profile
+ end
+
+ def to_s
+ "Name: #{@name} \n Char: #{@char} \n"
+ end
+ end # Person
end
View
83 lib/imdb/skeleton.rb
@@ -1,23 +1,71 @@
module IMDB
+ # Interface of IMDB data classes.
class Skeleton
attr_accessor :model, :method_names
-
- def initialize(*method_names)
- @method_names = method_names
- @model = Class.new do
- include MongoMapper::Document
-
- method_names.each { |m|
- key m, String
- }
+
+ class << self
+
+ end
+
+ def initialize(model_name = "", method_names = {}, keys = [])
+ if IMDB::Configuration.caching
+ @model = Class.new do
+ include MongoMapper::Document
+ set_collection_name model_name
+ method_names.each { |m, t|
+ key m, t
+ }
+ end
+ class_eval do
+ method_names.each_key { |meth|
+ unless keys.include?(meth)
+ old_meth = "old_#{meth}".to_sym
+ alias_method old_meth, meth.to_sym
+ define_method meth do
+ k = keys.to_hash { |k| k; self.send(k) }
+ @db_query = self.model.first(k)
+
+ if @db_query.nil?
+ @db_query = self.model.new(keys.to_hash { |k| k; self.send(k)})
+ @db_query.save
+ end
+
+ if @db_query[meth].nil? or (@db_query[meth].length.zero? if @db_query[meth].kind_of?(Array))
+
+ a = send(old_meth)
+ if a.kind_of?(Array)
+ a.compact!
+ a.map! {
+ |c|
+ c.to_hash
+ }
+ @db_query[meth] = a
+ else
+ @db_query[meth] = a
+ end
+ @db_query.save
+ end
+ @db_query[meth]
+ end
+ end
+ }
+ end
end
+ @method_names = method_names
end
-
+
+ # Serialize method's output to json
def to_json(*a)
+ tmp_hash = to_hash
+
+ tmp_hash.to_json(*a)
+ end
+
+
+ def to_hash(*a)
tmp_hash = {}
-
- @method_names.collect! { |x|
- evaled = eval("self.#{x}")
+ @method_names.each_key { |x|
+ evaled = self.send x
if evaled.kind_of?(Array)
tmp_hash[x] = evaled.collect! {|e|
e
@@ -26,14 +74,11 @@ def to_json(*a)
tmp_hash[x] = evaled
end
}
-
- tmp_hash.to_json(*a)
+ tmp_hash
end
-
+
def self.json_create(o)
new(*o['data'])
- end
+ end
end
-
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.