tvshow_renamer 0.4.1 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,13 +33,15 @@ You can specify many files to rename.
33
33
 
34
34
  ### Options
35
35
  * `-n TVSHOW_NAME` or `--name TVSHOW_NAME`
36
- This option define the TV show name used to rename the files. If the option is not present, the name will be asked first.
37
-
38
- * `-l FILENAME` or `--log FILENAME`
39
- This option make the utility create a file inside the same directory as the renamed files containing the old and new names of the files. It can be useful to keep the episode or version names.
36
+ This option defines the TV show name used to rename the files. If the option is not present, the name will be asked first.
40
37
 
38
+ * `-f FORMAT` or `--format FORMAT`
39
+ This option defines the format for the new filenames.
40
+ `$n` will be substitued by the TV show name, `$s` by the season number, `$e` by the episode number.
41
+ The default format is : `$n - $sx$e`.
41
42
 
42
- Todo
43
- ----
43
+ * `-l FILENAME` or `--log FILENAME`
44
+ This option makes the utility create a file inside the same directory as the renamed files containing the old and new names of the files. It can be useful to keep the episode or version names.
44
45
 
45
- * Add possibility to customize the renaming format.
46
+ * `-r` or `--recursive`
47
+ If any directory is passed as parameter, this option makes the utility look recursively inside directories for files to rename.
data/bin/tvshow_renamer CHANGED
@@ -2,4 +2,4 @@
2
2
  require 'rubygems'
3
3
  require 'tvshow_renamer'
4
4
 
5
- TVShowRenamer::CLI.start
5
+ TVShowRenamer::Runner.start
@@ -1,4 +1,5 @@
1
1
  require 'tvshow_renamer/cli'
2
2
  require 'tvshow_renamer/renamer'
3
+ require 'tvshow_renamer/runner'
3
4
  require 'tvshow_renamer/tvshow_file'
4
5
  require 'tvshow_renamer/version'
@@ -1,44 +1,5 @@
1
- require 'optparse'
2
-
3
1
  module TVShowRenamer
4
2
  class CLI
5
- def self.start
6
- options = {cli: true}
7
- opts = OptionParser.new do |opts|
8
- opts.banner = "Usage: tvshow_renamer [options] file|directory ..."
9
-
10
- opts.on("-n", "--name TVSHOW_NAME", "Log all renames to FILENAME, inside the TV show files directory") do |tvshow_name|
11
- options[:tvshow_name] = tvshow_name
12
- end
13
-
14
- opts.on("-l", "--log FILENAME", "Log all renames to FILENAME, inside the TV show files directory") do |log_file|
15
- options[:log_file] = log_file
16
- end
17
-
18
- opts.on_tail("-h", "--help", "Show this message") do
19
- puts opts
20
- exit
21
- end
22
-
23
- opts.on_tail("--version", "Show version") do
24
- puts TVShowRenamer::VERSION
25
- exit
26
- end
27
- end
28
- opts.parse!
29
-
30
- if ARGV.empty?
31
- puts opts
32
- else
33
- tvshow_name = if options[:tvshow_name]
34
- options.delete(:tvshow_name)
35
- else
36
- self.prompt 'Please enter the name of the TV Show for these files : '
37
- end
38
- Renamer.new(tvshow_name, options).rename(ARGV)
39
- end
40
- end
41
-
42
3
  def self.prompt(prompt)
43
4
  print prompt
44
5
  $stdout.flush
@@ -3,13 +3,10 @@ require 'fileutils'
3
3
 
4
4
  module TVShowRenamer
5
5
  class Renamer
6
- attr_accessor :tvshow_name
7
-
8
6
  EXTENSIONS = %w( .mkv .avi .mp4 .srt )
9
7
  NUMBER_REGEX = /\A\d+\z/
10
8
 
11
- def initialize(tvshow_name, options = {})
12
- @tvshow_name = tvshow_name
9
+ def initialize(options = {})
13
10
  @options = options
14
11
  end
15
12
 
@@ -29,14 +26,14 @@ module TVShowRenamer
29
26
 
30
27
  def rename_dir(dirname)
31
28
  puts "Renaming files in directory \"#{dirname}\""
32
- Dir.glob(dirname + '/**').each do |filename|
29
+ Dir.glob(dirname + (@options[:recursive] ? '/**/*' : '/**')).each do |filename|
33
30
  rename_file(filename)
34
31
  end
