reorganise 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/reorganise CHANGED
@@ -3,15 +3,179 @@
3
3
  module Reorganise
4
4
  class Reorganise
5
5
 
6
- def initialize()
7
- puts "Hi"
6
+ require 'optparse'
7
+ require 'to_regexp'
8
+ require 'fileutils'
9
+
10
+ attr_accessor :options, :matcher, :extensions, :directory, :series_name
11
+
12
+ def initialize(argv)
13
+
14
+ @extensions = [:avi, :mp4, :mkv]
15
+ @options = {}
16
+ @matcher = /(?:S(\d+)x?E(\d+)|(\d{1,2})(\d{2,})|(\d+)x(\d+))/i
17
+
18
+ parse_options!(argv)
19
+ sort!
20
+
8
21
  end
9
22
 
10
- def say_hello
11
- puts "RAR!"
23
+ def parse_options!(argv)
24
+ optparse = OptionParser.new(argv) do |opts|
25
+ opts.banner = "Usage: rename.rb [options]\n\nCommand options:"
26
+
27
+ @options[:regex] = @matcher
28
+
29
+ opts.on('-i', '--input-dir DIRECTORY', 'Specifies where to look for episodes to rename.') do |dir_name|
30
+ @options[:directory] = dir_name
31
+ end
32
+
33
+ opts.on('-n', '--name NAME', 'What to rename the series to. Title is derived from series if not given.') do |name|
34
+ @options[:series_name] = sanitize_user_string(name)
35
+ end
36
+
37
+ @options[:confirm] = false
38
+ 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|
39
+ @options[:confirm] = true
40
+ end
41
+
42
+ opts.on('-r', '--regexp MATCHER', 'Alternative regexp matcher.') do |regex|
43
+ @options[:regex] = regex.to_regexp
44
+ end
45
+
46
+ @options[:verbose] = false
47
+ opts.on('-v', '--verbose', 'Verbose output. Automatically selected for pretend runs.') do |regex|
48
+ @options[:verbose] = true
49
+ end
50
+
51
+ opts.on('-h', '--help', 'Display this to screen.') do
52
+ puts opts
53
+ exit
54
+ end
55
+ end
56
+
57
+ optparse.parse!
58
+ end
59
+
60
+ def sort!
61
+ @directory = @options[:directory]
62
+ @series_name = @options[:series_name]
63
+
64
+ if directory.nil? or !Dir.exists?(directory)
65
+ puts "ERROR: Directory '#{directory}' does not exist. Run with -h for more information."
66
+ exit
67
+ end
68
+
69
+ Dir.chdir(directory)
70
+ filenames = Dir.glob("**/*.{#{@extensions.join(",")}}")
71
+
72
+ puts "------------------------------------------------------------------------"
73
+ puts "Finding all files with the following extensions: #{@extensions.join(", ")}"
74
+ puts ""
75
+ puts "Directory: #{directory}"
76
+ puts "Found #{filenames.size} files."
77
+ puts "------------------------------------------------------------------------"
78
+ puts ""
79
+ puts ""
80
+ 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]"
82
+ puts ""
83
+ puts ""
84
+ puts "Starting to rename files using regex #{@options[:regex]}..."
85
+ puts ""
86
+
87
+ filenames.each do |filename|
88
+ if File.file?(filename)
89
+
90
+ if details = parse_filename(filename)
91
+
92
+ old_directory = File.absolute_path(File.dirname(filename))
93
+
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
97
+ else
98
+ target_directory = File.join(old_directory, "Season #{details[:season].to_i}")
99
+ end
100
+
101
+ if details[:series_name]
102
+ renamed_file = "#{details[:series_name]} - S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
103
+ else
104
+ renamed_file = "S#{details[:season]}E#{details[:episode]}#{details[:ext]}"
105
+ end
106
+
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 ""
117
+ end
118
+ else
119
+ puts "ERROR: Skipped file \"#{filename}\" due to invalid file format."
120
+ end
121
+ else
122
+ puts "ERROR: Skipped non-file \"#{filename}\"."
123
+ end
124
+
125
+ end
126
+
127
+ puts ""
128
+ puts "Finished renaming #{filenames.size} files."
129
+
130
+ if !@options[:confirm]
131
+ puts ""
132
+ puts "!!! NOTICE: This was a pretend run, nothing was renamed. !!!"
133
+ puts "To confirm the renaming, run this command again with -c"
134
+ end
135
+ end
136
+
137
+ def parse_filename(filename)
138
+ basename = File.basename(filename)
139
+ extension = File.extname(filename)
140
+
141
+ parts = basename.gsub(/[-._]/i, " ").strip.split(" ")
142
+
143
+ position = season = episode = -1
144
+
145
+ parts.each_with_index do |v, k|
146
+ if v =~ @matcher
147
+ season = "%02d" % ($1 || $3 || $5).to_i
148
+ episode = "%02d" % ($2 || $4 || $6).to_i
149
+ position = k
150
+
151
+ break if position != -1
152
+ end
153
+ end
154
+
155
+ if position != -1
156
+ # No title if episode numbers are first
157
+ if position == 0
158
+ title = (@series_name) ? @series_name : nil
159
+ else
160
+ title = parts[0..position-1].map{|p| p.capitalize}.join(" ")
161
+ end
162
+ details = {:basename => basename, :ext => extension, :season => season, :episode => episode}
163
+ details[:series_name] = (@series_name.nil?) ? title : @series_name
164
+ details
165
+ else
166
+ false
167
+ end
168
+ end
169
+
170
+ def sanitize_user_string(str)
171
+ return false if str.empty?
172
+ str = str.gsub(/[^\s\w.\-]/, '')
173
+ str
12
174
  end
13
175
 
14
176
  end
15
177
  end
16
178
 
17
- Reorganise::Reorganise.new
179
+ Reorganise::Reorganise.new(ARGV)
180
+
181
+
@@ -1,3 +1,3 @@
1
1
  module Reorganise
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/reorganise.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.summary = "Sorts and renames your series."
12
12
  s.description = "Sorts and renames your series."
13
13
 
14
- s.rubyforge_project = "resort"
14
+ s.rubyforge_project = "reorganise"
15
15
 
16
16
 
17
17
  s.files = `git ls-files`.split("\n")
@@ -19,6 +19,6 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_runtime_dependency "tvdb_party"
22
+ s.add_runtime_dependency "to_regexp"
23
23
 
24
24
  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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,8 +12,8 @@ cert_chain: []
12
12
  date: 2012-01-08 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: tvdb_party
16
- requirement: &76836100 !ruby/object:Gem::Requirement
15
+ name: to_regexp
16
+ requirement: &83562250 !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: *76836100
24
+ version_requirements: *83562250
25
25
  description: Sorts and renames your series.
26
26
  email:
27
27
  - justin.cossutti@gmail.com
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubyforge_project: resort
59
+ rubyforge_project: reorganise
60
60
  rubygems_version: 1.8.10
61
61
  signing_key:
62
62
  specification_version: 3