Created
January 27, 2009 03:13
-
-
Save indirect/53142 to your computer and use it in GitHub Desktop.
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
#!/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