Skip to content

Instantly share code, notes, and snippets.

@BilalBudhani
Last active December 21, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BilalBudhani/6256046 to your computer and use it in GitHub Desktop.
Save BilalBudhani/6256046 to your computer and use it in GitHub Desktop.
ruby imdb_fetch.rb -n 10 "Morgan Freeman"
require 'rubygems'
require 'nokogiri'
require 'open-uri'
### Functions to work around
def fetch_top_movies(top_no)
top_movies = Array.new
ctr = 0
url = 'http://www.imdb.com/chart/top'
document = Nokogiri::HTML(open(url))
document.search("a[@href^='/title/tt']").each do |movie|
m = Hash.new
m[:id] = movie['href'][/\d+/]
m[:title] = movie.inner_html
top_movies << m
ctr += 1
break if ctr == top_no
end
top_movies
end
def fetch_cast_members(movie_id)
url = "http://www.imdb.com/title/tt#{movie_id}/combined"
document = Nokogiri::HTML(open(url))
document.search("table.cast td.nm a").map{ |link| link.content.strip }
end
def nothing_to_do
puts 'Nothing to do actually'
end
def get_top_movies(top_no)
top = fetch_top_movies top_no
ctr = 0
movies_to_return = Array.new
top.each do |movie|
ctr += 1
movies_to_return << movie
break if ctr == top_no
end
movies_to_return
end
def show_movies_output(top_movies)
puts "======================== #{top_movies.count} Matches found ============================"
top_movies.each do |movie|
output = "Movie: #{movie[:title]}"
puts output
end
end
def filter_cast(top_movies, filter)
filtered_movies = Array.new
top_movies.each do |movie|
puts "fetching cast members for #{movie[:title]}..."
filter_cast = fetch_cast_members(movie[:id].to_s).select{|cast| cast[/#{filter}/]}
if !filter_cast.empty?
filtered_movies << movie
end
end
filtered_movies
end
### Before work logic
if ARGV[0].nil?
what = false
else
what = ARGV[0]
end
if ARGV[1].nil?
top = false
else
top = ARGV[1].to_i
end
if ARGV[2].nil?
filter = false
else
filter = ARGV[2]
end
### Actual processing
if what and what == '-n'
puts 'fetching...'
top_movies = get_top_movies top
if filter
puts "\n filtering..."
top_movies = filter_cast top_movies, filter
end
show_movies_output top_movies
else
nothing_to_do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment