Skip to content

Instantly share code, notes, and snippets.

@indirect
Created January 27, 2009 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indirect/53142 to your computer and use it in GitHub Desktop.
Save indirect/53142 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Movie date search by filename
require 'rubygems'
require 'open-uri'
require 'cgi'
require 'hpricot'
files = Dir[File.join(ARGV[0], '*')]
unless files.any?
puts "Call this script with the name of a directory full of movie files you want to add years to. For example:"
puts " movie-date-search /folder/with/movies/in/it"
end
trap(:INT) { exit }
files.each do |fname|
title = File.basename(fname, File.extname(fname))
unless title =~ /\(\d{4}\)/
puts "Searching for #{title}"
html = open("http://us.imdb.com/find?q=#{CGI.escape(title)}").read
if html =~ /no matches/i
puts "Couldn't find any matches on IMDB"
else
doc = Hpricot(html)
# this is nth-child(1) instead of nth-child(3) because of a bug in hpricot
# http://github.com/fizx/hpricot/commit/1208019ade1af02816de6217d3c34bd82054b9b7
matches = doc.search("tr td:nth-child(1)").map do |td|
td.inner_html.match(/">(.*?)<\/a> \((\d{4})/).to_a[1..-1]
end.compact
options = matches.map do |m|
CGI.unescapeHTML(m.first.gsub(/<.*?>/, '') + " (#{m.last})")
end
options = [doc.at("//title").inner_html] unless options.any?
options.reverse.each_with_index do |o, i|
puts "#{options.size - i}. #{o}"
end
puts "Which one matches '#{title}'? "
line = STDIN.gets
next if !line || line =~ /^[\s\r\n]*$/
choice = options[line.to_i - 1]
command = %{mv "#{fname}" "#{File.dirname(fname)}/#{choice}#{File.extname(fname)}"}
puts(command)
system(command)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment