lib/movie-renamer.rb in movie-renamer-0.0.16 vs lib/movie-renamer.rb in movie-renamer-0.0.17

- old
+ new

@@ -1,10 +1,11 @@ #!/usr/bin/ruby require 'imdb' require 'highline' require 'highline/import' +require 'htmlentities' require 'yaml' $config = '' CONFIGFILE = File.join(File.expand_path(ENV['HOME']), '.movie-renamer') begin @@ -16,18 +17,36 @@ filename: /path/to/your/movies/dir" exit =end end - begin require 'imdb' rescue LoadError $stderr.print "#{File.basename($0)} requires imdb gem to work\nPlease install it with gem install imdb\n" exit end + +if $config['language'] + $language = $config['language'] +else + $language = 'com' +end +# MonkeyPatching is bad.. +module Imdb + + class Search + class << self + def query(query) + open("http://www.imdb.#{$language}/find?q=#{CGI::escape(query)};s=tt") + end + end + end + +end + module MovieRenamer @newpath = 'tmp' # TODO insert default @folderpath = '' @@ -149,22 +168,28 @@ # play movie option? # XXX add part integer check def MovieRenamer::editMovie(filename) movie = MovieRenamer::readMovie(filename) MovieRenamer::printMovieInfo(movie) - ans = askMore "would you like to edit this movie? [ Yes, No, Quit, Info]"# , play] " + ans = askMore "would you like to edit this movie? [ Yes, Skip movie, Quit, Imdb lookup]"# , play] " if ans if ans == :info - MovieRenamer::suggestMovies(movie) + ret = MovieRenamer::suggestMovies(movie) + if ret.class == nil + return true + end elsif ans == :play MovieRenamer::playMovie(movie) end + + #if ask "play movie with mplayer?" # MovieRenamer::playMovie(movie) #end # TODO insert imdb suggestions here? + if movie.year == '' @output.puts "Enter a year" movie.year = @input.gets.chomp.to_i @@ -225,15 +250,15 @@ @output.puts question response = @input.gets.chomp case response when /^y(es)?$/i true - when /^no?$/i + when /^s(kip)?$/i false when /^q(uit)?$/i exit 0 - when /^i(nfo)?$/i + when /^i(mdb)?$/i return :info when /^p(lay)?$/i return :play else puts "I don't understand. Please retry" @@ -274,30 +299,37 @@ input.gsub(/[^A-Za-z0-9\_\-\s']/,'').gsub(/\s+/,' ').chomp.sub(/ +$/,'') end def MovieRenamer::imdbLookup(name) s = Imdb::Search.new(name) + coder = HTMLEntities.new s.movies[0..4].each_with_index do |m,i| + m.title = coder.decode(m.title)#.encode("iso-8859-1") @output.puts "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}" end end # makes a query to imdb database def MovieRenamer::suggestMovies(movie) + coder = HTMLEntities.new s = Imdb::Search.new(movie.title) s.movies[0..4].each_with_index do |m,i| + m.title = coder.decode(m.title)#.encode("iso-8859-1") @output.puts "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}" end - cmd = ask("pick a choice [0..4], Manual search, Edit Manually", %w{0 1 2 3 4 m e}) do |q| - q.readline = true - end - if %w{0 1 2 3 4}.include?(cmd) + mt = s.movies[0..4] + cmd = ask("pick a choice [0..#{(mt.length) -1 }], Manual search, Edit manually, Skip Movie, Quit", ((0...mt.length).to_a.map{ |e| e.to_s} << %w{m e s q}).flatten) + if (0..mt.length).to_a.map{|e| e.to_s}.include?(cmd) m = s.movies[cmd.to_i] - movie.title = m.title.gsub(/ .*/,'').gsub(/\s*\([0-9]+\)/,'').gsub(/\saka\s.*/,'') # aka removes other lang from title + movie.title = m.title.gsub(/ .*/,'').gsub(/\s*\([0-9]+\).*/,'')#.gsub(/\saka\s.*/,'') # aka removes other lang from title movie.year = m.year movie.director = m.director.to_s.gsub(/(\[")|("\])/,'') elsif cmd == "m" movie.title = ask("enter title") MovieRenamer::suggestMovies(movie ) + elsif cmd == "q" + exit(0) + elsif cmd == "s" + return nil end return movie end # TODO output string variable