35
32
  end
36
33
 
37
34
  def rename_file(filename)
38
35
  renamed = false
39
- @tvfile = TVShowFile.new @tvshow_name, File.expand_path(filename)
36
+ @tvfile = TVShowFile.new @options, File.expand_path(filename)
40
37
 
41
38
  if EXTENSIONS.include?(@tvfile.extname.downcase)
42
39
  @tvfile.detect_season_and_episode
@@ -112,20 +109,26 @@ module TVShowRenamer
112
109
  puts "Menu"
113
110
  puts "----"
114
111
  puts "1. Change TV Show name"
115
- puts "2. Change format (not yet available)"
112
+ puts "2. Change format"
116
113
  puts "q. Quit menu"
117
114
  puts "----"
118
115
  quit = false
119
116
  until quit
120
117
  case CLI.prompt("Choice : ")
121
118
  when '1'
122
- new_tvshow_name = CLI.prompt_edit_value("TV Show Name", @tvfile.tvshow_name)
123
- if new_tvshow_name != @tvshow_name
124
- @tvshow_name = @tvfile.tvshow_name = new_tvshow_name
125
- puts "TV Show Name is now #{@tvshow_name}."
119
+ new_tvshow_name = CLI.prompt_edit_value("TV Show Name", @options[:tvshow_name])
120
+ if new_tvshow_name != @options[:tvshow_name]
121
+ @options[:tvshow_name] = new_tvshow_name
122
+ @tvfile.options_modified
123
+ puts "TV Show Name is now #{new_tvshow_name}."
126
124
  end
127
125
  when '2'
128
- puts "You cannot change the format yet, sorry."
126
+ new_format = CLI.prompt_edit_value("Format - $n : TV show name, $s : Season, $e : Episode", @options[:format])
127
+ if new_format != @options[:format]
128
+ @options[:format] = new_format
129
+ @tvfile.options_modified
130
+ puts "Format is now #{new_format}."
131
+ end
129
132
  when 'q'
130
133
  quit = true
131
134
  end
@@ -0,0 +1,52 @@
1
+ require 'optparse'
2
+
3
+ module TVShowRenamer
4
+ class Runner
5
+ def self.start
6
+ options = {}
7
+ options[:format] = "$n - $sx$e"
8
+
9
+ opts = OptionParser.new do |opts|
10
+ opts.banner = "Usage: tvshow_renamer [options] file|directory ..."
11
+
12
+ opts.on("-n", "--name TVSHOW_NAME", "Define the TV show name used to rename the files") do |tvshow_name|
13
+ options[:tvshow_name] = tvshow_name
14
+ end
15
+
16
+ opts.on("-f", "--format FORMAT",
17
+ "Define the format for the new filenames - Default : \"$n - $sx$e\"",
18
+ "$n will be substitued by the TV show name, $s by the season number, $e by the episode number") do |format|
19
+ options[:format] = format
20
+ end
21
+
22
+ opts.on("-l", "--log FILENAME", "Log all renames to FILENAME, inside the TV show files directory") do |log_file|
23
+ options[:log_file] = log_file
24
+ end
25
+
26
+ opts.on("-r", "--recursive", "If passed a directory, look recursively inside it for files to rename") do |recursive|
27
+ options[:recursive] = recursive
28
+ end
29
+
30
+ opts.separator ""
31
+
32
+ opts.on_tail("-h", "--help", "Show this message") do
33
+ puts opts
34
+ exit
35
+ end
36
+
37
+ opts.on_tail("--version", "Show version") do
38
+ puts TVShowRenamer::VERSION
39
+ exit
40
+ end
41
+ end
42
+ opts.parse!
43
+
44
+ if ARGV.empty?
45
+ puts opts
46
+ else
47
+ options[:tvshow_name] ||= CLI.prompt 'Please enter the name of the TV Show for these files : '
48
+ Renamer.new(options).rename(ARGV)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,19 +1,14 @@
1
1
  module TVShowRenamer
2
2
  class TVShowFile
3
- attr_reader :tvshow_name, :filename, :season, :episode, :detected_season, :detected_episode
3
+ attr_reader :filename, :season, :episode, :detected_season, :detected_episode
4
4
 
5
- def initialize(tvshow_name, filename)
6
- @tvshow_name = tvshow_name
5
+ def initialize(options = {}, filename = nil)
6
+ @options = options
7
7
  @filename = filename
