reorganise 0.0.5 → 0.0.6

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.
Files changed (3) hide show
  1. data/bin/reorganise +48 -33
  2. data/lib/reorganise/version.rb +1 -1
  3. metadata +3 -3
@@ -7,10 +7,10 @@ module Reorganise
7
7
  require 'to_regexp'
8
8
  require 'fileutils'
9
9
 
10
- attr_accessor :options, :matcher, :extensions, :directory, :series_name, :output_dir
10
+ attr_accessor :options, :matcher, :extensions, :directory, :series_name, :output_dir
11
11
 
12
12
  def initialize(argv)
13
-
13
+
14
14
  @extensions = [:avi, :mp4, :mkv]
15
15
  @options = {}
16
16
  @matcher = /(?:S(\d+)x?E(\d+)|(\d{1,2})(\d{2,})|(\d+)x(\d+))/i
@@ -29,7 +29,7 @@ module Reorganise
29
29
  opts.on('-i', '--input-dir DIRECTORY', 'Specifies where to look for episodes to rename.') do |dir_name|
30
30
  @options[:input_directory] = dir_name.gsub(/\\/, "/")
31
31
  end
32
-
32
+
33
33
  opts.on('-o', '--output-dir DIRECTORY', 'Specifies where to move renamed files to. Default is the input directory.') do |dir_name|
34
34
  @options[:output_directory] = dir_name.gsub(/\\/, "/")
35
35
  end
@@ -37,12 +37,12 @@ module Reorganise
37
37
  opts.on('-n', '--name NAME', 'What to rename the series to. Title is derived from series if not given.') do |name|
38
38
  @options[:series_name] = sanitize_user_string(name)
39
39
  end
40
-
40
+
41
41
  @options[:confirm] = false
42
42
  opts.on('-c', '--confirm', 'Confirms the go ahead to rename files. Only pretends to rename files to show the effects if not given.') do |regex|
43
43
  @options[:confirm] = true
44
44
  end
45
-
45
+
46
46
  opts.on('-r', '--regexp MATCHER', 'Alternative regexp matcher.') do |regex|
47
47
  @options[:regex] = regex.to_regexp
48
48
  end
@@ -65,17 +65,17 @@ module Reorganise
65
65
  @directory = @options[:input_directory]
66
66
  @series_name = @options[:series_name]
67
67
  @output_dir = @options[:output_directory]
68
-
68
+
69
69
  if directory.nil? or !Dir.exists?(@directory)
70
70
  puts "ERROR: Directory '#{@directory}' does not exist. Run with -h for more information."
71
71
  exit
72
72
  end
73
-
73
+
74
74
  if @output_dir and !Dir.exists?(@output_dir)
75
75
  puts "ERROR: Destination directory #{@output_dir} does not exist."
76
76
  exit
77
77
  end
78
-
78
+
79
79
  Dir.chdir(@directory)
80
80
  filenames = Dir.glob("**/*.{#{@extensions.join(",")}}")
81
81
 
@@ -98,10 +98,10 @@ module Reorganise
98
98
  if File.file?(filename)
99
99
 
100
100
  if details = parse_filename(filename)
101
-
101
+
102
102
  old_directory = File.absolute_path(File.dirname(filename))
103
103
  old_file = File.join(old_directory, details[:basename])
104
-
104
+
105
105
  unless @output_dir
106
106
  # Check to see if the files were already organised into season folders
107
107
  if old_directory.split("/").last =~ /(?:season (\d+)|^s(\d+)$)/i and details[:season].to_i == ($1 || $2).to_i
@@ -117,31 +117,31 @@ module Reorganise
117
117
  next
118
118
  end
119
119
  end
120
-
120
+
121
121
  if details[:series_name]
122
122
  renamed_file = "#{details[:series_name]} - S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
123
123
  else
124
124
  renamed_file = "S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
125
125
  end
126
-
126
+
127
127
  target_file = File.join(target_directory, renamed_file)
128
-
128
+
129
129
  # Check if the target file already exists
