reorganise 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/reorganise +69 -27
  2. data/lib/reorganise/version.rb +1 -1
  3. metadata +4 -4
data/bin/reorganise CHANGED
@@ -7,7 +7,7 @@ module Reorganise
7
7
  require 'to_regexp'
8
8
  require 'fileutils'
9
9
 
10
- attr_accessor :options, :matcher, :extensions, :directory, :series_name
10
+ attr_accessor :options, :matcher, :extensions, :directory, :series_name, :output_dir
11
11
 
12
12
  def initialize(argv)
13
13
 
@@ -27,8 +27,12 @@ module Reorganise
27
27
  @options[:regex] = @matcher
28
28
 
29
29
  opts.on('-i', '--input-dir DIRECTORY', 'Specifies where to look for episodes to rename.') do |dir_name|
30
- @options[:directory] = dir_name
30
+ @options[:input_directory] = dir_name.gsub(/\\/, "/")
31
31
  end
32
+
33
+ opts.on('-o', '--output-dir DIRECTORY', 'Specifies where to move renamed files to. Default is the input directory.') do |dir_name|
34
+ @options[:output_directory] = dir_name.gsub(/\\/, "/")
35
+ end
32
36
 
33
37
  opts.on('-n', '--name NAME', 'What to rename the series to. Title is derived from series if not given.') do |name|
34
38
  @options[:series_name] = sanitize_user_string(name)
@@ -58,27 +62,33 @@ module Reorganise
58
62
  end
59
63
 
60
64
  def sort!
61
- @directory = @options[:directory]
65
+ @directory = @options[:input_directory]
62
66
  @series_name = @options[:series_name]
67
+ @output_dir = @options[:output_directory]
63
68
 
64
- if directory.nil? or !Dir.exists?(directory)
65
- puts "ERROR: Directory '#{directory}' does not exist. Run with -h for more information."
69
+ if directory.nil? or !Dir.exists?(@directory)
70
+ puts "ERROR: Directory '#{@directory}' does not exist. Run with -h for more information."
66
71
  exit
67
72
  end
68
73
 
69
- Dir.chdir(directory)
74
+ if @output_dir and !Dir.exists?(@output_dir)
75
+ puts "ERROR: Destination directory #{@output_dir} does not exist."
76
+ exit
77
+ end
78
+
79
+ Dir.chdir(@directory)
70
80
  filenames = Dir.glob("**/*.{#{@extensions.join(",")}}")
71
81
 
72
82
  puts "------------------------------------------------------------------------"
73
83
  puts "Finding all files with the following extensions: #{@extensions.join(", ")}"
74
84
  puts ""
75
- puts "Directory: #{directory}"
85
+ puts "Directory: #{@directory}"
76
86
  puts "Found #{filenames.size} files."
77
87
  puts "------------------------------------------------------------------------"
78
88
  puts ""
79
89
  puts ""
80
90
  puts "!!! NOTICE: This is a PRETEND renaming to show what would happen. !!! "
81
- puts "To confirm the renaming of files, run the command again with -c. e.g. `reorganise -c -d [directory] -n [series name]"
91
+ puts "To confirm the renaming of files, run the command again with -c. e.g. `reorganise -c -i [input-dir]"
82
92
  puts ""
83
93
  puts ""
84
94
  puts "Starting to rename files using regex #{@options[:regex]}..."
@@ -90,36 +100,56 @@ module Reorganise
90
100
  if details = parse_filename(filename)
91
101
 
92
102
  old_directory = File.absolute_path(File.dirname(filename))
103
+ old_file = File.join(old_directory, details[:basename])
93
104
 
94
- # Check to see if the files were already organised into season folders
95
- if old_directory.split("/").last.downcase == "season #{details[:season].to_i}"
96
- target_directory = old_directory
105
+ unless @output_dir
106
+ # Check to see if the files were already organised into season folders
107
+ if old_directory.split("/").last =~ /^(?:season (\d+)|s(\d+))$/i and details[:season].to_i == ($1 || $2).to_i
108
+ target_directory = old_directory
109
+ else
110
+ target_directory = File.join(old_directory, "Season #{details[:season].to_i}")
111
+ end
97
112
  else