8
8
  end
9
9
 
10
10
  # Custom setters
11
11
 
12
- def tvshow_name=(tvshow_name)
13
- @tvshow_name = tvshow_name
14
- @new_basename = @new_filename = nil
15
- end
16
-
17
12
  def season=(season)
18
13
  @season = season
19
14
  @new_basename = @new_filename = nil
@@ -40,7 +35,10 @@ module TVShowRenamer
40
35
 
41
36
  def new_basename
42
37
  unless @new_basename
43
- @new_basename = "#{@tvshow_name} - %02ix%02i#{extname}" % [@season, @episode]
38
+ @new_basename = @options[:format].gsub('$n', @options[:tvshow_name])
39
+ .gsub('$s', '%02i' % @season)
40
+ .gsub('$e', '%02i' % @episode)
41
+ .concat(extname)
44
42
  @new_filename = nil
45
43
  end
46
44
  @new_basename
@@ -52,6 +50,10 @@ module TVShowRenamer
52
50
 
53
51
  # Active methods
54
52
 
53
+ def options_modified
54
+ @new_basename = @new_filename = nil
55
+ end
56
+
55
57
  def detect_season_and_episode
56
58
  regex = /(?<season>\d{1,2})(e|x|\.)?(?<episode>\d{2,})/i
57
59
  match = regex.match basename
@@ -1,3 +1,3 @@
1
1
  module TVShowRenamer
2
- Version = VERSION = '0.4.1'
2
+ Version = VERSION = '0.5'
3
3
  end
@@ -2,9 +2,11 @@ require 'test/unit'
2
2
  require 'tvshow_renamer'
3
3
 
4
4
  class TVShowRenamerTest < Test::Unit::TestCase
5
+ FORMAT = '$n - $sx$e'
5
6
  TVSHOW_NAME = 'Famous Show'
6
7
 
7
8
  def test_detect_season_and_episode
9
+ options = {:format => FORMAT, :tvshow_name => TVSHOW_NAME}
8
10
  {
9
11
  'FamousShow.S05E01.DVDRip.XviD-BLAH.avi' => [5, 1],
10
12
  'FamousShow.12x03.DVDRip.XviD-BLAH.avi' => [12,3],
@@ -13,7 +15,7 @@ class TVShowRenamerTest < Test::Unit::TestCase
13
15
  'Famous.Show.03.04.avi' => [3, 4],
14
16
  'Famous-Show.04x00.srt' => [4, 0],
15
17
  }.each do |k,v|
16
- tvfile = TVShowRenamer::TVShowFile.new(TVSHOW_NAME, k)
18
+ tvfile = TVShowRenamer::TVShowFile.new(options, k)
17
19
  tvfile.detect_season_and_episode
18
20
 
19
21
  assert_equal v, [tvfile.season, tvfile.episode]
@@ -21,13 +23,14 @@ class TVShowRenamerTest < Test::Unit::TestCase
21
23
  end
22
24
 
23
25
  def test_new_basename
24
- tvfile = TVShowRenamer::TVShowFile.new(TVSHOW_NAME, 'test.mkv')
26
+ options = {:format => FORMAT, :tvshow_name => TVSHOW_NAME}
27
+ tvfile = TVShowRenamer::TVShowFile.new(options, 'test.mkv')
25
28
  tvfile.season = 1
26
-
27
29
  tvfile.episode = 2
28
30
  assert_equal 'Famous Show - 01x02.mkv', tvfile.new_basename
29
31
 
30
- tvfile.episode = 101
31
- assert_equal 'Famous Show - 01x101.mkv', tvfile.new_basename
32
+ options[:format] = '$n$s$e'
33
+ tvfile.options_modified
34
+ assert_equal 'Famous Show0102.mkv', tvfile.new_basename
32
35
  end
33
36
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tvshow_renamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: '0.5'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-26 00:00:00.000000000 Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Utility to rename TV Show files to a correct format
15
15
  email: francois@fklingler.com
@@ -23,6 +23,7 @@ files:
23
23
  - Rakefile
24
24
  - lib/tvshow_renamer/cli.rb
25
25
  - lib/tvshow_renamer/renamer.rb
26
+ - lib/tvshow_renamer/runner.rb
26
27
  - lib/tvshow_renamer/tvshow_file.rb
27
28
  - lib/tvshow_renamer/version.rb
28
29
  - lib/tvshow_renamer.rb