Fork Of

Revisions

gist: 53148 Download_button fork
public
Public Clone URL: git://gist.github.com/53148.git
Embed All Files: show embed
snippet.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/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