-
-
Save eventualbuddha/53148 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 | |
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) | |
matches = doc.search("//td//td//td").map do |td| | |
td.inner_html.match(/">(.*?)<\/a> \((\d{4})\)/) | |
end.compact | |
options = matches.to_a[0..9].map do |m| | |
CGI.unescapeHTML(m[1].gsub(/<.*?>/, '') + " (#{m[2]})") | |
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