98
- target_directory = File.join(old_directory, "Season #{details[:season].to_i}")
113
+ unless details[:series_name].nil? or details[:series_name].empty?
114
+ target_directory = File.join(@output_dir, details[:series_name], "Season #{details[:season].to_i}")
115
+ else
116
+ puts "ERROR: Cannot move #{old_file} as it does not have a series name and none was supplied. Please supply a series name with -n [series name]."
117
+ next
118
+ end
99
119
  end
100
120
 
101
121
  if details[:series_name]
102
122
  renamed_file = "#{details[:series_name]} - S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
103
123
  else
104
124
  renamed_file = "S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
105
- end
125
+ end
106
126
 
107
- # Perform mv if confimed
108
- if @options[:confirm]
109
- FileUtils.mkdir_p(target_directory)
110
- FileUtils.mv(File.join(old_directory, details[:basename]), File.join(target_directory, renamed_file))
111
- end
112
-
113
- if @options[:verbose] or !@options[:confirm]
114
- puts "-: #{File.join(old_directory, details[:basename])}"
115
- puts "+: #{File.join(target_directory, renamed_file)}"
116
- puts ""
127
+ target_file = File.join(target_directory, renamed_file)
128
+
129
+ # Check if the target file already exists
130
+ unless File.file?(target_file)
131
+
132
+ if @options[:verbose] or !@options[:confirm]
133
+ puts "-: #{old_file}"
134
+ puts "+: #{target_file}"
135
+ #puts ""
136
+ end
137
+
138
+ # Perform mv if confimed
139
+ if @options[:confirm]
140
+ FileUtils.mkdir_p(target_directory)
141
+ FileUtils.mv(old_file, target_file)
142
+ try_delete_folder(old_directory)
143
+ end
144
+
145
+ else
146
+ puts "ERROR: Could not move #{old_file} to #{target_file} as it already exists."
117
147
  end
118
148
  else
119
- puts "ERROR: Skipped file \"#{filename}\" due to invalid file format."
149
+ puts "ERROR: Skipped file #{filename} due to invalid file format."
120
150
  end
121
151
  else
122
- puts "ERROR: Skipped non-file \"#{filename}\"."
152
+ puts "ERROR: Skipped non-file #{filename}."
123
153
  end
124
154
 
125
155
  end
@@ -144,8 +174,8 @@ module Reorganise
144
174
 
145
175
  parts.each_with_index do |v, k|
146
176
  if v =~ @matcher
147
- season = "%02d" % ($1 || $3 || $5).to_i
148
- episode = "%02d" % ($2 || $4 || $6).to_i
177
+ season = "%02d" % ($1 || $3 || $5 || $7).to_i
178
+ episode = "%02d" % ($2 || $4 || $6 || $8).to_i
149
179
  position = k
150
180
 
151
181
  break if position != -1
@@ -167,6 +197,18 @@ module Reorganise
167
197
  end
168
198
  end
169
199
 
200
+ def try_delete_folder(directory)
201
+ files = Dir.glob(File.join(directory, "*"))
202
+ if files.empty?
203
+ puts "Deleting empty folder: #{directory}" if @options[:verbose]
204
+ begin
205
+ FileUtils.rmdir(directory)
206
+ rescue Errno::ENOTEMPTY
207
+ puts "ERROR: Cannot delete folder #{directory} as it is not empty."
208
+ end
209
+ end
210
+ end
211
+
170
212
  def sanitize_user_string(str)
171
213
  return false if str.empty?
172
214
  str = str.gsub(/[^\s\w.\-]/, '')
@@ -1,3 +1,3 @@
1
1
  module Reorganise
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-08 00:00:00.000000000Z
12
+ date: 2012-01-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: to_regexp
16
- requirement: &83562250 !ruby/object:Gem::Requirement
16
+ requirement: &70441060 !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: *83562250
24
+ version_requirements: *70441060
25
25
  description: Sorts and renames your series.
26
26
  email:
27
27
  - justin.cossutti@gmail.com