130
130
  unless File.file?(target_file)
131
-
131
+
132
132
  if @options[:verbose] or !@options[:confirm]
133
133
  puts "-: #{old_file}"
134
134
  puts "+: #{target_file}"
135
135
  #puts ""
136
136
  end
137
-
137
+
138
138
  # Perform mv if confimed
139
139
  if @options[:confirm]
140
140
  FileUtils.mkdir_p(target_directory)
141
141
  FileUtils.mv(old_file, target_file)
142
142
  try_delete_folder(old_directory)
143
143
  end
144
-
144
+
145
145
  else
146
146
  puts "ERROR: Could not move #{old_file} to #{target_file} as it already exists."
147
147
  end
@@ -153,50 +153,66 @@ module Reorganise
153
153
  end
154
154
 
155
155
  end
156
-
156
+
157
157
  puts ""
158
158
  puts "Finished renaming #{filenames.size} files."
159
-
159
+
160
160
  if !@options[:confirm]
161
161
  puts ""
162
162
  puts "!!! NOTICE: This was a pretend run, nothing was renamed. !!!"
163
163
  puts "To confirm the renaming, run this command again with -c"
164
164
  end
165
165
  end
166
-
166
+
167
167
  def parse_filename(filename)
168
168
  basename = File.basename(filename)
169
169
  extension = File.extname(filename)
170
-
171
- parts = basename.gsub(/[-._]/i, " ").strip.split(" ")
172
-
170
+
171
+ cleaned_name = basename.gsub(/[-._]/i, " ").strip
172
+ parts = cleaned_name.split(" ")
173
+
173
174
  position = season = episode = -1
174
-
175
+
175
176
  parts.each_with_index do |v, k|
176
177
  if v =~ @matcher
177
178
  season = "%02d" % ($1 || $3 || $5 || $7).to_i
178
179
  episode = "%02d" % ($2 || $4 || $6 || $8).to_i
179
180
  position = k
180
-
181
+
181
182
  break if position != -1
182
183
  end
183
184
  end
184
-
185
- if position != -1
186
- # No title if episode numbers are first
185
+
186
+ if position == -1
187
+ if cleaned_name =~ /(.*)s(\d){1,2}.?e(\d){1,2}/i
188
+ title = $1.strip.split(" ")
189
+ if title.empty?
190
+ title = nil
191
+ else
192
+ title = title.map{|p| p.capitalize}.join(" ")
193
+ end
194
+ season = "%02d" % $2
195
+ episode = "%02d" % $3
196
+ position = 1
197
+ end
198
+ else
187
199
  if position == 0
188
200
  title = (@series_name) ? @series_name : nil
189
201
  else
190
202
  title = parts[0..position-1].map{|p| p.capitalize}.join(" ")
191
- end
203
+ end
204
+ end
205
+
206
+ if position != -1
192
207
  details = {:basename => basename, :ext => extension, :season => season, :episode => episode}
193
208
  details[:series_name] = (@series_name.nil?) ? title : @series_name
194
209
  details
195
210
  else
196
- false
197
- end
211
+ return false
212
+ end
213
+
198
214
  end
199
-
215
+
200
216
  def try_delete_folder(directory)
201
217
  files = Dir.glob(File.join(directory, "*"))
202
218
  if files.empty?
@@ -208,13 +224,12 @@ module Reorganise
208
224
  end
209
225
  end
210
226
  end
211
-
227
+
212
228
  def sanitize_user_string(str)
213
229
  return false if str.empty?
214
230
  str = str.gsub(/[^\s\w.\-]/, '')
215
231
  str
216
232
  end
217
-
218
233
  end
219
234
  end
220
235
 
@@ -1,3 +1,3 @@
1
1
  module Reorganise
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reorganise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-01-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: to_regexp
16
- requirement: &86167830 !ruby/object:Gem::Requirement
16
+ requirement: &82330130 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *86167830
24
+ version_requirements: *82330130
25
25
  description: Sorts and renames your series.
26
26
  email:
27
27
  - justin.cossutti@gmail.com