movie-renamer 0.0.22 → 0.0.23
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/movie-renamer +2 -2
- data/lib/movie-renamer.rb +10 -366
- data/lib/movie-renamer/imdb.rb +20 -0
- data/lib/movie-renamer/misc.rb +244 -0
- data/lib/movie-renamer/movie.rb +27 -0
- data/lib/movie-renamer/parse.rb +58 -0
- data/test/test_movie-renamer.rb +1 -57
- metadata +7 -9
data/bin/movie-renamer
CHANGED
data/lib/movie-renamer.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
3
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
2
4
|
|
3
5
|
require 'imdb'
|
4
6
|
require 'highline'
|
@@ -6,17 +8,14 @@ require 'highline/import'
|
|
6
8
|
require 'htmlentities'
|
7
9
|
require 'yaml'
|
8
10
|
|
11
|
+
|
12
|
+
VERSION = File.open(File.join(File.dirname(__FILE__), '..', 'VERSION'), 'r') { |f| f.read.strip }
|
13
|
+
|
9
14
|
$config = ''
|
10
15
|
CONFIGFILE = File.join(File.expand_path(ENV['HOME']), '.movie-renamer')
|
11
16
|
begin
|
12
17
|
$config = YAML.load_file(CONFIGFILE)
|
13
18
|
rescue
|
14
|
-
=begin
|
15
|
-
raise "\nplease create a .movie-renamer file in your $HOME
|
16
|
-
example:
|
17
|
-
filename: /path/to/your/movies/dir"
|
18
|
-
exit
|
19
|
-
=end
|
20
19
|
end
|
21
20
|
|
22
21
|
begin
|
@@ -32,363 +31,8 @@ if $config['language']
|
|
32
31
|
else
|
33
32
|
$language = 'com'
|
34
33
|
end
|
35
|
-
# MonkeyPatching is bad..
|
36
|
-
module Imdb
|
37
|
-
|
38
|
-
class Search
|
39
|
-
class << self
|
40
|
-
def query(query)
|
41
|
-
open("http://www.imdb.#{$language}/find?q=#{CGI::escape(query)};s=tt")
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class Movie
|
47
|
-
def director
|
48
|
-
document.at("h4[text()='Director:'] ~ a").innerHTML.strip.imdb_unescape_html rescue nil
|
49
|
-
document.search("h4[text()^='Director'] ~ a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
module MovieRenamer
|
56
|
-
|
57
|
-
@newpath = 'tmp'
|
58
|
-
# TODO insert default
|
59
|
-
@folderpath = ''
|
60
|
-
@is_a_test = false
|
61
|
-
@renamepattern = '$year - $director - $title'
|
62
|
-
MOVIEPATTERN = %r{\.((avi|AVI)|(mkv|MKV)|(mpg|MPG|mpeg|MPEG))$}
|
63
|
-
@input = STDIN
|
64
|
-
@output = STDOUT
|
65
|
-
|
66
|
-
if $config['filename']
|
67
|
-
@renamepattern = $config['filename']
|
68
|
-
end
|
69
|
-
|
70
|
-
if $config['parsepattern']
|
71
|
-
@parsepattern = $config['parsepattern']
|
72
|
-
end
|
73
|
-
|
74
|
-
if $config['savepath']
|
75
|
-
@newpath = File.expand_path($config['savepath'])
|
76
|
-
end
|
77
|
-
|
78
|
-
puts "Renamed movies will be saved in #{@newpath}"
|
79
|
-
|
80
|
-
class Movie
|
81
|
-
|
82
|
-
def initialize(filename,opts={})
|
83
|
-
opts = { :year => '', :director => '', :title => '', :part => '', :imdb =>''}.merge(opts)
|
84
|
-
@year = opts[:year]
|
85
|
-
@director = opts[:director]
|
86
|
-
@title = opts[:title]
|
87
|
-
@part = opts[:part]
|
88
|
-
@imdb = opts[:imdb]
|
89
|
-
@filename = filename
|
90
|
-
end
|
91
|
-
|
92
|
-
attr_accessor :year, :director, :title, :part, :imdb, :filename
|
93
|
-
|
94
|
-
def == (movie)
|
95
|
-
if @year == movie.year and @director == movie.director and @title == movie.title and @part == movie.part and @imdb == movie.imdb and @filename == movie.filename
|
96
|
-
return true
|
97
|
-
else
|
98
|
-
return false
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
# setters
|
105
|
-
def MovieRenamer::folderPath=(folderpath)
|
106
|
-
@folderpath = folderpath
|
107
|
-
end
|
108
|
-
|
109
|
-
def MovieRenamer::newpath=(newpath)
|
110
|
-
@newpath = newpath
|
111
|
-
end
|
112
|
-
|
113
|
-
#test helpers
|
114
|
-
def MovieRenamer::input=(input)
|
115
|
-
@input = input
|
116
|
-
end
|
117
|
-
|
118
|
-
def MovieRenamer::is_a_test=(input)
|
119
|
-
@is_a_test = input
|
120
|
-
end
|
121
|
-
def MovieRenamer::output=(output)
|
122
|
-
@output = output
|
123
|
-
end
|
124
|
-
|
125
|
-
# returns an array of filenames
|
126
|
-
# TODO recursive find?
|
127
|
-
def MovieRenamer::findMovies(folder = @folderpath)
|
128
|
-
ar = Array.new
|
129
|
-
Dir.open(folder) do |dir|
|
130
|
-
dir.each do |file|
|
131
|
-
if file =~ MOVIEPATTERN
|
132
|
-
ar << file
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
return ar.sort
|
137
|
-
end
|
138
|
-
|
139
|
-
# reads move filename and tries to initialize a movie object?
|
140
|
-
# returns the movie object
|
141
|
-
def MovieRenamer::readMovie(filename)
|
142
|
-
# TODO insert logic here
|
143
|
-
if @parsepattern
|
144
|
-
MovieRenamer::parseMovie(filename)
|
145
|
-
else
|
146
|
-
filename = File.basename(filename)
|
147
|
-
title =MovieRenamer::titleExtract(File.basename(filename,'.*'))
|
148
|
-
return Movie.new(filename,:title => title)
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
def MovieRenamer::parseMovie(filename)
|
153
|
-
filename.gsub!(/(\..+$)/,'')
|
154
|
-
ext = $1
|
155
|
-
#@parsepattern = "$title - $director"
|
156
|
-
p = []
|
157
|
-
p << [:year= , @parsepattern =~ /\$year/] if @parsepattern =~ /\$year/
|
158
|
-
p << [:title=, @parsepattern =~ /\$title/] if @parsepattern =~ /\$title/
|
159
|
-
p << [:director=, @parsepattern =~ /\$director/] if @parsepattern =~ /\$director/
|
160
|
-
p.compact!
|
161
|
-
p.sort! { |a, b| a[1]<=> b[1] }
|
162
|
-
newpattern = @parsepattern.gsub(/\$[a-z]+/,'(.+)')
|
163
|
-
#puts %r{#{newpattern}}.inspect
|
164
|
-
m = Movie.new(filename+ ext) #TODO add extensioooonn that was lost
|
165
|
-
p.each_with_index do |e,i|
|
166
|
-
filename =~ %r{#{newpattern}}
|
167
|
-
m.send e[0], eval("$" +(i+1).to_s)
|
168
|
-
end
|
169
|
-
#m.title = $year
|
170
|
-
#m.director = $director
|
171
|
-
#m.year = $title
|
172
|
-
if m.title == nil
|
173
|
-
m.title = MovieRenamer::titleExtract(filename)
|
174
|
-
return m
|
175
|
-
end
|
176
|
-
# attempt to remove the divx part from a filename
|
177
|
-
def MovieRenamer::titleExtract(filename)
|
178
|
-
r1 = %r{\s*\[?\(?\s*[dD](i|I)(v|V)(x|X)\s?(-|_)?\s?\w+\s*\)?\]?\s*}
|
179
|
-
r2 = %r{\s*\[?\(?\s*(x|X)(v|V)(i|I)(d|D)\s?(-|_)?\s?\w+\s*\)?\]?\s*}
|
180
|
-
r3 = %r{\s*\[?\(?\s*(d|D)(v|V)(d|D)(r|R)(i|I)(p|P)\s?(-|_)?\s*\)?\]?\s*}
|
181
|
-
r = /(#{r1}|#{r2}|#{r3})/
|
182
|
-
filename.gsub!(/-.*/,'') # XXX takes only first part
|
183
|
-
filename.gsub(r,'').gsub(/\s?(-|_)\s?/,' ').gsub(/^\s/,'')
|
184
|
-
end
|
185
|
-
|
186
|
-
# rename a movie according to movie data
|
187
|
-
# and moves it to the new path in filesystem
|
188
|
-
def MovieRenamer::renameMovie(movie,newpath = @newpath)
|
189
|
-
filename = MovieRenamer::newName(movie)
|
190
|
-
path = File.expand_path(newpath)
|
191
|
-
unless File.exist?(path)
|
192
|
-
Dir.mkdir(path)
|
193
|
-
end
|
194
|
-
|
195
|
-
begin
|
196
|
-
require 'fileutils'
|
197
|
-
# remove noop
|
198
|
-
return FileUtils::mv(File.join(@folderpath,movie.filename), File.join(path,filename), :noop => @is_a_test) ? true : false
|
199
|
-
rescue SystemCallError => e
|
200
|
-
puts e
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
|
-
# plays the movie with mplayer
|
205
|
-
def MovieRenamer::playMovie(movie)
|
206
|
-
|
207
|
-
end
|
208
|
-
|
209
|
-
# edit a movie interactively
|
210
|
-
# read the movie
|
211
|
-
# print movie info
|
212
|
-
# ask movie data
|
213
|
-
# rename movie
|
214
|
-
# play movie option?
|
215
|
-
# XXX add part integer check
|
216
|
-
def MovieRenamer::editMovie(filename)
|
217
|
-
movie = MovieRenamer::readMovie(filename)
|
218
|
-
MovieRenamer::printMovieInfo(movie)
|
219
|
-
ans = askMore "would you like to edit this movie? [ Yes, Skip movie, Quit, Imdb lookup]"# , play] "
|
220
|
-
if ans
|
221
|
-
if ans == :info
|
222
|
-
ret = MovieRenamer::suggestMovies(movie)
|
223
|
-
if ret.class == nil
|
224
|
-
return true
|
225
|
-
end
|
226
|
-
elsif ans == :play
|
227
|
-
MovieRenamer::playMovie(movie)
|
228
|
-
end
|
229
|
-
|
230
|
-
|
231
|
-
#if ask "play movie with mplayer?"
|
232
|
-
# MovieRenamer::playMovie(movie)
|
233
|
-
#end
|
234
|
-
|
235
|
-
# TODO insert imdb suggestions here?
|
236
|
-
|
237
|
-
|
238
|
-
if movie.year == ''
|
239
|
-
@output.puts "Enter a year"
|
240
|
-
movie.year = @input.gets.chomp.to_i
|
241
|
-
|
242
|
-
@output.puts "Enter a director"
|
243
|
-
movie.director = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
244
|
-
|
245
|
-
@output.puts "Enter a title"
|
246
|
-
movie.title = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
247
|
-
|
248
|
-
@output.puts "Enter a part (you can leave this blank)"
|
249
|
-
movie.part = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
250
|
-
end
|
251
|
-
|
252
|
-
MovieRenamer::printMovieInfo(movie)
|
253
|
-
|
254
|
-
ans = ask("is this information correct: Yes, No") do |q|
|
255
|
-
q.validate = /^y(es)?|^n(o)?/
|
256
|
-
end
|
257
|
-
if ans =~/^y(es)?/
|
258
|
-
return MovieRenamer::renameMovie(movie)
|
259
|
-
#return true
|
260
|
-
else
|
261
|
-
editMovie(filename)
|
262
|
-
end
|
263
|
-
else
|
264
|
-
return true
|
265
|
-
end
|
266
|
-
|
267
|
-
end
|
268
|
-
|
269
|
-
|
270
|
-
# invoke edit movie on a whole folder
|
271
|
-
def MovieRenamer::folderLoop(folder = @folderpath)
|
272
|
-
MovieRenamer::findMovies(folder).each do |file|
|
273
|
-
MovieRenamer::editMovie(file)
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
=begin
|
278
|
-
# yes or no questioner
|
279
|
-
def MovieRenamer::ask(question)
|
280
|
-
@output.puts question
|
281
|
-
response = @input.gets.chomp
|
282
|
-
case response
|
283
|
-
when /^y(es)?$/i
|
284
|
-
true
|
285
|
-
when /^no?$/i
|
286
|
-
false
|
287
|
-
else
|
288
|
-
puts "I don't understand. Please retry"
|
289
|
-
MovieRenamer::ask(question)
|
290
|
-
end
|
291
|
-
end
|
292
|
-
=end
|
293
|
-
|
294
|
-
# yes no quit info play questioner
|
295
|
-
def MovieRenamer::askMore(question)
|
296
|
-
@output.puts question
|
297
|
-
response = @input.gets.chomp
|
298
|
-
case response
|
299
|
-
when /^y(es)?$/i
|
300
|
-
true
|
301
|
-
when /^s(kip)?$/i
|
302
|
-
false
|
303
|
-
when /^q(uit)?$/i
|
304
|
-
exit 0
|
305
|
-
when /^i(mdb)?$/i
|
306
|
-
return :info
|
307
|
-
when /^p(lay)?$/i
|
308
|
-
return :play
|
309
|
-
else
|
310
|
-
puts "I don't understand. Please retry"
|
311
|
-
askMore(question)
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
def MovieRenamer::printMovieInfo(movie)
|
316
|
-
say("Movie info is:
|
317
|
-
<%= color('old filename:', :red) %> #{movie.filename}
|
318
|
-
<%= color('year:', :red) %> #{movie.year}
|
319
|
-
<%= color('director:', :red) %> #{movie.director}
|
320
|
-
<%= color('title:', :red) %> #{movie.title}
|
321
|
-
<%= color('part:', :red) %> #{movie.part}
|
322
|
-
<%= color('filename:', :red) %> #{MovieRenamer::newName(movie)}")
|
323
|
-
#@output.puts s
|
324
|
-
#return s
|
325
|
-
end
|
326
|
-
|
327
|
-
# calculates new movie name based on a pattern? XXX
|
328
|
-
# TODO change this and include a globalpattern
|
329
|
-
def MovieRenamer::newName(movie)
|
330
|
-
@renamepattern.gsub!(/\$[a-z]*/) { |m| ;'#{movie.'+m.sub(/\$/,'').chomp+'}' }
|
331
|
-
s = eval( '"' + @renamepattern + '"')
|
332
|
-
if movie.part =~ /\w/
|
333
|
-
s+= " - part#{movie.part.to_i}"
|
334
|
-
end
|
335
|
-
s += File.extname(movie.filename)
|
336
|
-
return s
|
337
|
-
end
|
338
|
-
|
339
|
-
|
340
|
-
# LIMITS the set of chars that can be used in movie names
|
341
|
-
# just 'cause we love shell and we know how painful those chars can be :P
|
342
|
-
def MovieRenamer::sanitizeInput(input)
|
343
|
-
# XXX naive sanitize
|
344
|
-
# simply removing all non standard characters
|
345
|
-
input.gsub(/[^A-Za-z0-9\_\-\s']/,'').gsub(/\s+/,' ').chomp.sub(/ +$/,'')
|
346
|
-
end
|
347
|
-
|
348
|
-
def MovieRenamer::imdbLookup(name)
|
349
|
-
s = Imdb::Search.new(name)
|
350
|
-
coder = HTMLEntities.new
|
351
|
-
s.movies[0..4].each_with_index do |m,i|
|
352
|
-
m.title = coder.decode(m.title)#.encode("iso-8859-1")
|
353
|
-
@output.puts "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}"
|
354
|
-
end
|
355
|
-
end
|
356
|
-
# makes a query to imdb database
|
357
|
-
def MovieRenamer::suggestMovies(movie)
|
358
|
-
coder = HTMLEntities.new
|
359
|
-
name = (movie.title + " ").gsub(/\W/,' ').gsub(/(^\w{,3})|( \w{,3} )/,'').gsub(/\s+/,' ').chomp
|
360
|
-
s = Imdb::Search.new(name)
|
361
|
-
s.movies[0..4].each_with_index do |m,i|
|
362
|
-
m.title = coder.decode(m.title)#.encode("iso-8859-1")
|
363
|
-
out = "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}"
|
364
|
-
say(HighLine.new.color(out, :green))
|
365
|
-
end
|
366
|
-
mt = s.movies[0..4]
|
367
|
-
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)
|
368
|
-
if (0..mt.length).to_a.map{|e| e.to_s}.include?(cmd)
|
369
|
-
m = s.movies[cmd.to_i]
|
370
|
-
movie.title = m.title.gsub(/ .*/,'').gsub(/\s*\([0-9]+\).*/,'')#.gsub(/\saka\s.*/,'') # aka removes other lang from title
|
371
|
-
movie.year = m.year
|
372
|
-
movie.director = m.director.to_s.gsub(/(\[")|("\])/,'')
|
373
|
-
elsif cmd == "m"
|
374
|
-
movie.title = ask("enter title")
|
375
|
-
MovieRenamer::suggestMovies(movie )
|
376
|
-
elsif cmd == "q"
|
377
|
-
exit(0)
|
378
|
-
elsif cmd == "s"
|
379
|
-
return nil
|
380
|
-
end
|
381
|
-
return movie
|
382
|
-
end
|
383
|
-
|
384
|
-
# TODO output string variable
|
385
|
-
def MovieRenamer::suggestMovie(name)
|
386
|
-
s = Imdb::Search.new(name)
|
387
|
-
m = s.movies.first
|
388
|
-
@output.puts "#{m.year} - #{m.director} - #{m.title}"
|
389
|
-
end
|
390
|
-
|
391
|
-
# returns the first movie from imdb query
|
392
|
-
|
393
|
-
end
|
394
34
|
|
35
|
+
require "movie-renamer/misc"
|
36
|
+
require "movie-renamer/movie"
|
37
|
+
require "movie-renamer/imdb"
|
38
|
+
require "movie-renamer/parse"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# imdb monkeypatch
|
2
|
+
module Imdb
|
3
|
+
|
4
|
+
class Search
|
5
|
+
class << self
|
6
|
+
def query(query)
|
7
|
+
open("http://www.imdb.#{$language}/find?q=#{CGI::escape(query)};s=tt")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Movie
|
13
|
+
#TODO improve this 'cause imdb site has changed
|
14
|
+
def director
|
15
|
+
document.at("h4[text()='Director:'] ~ a").innerHTML.strip.imdb_unescape_html rescue nil
|
16
|
+
document.search("h4[text()^='Director'] ~ a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,244 @@
|
|
1
|
+
|
2
|
+
module MovieRenamer
|
3
|
+
|
4
|
+
@newpath = 'tmp'
|
5
|
+
# TODO insert default
|
6
|
+
@folderpath = ''
|
7
|
+
@is_a_test = false
|
8
|
+
@renamepattern = '$year - $director - $title'
|
9
|
+
MOVIEPATTERN = %r{\.((avi|AVI)|(mkv|MKV)|(mpg|MPG|mpeg|MPEG))$}
|
10
|
+
@input = STDIN
|
11
|
+
@output = STDOUT
|
12
|
+
|
13
|
+
if $config['filename']
|
14
|
+
@renamepattern = $config['filename']
|
15
|
+
end
|
16
|
+
|
17
|
+
if $config['parsepattern']
|
18
|
+
@parsepattern = $config['parsepattern']
|
19
|
+
end
|
20
|
+
|
21
|
+
if $config['savepath']
|
22
|
+
@newpath = File.expand_path($config['savepath'])
|
23
|
+
end
|
24
|
+
|
25
|
+
puts "Renamed movies will be saved in #{@newpath}/"
|
26
|
+
|
27
|
+
|
28
|
+
# setters
|
29
|
+
def MovieRenamer::folderPath=(folderpath)
|
30
|
+
@folderpath = folderpath
|
31
|
+
end
|
32
|
+
|
33
|
+
def MovieRenamer::newpath=(newpath)
|
34
|
+
@newpath = newpath
|
35
|
+
end
|
36
|
+
|
37
|
+
#test helpers
|
38
|
+
def MovieRenamer::input=(input)
|
39
|
+
@input = input
|
40
|
+
end
|
41
|
+
|
42
|
+
def MovieRenamer::is_a_test=(input)
|
43
|
+
@is_a_test = input
|
44
|
+
end
|
45
|
+
|
46
|
+
def MovieRenamer::output=(output)
|
47
|
+
@output = output
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# rename a movie according to movie data
|
52
|
+
# and moves it to the new path in filesystem
|
53
|
+
def MovieRenamer::renameMovie(movie,newpath = @newpath)
|
54
|
+
filename = MovieRenamer::newName(movie)
|
55
|
+
path = File.expand_path(newpath)
|
56
|
+
unless File.exist?(path)
|
57
|
+
Dir.mkdir(path)
|
58
|
+
end
|
59
|
+
|
60
|
+
begin
|
61
|
+
require 'fileutils'
|
62
|
+
# remove noop
|
63
|
+
return FileUtils::mv(File.join(@folderpath,movie.filename), File.join(path,filename), :noop => @is_a_test) ? true : false
|
64
|
+
rescue SystemCallError => e
|
65
|
+
puts e
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# plays the movie with mplayer
|
70
|
+
def MovieRenamer::playMovie(movie)
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
# edit a movie interactively
|
75
|
+
# read the movie
|
76
|
+
# print movie info
|
77
|
+
# ask movie data
|
78
|
+
# rename movie
|
79
|
+
# play movie option?
|
80
|
+
# XXX add part integer check
|
81
|
+
def MovieRenamer::editMovie(filename)
|
82
|
+
movie = MovieRenamer::readMovie(filename)
|
83
|
+
MovieRenamer::printMovieInfo(movie)
|
84
|
+
ans = askMore "would you like to edit this movie? [ Yes, Skip movie, Quit, Imdb lookup]"# , play] "
|
85
|
+
if ans
|
86
|
+
if ans == :info
|
87
|
+
ret = MovieRenamer::suggestMovies(movie)
|
88
|
+
if ret.class == nil
|
89
|
+
return true
|
90
|
+
end
|
91
|
+
elsif ans == :play
|
92
|
+
MovieRenamer::playMovie(movie)
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
#if ask "play movie with mplayer?"
|
97
|
+
# MovieRenamer::playMovie(movie)
|
98
|
+
#end
|
99
|
+
|
100
|
+
# TODO insert imdb suggestions here?
|
101
|
+
|
102
|
+
|
103
|
+
if movie.year == ''
|
104
|
+
@output.puts "Enter a year"
|
105
|
+
movie.year = @input.gets.chomp.to_i
|
106
|
+
|
107
|
+
@output.puts "Enter a director"
|
108
|
+
movie.director = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
109
|
+
|
110
|
+
@output.puts "Enter a title"
|
111
|
+
movie.title = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
112
|
+
|
113
|
+
@output.puts "Enter a part (you can leave this blank)"
|
114
|
+
movie.part = MovieRenamer::sanitizeInput(@input.gets.chomp)
|
115
|
+
end
|
116
|
+
|
117
|
+
MovieRenamer::printMovieInfo(movie)
|
118
|
+
|
119
|
+
ans = ask("is this information correct: Yes, No") do |q|
|
120
|
+
q.validate = /^y(es)?|^n(o)?/
|
121
|
+
end
|
122
|
+
if ans =~/^y(es)?/
|
123
|
+
return MovieRenamer::renameMovie(movie)
|
124
|
+
#return true
|
125
|
+
else
|
126
|
+
editMovie(filename)
|
127
|
+
end
|
128
|
+
else
|
129
|
+
return true
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
# invoke edit movie on a whole folder
|
136
|
+
def MovieRenamer::folderLoop(folder = @folderpath)
|
137
|
+
MovieRenamer::findMovies(folder).each do |file|
|
138
|
+
MovieRenamer::editMovie(file)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
# yes no quit info play questioner
|
145
|
+
def MovieRenamer::askMore(question)
|
146
|
+
@output.puts question
|
147
|
+
response = @input.gets.chomp
|
148
|
+
case response
|
149
|
+
when /^y(es)?$/i
|
150
|
+
true
|
151
|
+
when /^s(kip)?$/i
|
152
|
+
false
|
153
|
+
when /^q(uit)?$/i
|
154
|
+
exit 0
|
155
|
+
when /^i(mdb)?$/i
|
156
|
+
return :info
|
157
|
+
when /^p(lay)?$/i
|
158
|
+
return :play
|
159
|
+
else
|
160
|
+
puts "I don't understand. Please retry"
|
161
|
+
askMore(question)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def MovieRenamer::printMovieInfo(movie)
|
166
|
+
say("Movie info is:
|
167
|
+
<%= color('old filename:', :red) %> #{movie.filename}
|
168
|
+
<%= color('year:', :red) %> #{movie.year}
|
169
|
+
<%= color('director:', :red) %> #{movie.director}
|
170
|
+
<%= color('title:', :red) %> #{movie.title}
|
171
|
+
<%= color('part:', :red) %> #{movie.part}
|
172
|
+
<%= color('filename:', :red) %> #{MovieRenamer::newName(movie)}")
|
173
|
+
end
|
174
|
+
|
175
|
+
# calculates new movie name based on a pattern? XXX
|
176
|
+
# TODO change this and include a globalpattern
|
177
|
+
def MovieRenamer::newName(movie)
|
178
|
+
@renamepattern.gsub!(/\$[a-z]*/) { |m| ;'#{movie.'+m.sub(/\$/,'').chomp+'}' }
|
179
|
+
s = eval( '"' + @renamepattern + '"')
|
180
|
+
if movie.part =~ /\w/
|
181
|
+
s+= " - part#{movie.part.to_i}"
|
182
|
+
end
|
183
|
+
s += File.extname(movie.filename)
|
184
|
+
return s
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
# LIMITS the set of chars that can be used in movie names
|
189
|
+
# just 'cause we love shell and we know how painful those chars can be :P
|
190
|
+
def MovieRenamer::sanitizeInput(input)
|
191
|
+
# XXX naive sanitize
|
192
|
+
# simply removing all non standard characters
|
193
|
+
input.gsub(/[^A-Za-z0-9\_\-\s']/,'').gsub(/\s+/,' ').chomp.sub(/ +$/,'')
|
194
|
+
end
|
195
|
+
|
196
|
+
def MovieRenamer::imdbLookup(name)
|
197
|
+
s = Imdb::Search.new(name)
|
198
|
+
coder = HTMLEntities.new
|
199
|
+
s.movies[0..4].each_with_index do |m,i|
|
200
|
+
m.title = coder.decode(m.title)#.encode("iso-8859-1")
|
201
|
+
say "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}"
|
202
|
+
end
|
203
|
+
end
|
204
|
+
# makes a query to imdb database
|
205
|
+
def MovieRenamer::suggestMovies(movie)
|
206
|
+
coder = HTMLEntities.new
|
207
|
+
name = (movie.title + " ").gsub(/\W/,' ')#.gsub(/(^\w{,3})|( \w{,3} )/,'').gsub(/\s+/,' ').chomp
|
208
|
+
s = Imdb::Search.new(name)
|
209
|
+
s.movies[0..4].each_with_index do |m,i|
|
210
|
+
m.title = coder.decode(m.title)#.encode("iso-8859-1")
|
211
|
+
out = "#{i}, #{m.year} - #{m.director.to_s.gsub(/(\[")|("\])/,'')} - #{m.title.gsub(/ .*/,'')}"
|
212
|
+
say(HighLine.new.color(out, :green))
|
213
|
+
end
|
214
|
+
mt = s.movies[0..4]
|
215
|
+
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)
|
216
|
+
if (0..mt.length).to_a.map{|e| e.to_s}.include?(cmd)
|
217
|
+
m = s.movies[cmd.to_i]
|
218
|
+
movie.title = m.title.gsub(/ .*/,'').gsub(/\s*\([0-9]+\).*/,'')#.gsub(/\saka\s.*/,'') # aka removes other lang from title
|
219
|
+
movie.year = m.year
|
220
|
+
movie.director = m.director.to_s.gsub(/(\[")|("\])/,'')
|
221
|
+
elsif cmd == "m"
|
222
|
+
movie.title = ask("enter title")
|
223
|
+
MovieRenamer::suggestMovies(movie)
|
224
|
+
elsif cmd == "q"
|
225
|
+
exit(0)
|
226
|
+
elsif cmd == "s"
|
227
|
+
return nil
|
228
|
+
end
|
229
|
+
return movie
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
# TODO output string variable
|
234
|
+
def MovieRenamer::suggestMovie(name)
|
235
|
+
s = Imdb::Search.new(name)
|
236
|
+
m = s.movies.first
|
237
|
+
@output.puts "#{m.year} - #{m.director} - #{m.title}"
|
238
|
+
end
|
239
|
+
|
240
|
+
# returns the first movie from imdb query
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MovieRenamer
|
2
|
+
|
3
|
+
class Movie
|
4
|
+
|
5
|
+
def initialize(filename,opts={})
|
6
|
+
opts = { :year => '', :director => '', :title => '', :part => '', :imdb =>''}.merge(opts)
|
7
|
+
@year = opts[:year]
|
8
|
+
@director = opts[:director]
|
9
|
+
@title = opts[:title]
|
10
|
+
@part = opts[:part]
|
11
|
+
@imdb = opts[:imdb]
|
12
|
+
@filename = filename
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_accessor :year, :director, :title, :part, :imdb, :filename
|
16
|
+
|
17
|
+
def == (movie)
|
18
|
+
if @year == movie.year and @director == movie.director and @title == movie.title and @part == movie.part and @imdb == movie.imdb and @filename == movie.filename
|
19
|
+
return true
|
20
|
+
else
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module MovieRenamer
|
2
|
+
# returns an array of filenames
|
3
|
+
# TODO recursive find?
|
4
|
+
def MovieRenamer::findMovies(folder = @folderpath)
|
5
|
+
ar = Array.new
|
6
|
+
Dir.open(folder) do |dir|
|
7
|
+
dir.each do |file|
|
8
|
+
if file =~ MOVIEPATTERN
|
9
|
+
ar << file
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
return ar.sort
|
14
|
+
end
|
15
|
+
|
16
|
+
# reads move filename and tries to initialize a movie object
|
17
|
+
def MovieRenamer::readMovie(filename)
|
18
|
+
if @parsepattern
|
19
|
+
return MovieRenamer::parseMovie(filename)
|
20
|
+
else
|
21
|
+
filename = File.basename(filename)
|
22
|
+
title = MovieRenamer::titleExtract(File.basename(filename,'.*'))
|
23
|
+
return Movie.new(filename,:title => title)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def MovieRenamer::parseMovie(filename)
|
28
|
+
filename.gsub!(/(\..+$)/,'')
|
29
|
+
ext = $1
|
30
|
+
p = []
|
31
|
+
p << [:year= , @parsepattern =~ /\$year/] if @parsepattern =~ /\$year/
|
32
|
+
p << [:title=, @parsepattern =~ /\$title/] if @parsepattern =~ /\$title/
|
33
|
+
p << [:director=, @parsepattern =~ /\$director/] if @parsepattern =~ /\$director/
|
34
|
+
p.compact!
|
35
|
+
p.sort! { |a, b| a[1]<=> b[1] }
|
36
|
+
newpattern = @parsepattern.gsub(/\$[a-z]+/,'(.+)')
|
37
|
+
m = Movie.new(filename+ ext)
|
38
|
+
p.each_with_index do |e,i|
|
39
|
+
filename =~ %r{#{newpattern}}
|
40
|
+
m.send e[0], eval("$" +(i+1).to_s)
|
41
|
+
end
|
42
|
+
if m.title == nil #fallback on titleExtract
|
43
|
+
m.title = MovieRenamer::titleExtract(filename)
|
44
|
+
end
|
45
|
+
return m
|
46
|
+
end
|
47
|
+
|
48
|
+
# attempt to remove the divx part from a filename
|
49
|
+
def MovieRenamer::titleExtract(filename)
|
50
|
+
r1 = %r{\s*\[?\(?\s*[dD](i|I)(v|V)(x|X)\s?(-|_)?\s?\w+\s*\)?\]?\s*}
|
51
|
+
r2 = %r{\s*\[?\(?\s*(x|X)(v|V)(i|I)(d|D)\s?(-|_)?\s?\w+\s*\)?\]?\s*}
|
52
|
+
r3 = %r{\s*\[?\(?\s*(d|D)(v|V)(d|D)(r|R)(i|I)(p|P)\s?(-|_)?\s*\)?\]?\s*}
|
53
|
+
r = /(#{r1}|#{r2}|#{r3})/
|
54
|
+
filename.gsub!(/-.*/,'') # XXX takes only first part
|
55
|
+
filename.gsub(r,'').gsub(/\s?(-|_)\s?/,' ').gsub(/^\s/,'')
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/test/test_movie-renamer.rb
CHANGED
@@ -16,16 +16,6 @@ class TestMovieRenamer < Test::Unit::TestCase
|
|
16
16
|
MovieRenamer::is_a_test = true
|
17
17
|
end
|
18
18
|
|
19
|
-
# test find movies
|
20
|
-
must "find avi movies in the folder" do
|
21
|
-
assert_equal %w{movie1.avi movie2.avi movie3.mkv movie4.mpg}, @movies
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
# test read movie
|
26
|
-
must "create a movie object" do
|
27
|
-
assert_equal MovieRenamer::Movie.new('movie1.avi',title: 'movie1'),MovieRenamer::readMovie(@movies.first)
|
28
|
-
end
|
29
19
|
|
30
20
|
# test title extraction
|
31
21
|
must "extract a title correctly with bad words in filename" do
|
@@ -59,40 +49,6 @@ class TestMovieRenamer < Test::Unit::TestCase
|
|
59
49
|
assert_equal "2001 - me - famous.avi",MovieRenamer::newName(movie)
|
60
50
|
end
|
61
51
|
|
62
|
-
=begin
|
63
|
-
# print movie info check
|
64
|
-
# XXX usless check for now
|
65
|
-
must "print movie info correctly" do
|
66
|
-
movie = MovieRenamer::Movie.new 'movie1.avi', year: '2001' , director: 'me', title: 'famous'
|
67
|
-
assert MovieRenamer::printMovieInfo(movie)
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
# test for ask function movie
|
72
|
-
must "return true on yes input" do
|
73
|
-
provide_input "yes\n"
|
74
|
-
assert MovieRenamer::ask("do you want to edit this movie")
|
75
|
-
expect_output "do you want to edit this movie\n"
|
76
|
-
end
|
77
|
-
|
78
|
-
must "return false on no input" do
|
79
|
-
provide_input "no\n"
|
80
|
-
assert ! MovieRenamer::ask("do you want to edit this movie")
|
81
|
-
expect_output "do you want to edit this movie\n"
|
82
|
-
end
|
83
|
-
# test edit movie
|
84
|
-
must "edit a movie correctly" do
|
85
|
-
provide_input "yes\n1984\nOrwell James\nBig Brother\n\nyes\n"
|
86
|
-
assert ! MovieRenamer::editMovie(@movies.first)
|
87
|
-
end
|
88
|
-
|
89
|
-
must "edit a movie correctly testing recursion" do
|
90
|
-
provide_input "yes\n1984\nOrwell James\nBig Brother\n\nno\nyes\n1984\nOrwell James\nBig Brother\n1\nyes\n"
|
91
|
-
assert ! MovieRenamer::editMovie(@movies.first)
|
92
|
-
#expect_output("wow")
|
93
|
-
end
|
94
|
-
|
95
|
-
=end
|
96
52
|
# test input sanitize
|
97
53
|
must "sanitize input correctly" do
|
98
54
|
input = "ain't a very bad movie{}\@# son "
|
@@ -105,19 +61,7 @@ class TestMovieRenamer < Test::Unit::TestCase
|
|
105
61
|
assert_equal mov, MovieRenamer::parseMovie(input)
|
106
62
|
end
|
107
63
|
|
108
|
-
|
109
|
-
# test main loop over folder
|
110
|
-
must "ask for all movies in folder" do
|
111
|
-
provide_input "no\nno\nno\nno\n"
|
112
|
-
assert MovieRenamer::folderLoop()
|
113
|
-
end
|
114
|
-
|
115
|
-
# suggest movies XXX no test here
|
116
|
-
must "suggest a movie list from a movie title" do
|
117
|
-
MovieRenamer::suggestMovies("Kill Bill")
|
118
|
-
#expect_output "wow"
|
119
|
-
end
|
120
|
-
=end
|
64
|
+
|
121
65
|
|
122
66
|
# helpers
|
123
67
|
def provide_input (string)
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: movie-renamer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 51
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
7
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
8
|
+
- 23
|
9
|
+
version: 0.0.23
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- ghedamat
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2010-
|
17
|
+
date: 2010-11-02 00:00:00 +01:00
|
19
18
|
default_executable: movie-renamer
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ">="
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
28
|
segments:
|
31
29
|
- 0
|
32
30
|
version: "0"
|
@@ -40,7 +38,6 @@ dependencies:
|
|
40
38
|
requirements:
|
41
39
|
- - ">="
|
42
40
|
- !ruby/object:Gem::Version
|
43
|
-
hash: 3
|
44
41
|
segments:
|
45
42
|
- 0
|
46
43
|
version: "0"
|
@@ -54,7 +51,6 @@ dependencies:
|
|
54
51
|
requirements:
|
55
52
|
- - ">="
|
56
53
|
- !ruby/object:Gem::Version
|
57
|
-
hash: 3
|
58
54
|
segments:
|
59
55
|
- 0
|
60
56
|
version: "0"
|
@@ -72,6 +68,10 @@ extra_rdoc_files:
|
|
72
68
|
files:
|
73
69
|
- bin/movie-renamer
|
74
70
|
- lib/movie-renamer.rb
|
71
|
+
- lib/movie-renamer/imdb.rb
|
72
|
+
- lib/movie-renamer/misc.rb
|
73
|
+
- lib/movie-renamer/movie.rb
|
74
|
+
- lib/movie-renamer/parse.rb
|
75
75
|
- test/helper.rb
|
76
76
|
- test/test_movie-renamer.rb
|
77
77
|
- LICENSE
|
@@ -90,7 +90,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
90
90
|
requirements:
|
91
91
|
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
|
-
hash: 3
|
94
93
|
segments:
|
95
94
|
- 0
|
96
95
|
version: "0"
|
@@ -99,7 +98,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
98
|
requirements:
|
100
99
|
- - ">="
|
101
100
|
- !ruby/object:Gem::Version
|
102
|
-
hash: 3
|
103
101
|
segments:
|
104
102
|
- 0
|
105
103
|
version: "0"
|