Module: MovieRenamer
- Defined in:
- lib/movie-renamer.rb
Defined Under Namespace
Classes: Movie
Constant Summary collapse
- NEWPATH =
'tmp'
- RENAMEPATTERN =
''
- MOVIEPATTERN =
%r{\.((avi|AVI)|(mkv|MKV)|(mpg|MPG|mpeg|MPEG))$}
Class Method Summary collapse
-
.ask(question) ⇒ Object
yes or no questioner.
-
.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.
-
.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.
- .output=(output) ⇒ 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(name) ⇒ Object
makes a query to imdb database.
-
.titleExtract(filename) ⇒ Object
attempt to remove the divx part from a filename.
Class Method Details
.ask(question) ⇒ Object
yes or no questioner
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/movie-renamer.rb', line 174 def MovieRenamer::ask(question) @output.puts question response = @input.gets.chomp case response when /^y(es)?$/i true when /^no?$/i false else puts "I don't understand. Please retry" ask(question) end end |
.askMore(question) ⇒ Object
yes no quit info play questioner
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/movie-renamer.rb', line 189 def MovieRenamer::askMore(question) @output.puts question response = @input.gets.chomp case response when /^y(es)?$/i true when /^no?$/i false when /^q(uit)?$/i exit 0 when /^i(nfo)?$/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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/movie-renamer.rb', line 122 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] " if ans if ans == 'info' MovieRenamer::suggestMovies(movie.title) elsif ans == 'play' MovieRenamer::playMovie(movie) end #if ask "play movie with mplayer?" # MovieRenamer::playMovie(movie) #end # TODO insert imdb suggestions here? @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) MovieRenamer::printMovieInfo(movie) if ask "is this information correct" 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?
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/movie-renamer.rb', line 62 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
167 168 169 170 171 |
# File 'lib/movie-renamer.rb', line 167 def MovieRenamer::folderLoop(folder = @folderpath) MovieRenamer::findMovies(folder).each do |file| MovieRenamer::editMovie(file) end end |
.folderPath=(folderpath) ⇒ Object
setters
43 44 45 |
# File 'lib/movie-renamer.rb', line 43 def MovieRenamer::folderPath=(folderpath) @folderpath = folderpath end |
.input=(input) ⇒ Object
test helpers
49 50 51 |
# File 'lib/movie-renamer.rb', line 49 def MovieRenamer::input=(input) @input = input end |
.is_a_test=(input) ⇒ Object
53 54 55 |
# File 'lib/movie-renamer.rb', line 53 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
223 224 225 226 227 228 229 230 |
# File 'lib/movie-renamer.rb', line 223 def MovieRenamer::newName(movie) s = "#{movie.year} - #{movie.director} - #{movie.title}" if movie.part =~ /\w/ s+= " - part#{movie.part.to_i}" end s += File.extname(movie.filename) return s end |
.output=(output) ⇒ Object
56 57 58 |
# File 'lib/movie-renamer.rb', line 56 def MovieRenamer::output=(output) @output = output end |
.playMovie(movie) ⇒ Object
plays the movie with mplayer
111 112 113 |
# File 'lib/movie-renamer.rb', line 111 def MovieRenamer::playMovie(movie) end |
.printMovieInfo(movie) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/movie-renamer.rb', line 209 def MovieRenamer::printMovieInfo(movie) s = "Movie info is:\n" s+= "@oldfilename: #{movie.filename}\n" s+= "@year: #{movie.year}\n" s+= "@director: #{movie.director}\n" s+= "@title: #{movie.title}\n" s+= "@part: #{movie.part}\n" s+= "New filename = #{MovieRenamer::newName(movie)}\n" @output.puts s return s end |
.readMovie(filename) ⇒ Object
reads move filename and tries to initialize a movie object? returns the movie object
76 77 78 79 80 81 |
# File 'lib/movie-renamer.rb', line 76 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/movie-renamer.rb', line 94 def MovieRenamer::renameMovie(movie,newpath = NEWPATH) filename = MovieRenamer::newName(movie) path = File.join(@folderpath,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
235 236 237 238 239 |
# File 'lib/movie-renamer.rb', line 235 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
250 251 252 253 254 |
# File 'lib/movie-renamer.rb', line 250 def MovieRenamer::suggestMovie(name) s = Imdb::Search.new(name) m = s.movies.first @output.puts "#{m.year} - #{m.director} - #{m.title}" end |
.suggestMovies(name) ⇒ Object
makes a query to imdb database
242 243 244 245 246 247 |
# File 'lib/movie-renamer.rb', line 242 def MovieRenamer::suggestMovies(name) s = Imdb::Search.new(name) s.movies[0..5].each do |m| @output.puts "#{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}" end end |
.titleExtract(filename) ⇒ Object
attempt to remove the divx part from a filename
84 85 86 87 88 89 90 |
# File 'lib/movie-renamer.rb', line 84 def MovieRenamer::titleExtract(filename) r1 = %r{\s*\[?\(?\s*(d|D)(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(r,'').gsub(/\s?(-|_)\s?/,' ').gsub(/^\s/,'') end |