Module: MovieRenamer
- Defined in:
- lib/movie-renamer.rb
Defined Under Namespace
Classes: Movie
Constant Summary collapse
- MOVIEPATTERN =
%r{\.((avi|AVI)|(mkv|MKV)|(mpg|MPG|mpeg|MPEG))$}
Class Method Summary collapse
-
.askMore(question) ⇒ Object
yes no quit info play questioner.
-
.editMovie(filename) ⇒ Object
edit a movie interactively read the movie print movie info ask movie data rename movie play movie option? XXX add part integer check.
-
.findMovies(folder = @folderpath) ⇒ Object
returns an array of filenames TODO recursive find?.
-
.folderLoop(folder = @folderpath) ⇒ Object
invoke edit movie on a whole folder.
-
.folderPath=(folderpath) ⇒ Object
setters.
- .imdbLookup(name) ⇒ Object
-
.input=(input) ⇒ Object
test helpers.
- .is_a_test=(input) ⇒ Object
-
.newName(movie) ⇒ Object
calculates new movie name based on a pattern? XXX TODO change this and include a globalpattern.
- .newpath=(newpath) ⇒ Object
- .output=(output) ⇒ Object
- .parseMovie(filename) ⇒ Object
-
.playMovie(movie) ⇒ Object
plays the movie with mplayer.
- .printMovieInfo(movie) ⇒ Object
-
.readMovie(filename) ⇒ Object
reads move filename and tries to initialize a movie object? returns the movie object.
-
.renameMovie(movie, newpath = @newpath) ⇒ Object
rename a movie according to movie data and moves it to the new path in filesystem.
-
.sanitizeInput(input) ⇒ Object
LIMITS the set of chars that can be used in movie names just ‘cause we love shell and we know how painful those chars can be :P.
-
.suggestMovie(name) ⇒ Object
TODO output string variable.
-
.suggestMovies(movie) ⇒ Object
makes a query to imdb database.
-
.titleExtract(filename) ⇒ Object
attempt to remove the divx part from a filename.
Class Method Details
.askMore(question) ⇒ Object
yes no quit info play questioner
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/movie-renamer.rb', line 267 def MovieRenamer::askMore(question) @output.puts question response = @input.gets.chomp case response when /^y(es)?$/i true when /^s(kip)?$/i false when /^q(uit)?$/i exit 0 when /^i(mdb)?$/i return :info when /^p(lay)?$/i return :play else puts "I don't understand. Please retry" askMore(question) end end |
.editMovie(filename) ⇒ Object
edit a movie interactively read the movie print movie info ask movie data rename movie play movie option? XXX add part integer check
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/movie-renamer.rb', line 188 def MovieRenamer::editMovie(filename) movie = MovieRenamer::readMovie(filename) MovieRenamer::printMovieInfo(movie) ans = askMore "would you like to edit this movie? [ Yes, Skip movie, Quit, Imdb lookup]"# , play] " if ans if ans == :info 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 @output.puts "Enter a director" movie.director = MovieRenamer::sanitizeInput(@input.gets.chomp) @output.puts "Enter a title" movie.title = MovieRenamer::sanitizeInput(@input.gets.chomp) @output.puts "Enter a part (you can leave this blank)" movie.part = MovieRenamer::sanitizeInput(@input.gets.chomp) end MovieRenamer::printMovieInfo(movie) ans = ask("is this information correct: Yes, No") do |q| q.validate = /^y(es)?|^n(o)?/ end if ans =~/^y(es)?/ return MovieRenamer::renameMovie(movie) #return true else editMovie(filename) end else return true end end |
.findMovies(folder = @folderpath) ⇒ Object
returns an array of filenames TODO recursive find?
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/movie-renamer.rb', line 123 def MovieRenamer::findMovies(folder = @folderpath) ar = Array.new Dir.open(folder) do |dir| dir.each do |file| if file =~ MOVIEPATTERN ar << file end end end return ar.sort end |
.folderLoop(folder = @folderpath) ⇒ Object
invoke edit movie on a whole folder
243 244 245 246 247 |
# File 'lib/movie-renamer.rb', line 243 def MovieRenamer::folderLoop(folder = @folderpath) MovieRenamer::findMovies(folder).each do |file| MovieRenamer::editMovie(file) end end |
.folderPath=(folderpath) ⇒ Object
setters
101 102 103 |
# File 'lib/movie-renamer.rb', line 101 def MovieRenamer::folderPath=(folderpath) @folderpath = folderpath end |
.imdbLookup(name) ⇒ Object
320 321 322 323 324 325 326 327 |
# File 'lib/movie-renamer.rb', line 320 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 |
.input=(input) ⇒ Object
test helpers
110 111 112 |
# File 'lib/movie-renamer.rb', line 110 def MovieRenamer::input=(input) @input = input end |
.is_a_test=(input) ⇒ Object
114 115 116 |
# File 'lib/movie-renamer.rb', line 114 def MovieRenamer::is_a_test=(input) @is_a_test = input end |
.newName(movie) ⇒ Object
calculates new movie name based on a pattern? XXX TODO change this and include a globalpattern
301 302 303 304 305 306 307 308 309 |
# File 'lib/movie-renamer.rb', line 301 def MovieRenamer::newName(movie) @renamepattern.gsub!(/\$[a-z]*/) { |m| ;'#{movie.'+m.sub(/\$/,'').chomp+'}' } s = eval( '"' + @renamepattern + '"') if movie.part =~ /\w/ s+= " - part#{movie.part.to_i}" end s += File.extname(movie.filename) return s end |
.newpath=(newpath) ⇒ Object
105 106 107 |
# File 'lib/movie-renamer.rb', line 105 def MovieRenamer::newpath=(newpath) @newpath = newpath end |
.output=(output) ⇒ Object
117 118 119 |
# File 'lib/movie-renamer.rb', line 117 def MovieRenamer::output=(output) @output = output end |
.parseMovie(filename) ⇒ Object
144 145 146 147 |
# File 'lib/movie-renamer.rb', line 144 def MovieRenamer::parseMovie(filename) Movie.new(filename) end |
.playMovie(movie) ⇒ Object
plays the movie with mplayer
177 178 179 |
# File 'lib/movie-renamer.rb', line 177 def MovieRenamer::playMovie(movie) end |
.printMovieInfo(movie) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/movie-renamer.rb', line 287 def MovieRenamer::printMovieInfo(movie) say("Movie info is: <%= color('old filename:', :red) %> #{movie.filename} <%= color('year:', :red) %> #{movie.year} <%= color('director:', :red) %> #{movie.director} <%= color('title:', :red) %> #{movie.title} <%= color('part:', :red) %> #{movie.part} <%= color('filename:', :red) %> #{MovieRenamer::newName(movie)}") #@output.puts s #return s end |
.readMovie(filename) ⇒ Object
reads move filename and tries to initialize a movie object? returns the movie object
137 138 139 140 141 142 |
# File 'lib/movie-renamer.rb', line 137 def MovieRenamer::readMovie(filename) # TODO insert logic here filename = File.basename(filename) title =MovieRenamer::titleExtract(File.basename(filename,'.*')) return Movie.new(filename,:title => title) end |
.renameMovie(movie, newpath = @newpath) ⇒ Object
rename a movie according to movie data and moves it to the new path in filesystem
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/movie-renamer.rb', line 160 def MovieRenamer::renameMovie(movie,newpath = @newpath) filename = MovieRenamer::newName(movie) path = File.(newpath) unless File.exist?(path) Dir.mkdir(path) end begin require 'fileutils' # remove noop return FileUtils::mv(File.join(@folderpath,movie.filename), File.join(path,filename), :noop => @is_a_test) ? true : false rescue SystemCallError => e puts e end end |
.sanitizeInput(input) ⇒ Object
LIMITS the set of chars that can be used in movie names just ‘cause we love shell and we know how painful those chars can be :P
314 315 316 317 318 |
# File 'lib/movie-renamer.rb', line 314 def MovieRenamer::sanitizeInput(input) # XXX naive sanitize # simply removing all non standard characters input.gsub(/[^A-Za-z0-9\_\-\s']/,'').gsub(/\s+/,' ').chomp.sub(/ +$/,'') end |
.suggestMovie(name) ⇒ Object
TODO output string variable
356 357 358 359 360 |
# File 'lib/movie-renamer.rb', line 356 def MovieRenamer::suggestMovie(name) s = Imdb::Search.new(name) m = s.movies.first @output.puts "#{m.year} - #{m.director} - #{m.title}" end |
.suggestMovies(movie) ⇒ Object
makes a query to imdb database
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/movie-renamer.rb', line 329 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") out = "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}" say(HighLine.new.color(out, :green)) end 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.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 |
.titleExtract(filename) ⇒ Object
attempt to remove the divx part from a filename
149 150 151 152 153 154 155 156 |
# File 'lib/movie-renamer.rb', line 149 def MovieRenamer::titleExtract(filename) r1 = %r{\s*\[?\(?\s*[dD](i|I)(v|V)(x|X)\s?(-|_)?\s?\w+\s*\)?\]?\s*} r2 = %r{\s*\[?\(?\s*(x|X)(v|V)(i|I)(d|D)\s?(-|_)?\s?\w+\s*\)?\]?\s*} r3 = %r{\s*\[?\(?\s*(d|D)(v|V)(d|D)(r|R)(i|I)(p|P)\s?(-|_)?\s*\)?\]?\s*} r = /(#{r1}|#{r2}|#{r3})/ filename.gsub!(/-.*/,'') filename.gsub(r,'').gsub(/\s?(-|_)\s?/,' ').gsub(/^\s/,'') end |