Last active
August 29, 2015 14:21
-
-
Save shaiguitar/5eda74e4bfb817d7f88a to your computer and use it in GitHub Desktop.
Got a huge dir of movies, wants to know what movie to see according to IMDB ratings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
def safe_comparison(matrix) | |
matrix.map do |row| | |
row.map do |el| | |
el = el.to_s | |
end | |
end | |
end | |
results = Dir.glob('*/').map do |dir| | |
# build search query | |
movie_name = dir.gsub("/", "") | |
url = "http://www.imdb.com/find?ref_=nv_sr_fn&q=#{URI.escape(movie_name)}&s=all" | |
url = url.gsub("'","").gsub('"',"") # hack so cli doesn't barf, imdb doesn't care. | |
# get the imdb link for this movie | |
imdb_title_link = %x{xidel '#{url}' --extract "//css('a')/@href" | grep 'title/' | head -1| awk '{print "http://imdb.com/"$1}' } | |
imdb_title_link.chomp! | |
# rating, genre for this movie | |
info = %x{xidel '#{imdb_title_link}' --extract "//css('div.star-box-giga-star')" --extract '//css(".infobar")/a' }.split.map(&:chomp) | |
rating = info.shift | |
genres = info.join(",") | |
[ rating, genres, movie_name, imdb_title_link.chomp ] | |
end | |
# save | |
File.open("/tmp/imdb_results.out", "w") do |f| | |
f.puts(results.inspect) | |
end | |
puts "========================" | |
puts "ORDERED MOVIE BY RATING" | |
puts "========================" | |
puts | |
safe_comparison(results).sort.reverse.map do |rating, genres, movie_name, link| | |
printf "%-3s %-30s %-36s %s\n", rating.chomp, genres, movie_name, link | |
end; 'ok' | |
File.open("/tmp/imdb_results.out", "w") do |f| | |
f.puts(results.inspect) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output:
https://gist.githubusercontent.com/shaiguitar/4891a11d51bb54cf9a3e/raw/a608fa264fbf9420a9eb0a04e0bfdeb9171eb6c8/gistfile1.txt