tvshow_renamer 0.3 → 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +14 -2
- data/lib/tvshow_renamer/cli.rb +14 -6
- data/lib/tvshow_renamer/renamer.rb +55 -26
- data/lib/tvshow_renamer/tvshow_file.rb +6 -6
- data/lib/tvshow_renamer/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -26,8 +26,20 @@ Installation
|
|
26
26
|
Usage
|
27
27
|
-----
|
28
28
|
|
29
|
-
`tvshow_renamer [options]
|
29
|
+
`tvshow_renamer [options] file|directory ...`
|
30
30
|
|
31
31
|
You can specify many files to rename.
|
32
32
|
|
33
|
-
|
33
|
+
|
34
|
+
### Options
|
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.
|
40
|
+
|
41
|
+
|
42
|
+
Todo
|
43
|
+
----
|
44
|
+
|
45
|
+
* Add possibility to customize the renaming format.
|
data/lib/tvshow_renamer/cli.rb
CHANGED
@@ -5,7 +5,11 @@ module TVShowRenamer
|
|
5
5
|
def self.start
|
6
6
|
options = {cli: true}
|
7
7
|
opts = OptionParser.new do |opts|
|
8
|
-
opts.banner = "Usage: tvshow_renamer [options]
|
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
|
9
13
|
|
10
14
|
opts.on("-l", "--log FILENAME", "Log all renames to FILENAME, inside the TV show files directory") do |log_file|
|
11
15
|
options[:log_file] = log_file
|
@@ -23,10 +27,14 @@ module TVShowRenamer
|
|
23
27
|
end
|
24
28
|
opts.parse!
|
25
29
|
|
26
|
-
if ARGV.
|
30
|
+
if ARGV.empty?
|
27
31
|
puts opts
|
28
32
|
else
|
29
|
-
tvshow_name =
|
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
|
30
38
|
Renamer.new(tvshow_name, options).rename(ARGV)
|
31
39
|
end
|
32
40
|
end
|
@@ -37,7 +45,7 @@ module TVShowRenamer
|
|
37
45
|
$stdin.gets.chomp.strip
|
38
46
|
end
|
39
47
|
|
40
|
-
def self.prompt_edit_value(prompt, value = nil)
|
48
|
+
def self.prompt_edit_value(prompt, value = nil, regex = nil)
|
41
49
|
prompt << " (#{value})" if value
|
42
50
|
prompt << " : "
|
43
51
|
ok = false
|
@@ -46,8 +54,8 @@ module TVShowRenamer
|
|
46
54
|
if value && str.empty?
|
47
55
|
ok = true
|
48
56
|
else
|
49
|
-
if
|
50
|
-
value = str
|
57
|
+
if !regex || str =~ regex
|
58
|
+
value = str
|
51
59
|
ok = true
|
52
60
|
end
|
53
61
|
end
|
@@ -6,6 +6,7 @@ module TVShowRenamer
|
|
6
6
|
attr_accessor :tvshow_name
|
7
7
|
|
8
8
|
EXTENSIONS = %w( .mkv .avi .mp4 .srt )
|
9
|
+
NUMBER_REGEX = /\A\d+\z/
|
9
10
|
|
10
11
|
def initialize(tvshow_name, options = {})
|
11
12
|
@tvshow_name = tvshow_name
|
@@ -35,21 +36,21 @@ module TVShowRenamer
|
|
35
36
|
|
36
37
|
def rename_file(filename)
|
37
38
|
renamed = false
|
38
|
-
tvfile = TVShowFile.new @tvshow_name, File.expand_path(filename)
|
39
|
+
@tvfile = TVShowFile.new @tvshow_name, File.expand_path(filename)
|
39
40
|
|
40
|
-
if EXTENSIONS.include?(tvfile.extname.downcase)
|
41
|
-
tvfile.detect_season_and_episode
|
41
|
+
if EXTENSIONS.include?(@tvfile.extname.downcase)
|
42
|
+
@tvfile.detect_season_and_episode
|
42
43
|
|
43
|
-
if tvfile.detected_season && tvfile.detected_episode
|
44
|
-
renamed = prompt_rename
|
44
|
+
if @tvfile.detected_season && @tvfile.detected_episode
|
45
|
+
renamed = prompt_rename
|
45
46
|
else
|
46
47
|
answered = false
|
47
48
|
until answered
|
48
49
|
case CLI.prompt("No season and episode values have been detected for this file, do you want to rename it ? (yn) ").downcase
|
49
50
|
when 'y'
|
50
|
-
tvfile.season = CLI.prompt_edit_value
|
51
|
-
tvfile.episode = CLI.prompt_edit_value
|
52
|
-
renamed = prompt_rename
|
51
|
+
@tvfile.season = CLI.prompt_edit_value("Season", nil, NUMBER_REGEX).to_i
|
52
|
+
@tvfile.episode = CLI.prompt_edit_value("Episode", nil, NUMBER_REGEX).to_i
|
53
|
+
renamed = prompt_rename
|
53
54
|
answered = true
|
54
55
|
when 'n'
|
55
56
|
answered = true
|
@@ -60,35 +61,43 @@ module TVShowRenamer
|
|
60
61
|
renamed
|
61
62
|
end
|
62
63
|
|
63
|
-
def prompt_rename
|
64
|
+
def prompt_rename
|
64
65
|
renamed = answered = false
|
65
66
|
until answered
|
66
|
-
case CLI.prompt("Rename \"#{tvfile.basename}\" to \"#{tvfile.new_basename}\" ? (
|
67
|
+
case CLI.prompt("Rename \"#{@tvfile.basename}\" to \"#{@tvfile.new_basename}\" ? (ynemqh) ").downcase
|
67
68
|
when 'y'
|
68
|
-
if
|
69
|
-
|
69
|
+
if @tvfile.filename == @tvfile.new_filename
|
70
|
+
puts "The two filenames are the same."
|
70
71
|
else
|
71
|
-
|
72
|
-
|
72
|
+
if File.exists?(@tvfile.new_filename)
|
73
|
+
renamed = answered = prompt_override
|
74
|
+
else
|
75
|
+
move_tvshow_file
|
76
|
+
renamed = answered = true
|
77
|
+
end
|
73
78
|
end
|
74
79
|
when 'n'
|
75
80
|
answered = true
|
76
81
|
when 'e'
|
77
|
-
tvfile.season = CLI.prompt_edit_value
|
78
|
-
tvfile.episode = CLI.prompt_edit_value
|
82
|
+
@tvfile.season = CLI.prompt_edit_value("Season", @tvfile.detected_season, NUMBER_REGEX)
|
83
|
+
@tvfile.episode = CLI.prompt_edit_value("Episode", @tvfile.detected_episode, NUMBER_REGEX)
|
84
|
+
when 'm'
|
85
|
+
show_menu
|
86
|
+
when 'q'
|
87
|
+
exit
|
79
88
|
when '?', 'h'
|
80
|
-
puts "y: Yes, n: No, e: Edit"
|
89
|
+
puts "y: Yes, n: No, e: Edit, m: Menu, q: Quit"
|
81
90
|
end
|
82
91
|
end
|
83
92
|
renamed
|
84
93
|
end
|
85
94
|
|
86
|
-
def prompt_override
|
95
|
+
def prompt_override
|
87
96
|
overrided = answered = false
|
88
97
|
until answered
|
89
|
-
case CLI.prompt("File \"#{tvfile.new_basename}\" already exists. Override ? (yn) ").downcase
|
98
|
+
case CLI.prompt("File \"#{@tvfile.new_basename}\" already exists. Override ? (yn) ").downcase
|
90
99
|
when 'y'
|
91
|
-
move_tvshow_file
|
100
|
+
move_tvshow_file
|
92
101
|
answered = true
|
93
102
|
overrided = true
|
94
103
|
when 'n'
|
@@ -99,15 +108,35 @@ module TVShowRenamer
|
|
99
108
|
overrided
|
100
109
|
end
|
101
110
|
|
102
|
-
def
|
103
|
-
|
104
|
-
|
111
|
+
def show_menu
|
112
|
+
puts "Menu"
|
113
|
+
puts "----"
|
114
|
+
puts "1. Change TV Show name"
|
115
|
+
puts "2. Change format (not yet available)"
|
116
|
+
puts "q. Quit menu"
|
117
|
+
puts "----"
|
118
|
+
quit = false
|
119
|
+
until quit
|
120
|
+
case CLI.prompt("Choice : ")
|
121
|
+
when '1'
|
122
|
+
@tvfile.tvshow_name = CLI.prompt_edit_value("TV Show Name", @tvfile.tvshow_name)
|
123
|
+
when '2'
|
124
|
+
puts "You cannot change the format yet, sorry."
|
125
|
+
when 'q'
|
126
|
+
quit = true
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def move_tvshow_file
|
132
|
+
FileUtils.mv @tvfile.filename, @tvfile.new_filename
|
133
|
+
log_rename
|
105
134
|
end
|
106
135
|
|
107
|
-
def log_rename
|
136
|
+
def log_rename
|
108
137
|
if @options[:log_file]
|
109
|
-
File.open(File.join(tvfile.dirname, @options[:log_file]), 'a') do |file|
|
110
|
-
file.puts "\"#{tvfile.basename}\" => \"#{tvfile.new_basename}\"\n"
|
138
|
+
File.open(File.join(@tvfile.dirname, @options[:log_file]), 'a') do |file|
|
139
|
+
file.puts "\"#{@tvfile.basename}\" => \"#{@tvfile.new_basename}\"\n"
|
111
140
|
end
|
112
141
|
end
|
113
142
|
end
|
@@ -1,11 +1,6 @@
|
|
1
1
|
module TVShowRenamer
|
2
2
|
class TVShowFile
|
3
|
-
attr_reader :tvshow_name
|
4
|
-
attr_reader :filename
|
5
|
-
attr_accessor :season
|
6
|
-
attr_accessor :episode
|
7
|
-
attr_reader :detected_season
|
8
|
-
attr_reader :detected_episode
|
3
|
+
attr_reader :tvshow_name, :filename, :season, :episode, :detected_season, :detected_episode
|
9
4
|
|
10
5
|
def initialize(tvshow_name, filename)
|
11
6
|
@tvshow_name = tvshow_name
|
@@ -14,6 +9,11 @@ module TVShowRenamer
|
|
14
9
|
|
15
10
|
# Custom setters
|
16
11
|
|
12
|
+
def tvshow_name=(tvshow_name)
|
13
|
+
@tvshow_name = tvshow_name
|
14
|
+
@new_basename = @new_filename = nil
|
15
|
+
end
|
16
|
+
|
17
17
|
def season=(season)
|
18
18
|
@season = season
|
19
19
|
@new_basename = @new_filename = nil
|
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
|
+
version: '0.4'
|
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-
|
12
|
+
date: 2012-11-26 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
|