id3rename 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README +20 -0
  2. data/Rakefile +239 -0
  3. data/bin/id3rename +101 -0
  4. data/id3rename.gemspec +26 -0
  5. metadata +57 -0
data/README ADDED
@@ -0,0 +1,20 @@
1
+ NAME
2
+ id3rename
3
+
4
+ SYNOPSIS
5
+ id3rename paths paths* [options]+
6
+
7
+ DESCRIPTION
8
+ id3rename is a program to do simple renaming of mp3 files
9
+
10
+ PARAMETERS
11
+ paths (-2 -> paths)
12
+ --noop, -n
13
+ do no work, simply show what would be done
14
+ --help, -h
15
+
16
+ EXAMPLES
17
+ . id3rename badly-named.mp3
18
+ . id3rename badly-named.mp3 another-badly-named.mp3
19
+ . id3rename directory/full/of/badly-named.mp3 directory/full/of/another-badly-named.mp3
20
+
data/Rakefile ADDED
@@ -0,0 +1,239 @@
1
+ This.author = "Ara T. Howard"
2
+ This.email = "ara.t.howard@gmail.com"
3
+ This.homepage = "http://github.com/ahoward/#{ This.lib }/tree/master"
4
+ This.rubyforge_project = 'codeforpeople'
5
+
6
+ task :default do
7
+ puts(Rake::Task.tasks.map{|task| task.name} - ['default'])
8
+ end
9
+
10
+ task :spec do
11
+ require 'spec/rake/spectask'
12
+ Spec::Rake::SpecTask.new do |t|
13
+ t.spec_files = FileList['spec/*_spec.rb']
14
+ end
15
+ end
16
+
17
+ task :gemspec do
18
+ ignore_extensions = 'git', 'svn', 'tmp', /sw./, 'bak', 'gem'
19
+ ignore_directories = 'pkg'
20
+ ignore_files = 'test/log'
21
+
22
+ shiteless =
23
+ lambda do |list|
24
+ list.delete_if do |entry|
25
+ next unless test(?e, entry)
26
+ extension = File.basename(entry).split(%r/[.]/).last
27
+ ignore_extensions.any?{|ext| ext === extension}
28
+ end
29
+ list.delete_if do |entry|
30
+ next unless test(?d, entry)
31
+ dirname = File.expand_path(entry)
32
+ ignore_directories.any?{|dir| File.expand_path(dir) == dirname}
33
+ end
34
+ list.delete_if do |entry|
35
+ next unless test(?f, entry)
36
+ filename = File.expand_path(entry)
37
+ ignore_files.any?{|file| File.expand_path(file) == filename}
38
+ end
39
+ end
40
+
41
+ lib = This.lib
42
+ version = This.version
43
+ files = shiteless[Dir::glob("**/**")]
44
+ executables = shiteless[Dir::glob("bin/*")].map{|exe| File.basename(exe)}
45
+ has_rdoc = true #File.exist?('doc')
46
+ test_files = "test/#{ lib }.rb" if File.file?("test/#{ lib }.rb")
47
+
48
+ extensions = This.extensions
49
+ if extensions.nil?
50
+ %w( Makefile configure extconf.rb ).each do |ext|
51
+ extensions << ext if File.exists?(ext)
52
+ end
53
+ end
54
+ extensions = [extensions].flatten.compact
55
+
56
+ template =
57
+ if test(?e, 'gemspec.erb')
58
+ Template{ IO.read('gemspec.erb') }
59
+ else
60
+ Template {
61
+ <<-__
62
+ ## #{ lib }.gemspec
63
+ #
64
+
65
+ Gem::Specification::new do |spec|
66
+ spec.name = #{ lib.inspect }
67
+ spec.version = #{ version.inspect }
68
+ spec.platform = Gem::Platform::RUBY
69
+ spec.summary = #{ lib.inspect }
70
+
71
+ spec.files = #{ files.inspect }
72
+ spec.executables = #{ executables.inspect }
73
+
74
+ spec.require_path = "lib"
75
+
76
+ spec.has_rdoc = #{ has_rdoc.inspect }
77
+ spec.test_files = #{ test_files.inspect }
78
+ #spec.add_dependency 'lib', '>= version'
79
+ #spec.add_dependency 'fattr'
80
+
81
+ spec.extensions.push(*#{ extensions.inspect })
82
+
83
+ spec.rubyforge_project = #{ This.rubyforge_project.inspect }
84
+ spec.author = #{ This.author.inspect }
85
+ spec.email = #{ This.email.inspect }
86
+ spec.homepage = #{ This.homepage.inspect }
87
+ end
88
+ __
89
+ }
90
+ end
91
+
92
+ open("#{ lib }.gemspec", "w"){|fd| fd.puts template}
93
+ This.gemspec = "#{ lib }.gemspec"
94
+ end
95
+
96
+ task :gem => [:clean, :gemspec] do
97
+ Fu.mkdir_p This.pkgdir
98
+ before = Dir['*.gem']
99
+ cmd = "gem build #{ This.gemspec }"
100
+ `#{ cmd }`
101
+ after = Dir['*.gem']
102
+ gem = ((after - before).first || after.first) or abort('no gem!')
103
+ Fu.mv gem, This.pkgdir
104
+ This.gem = File.basename(gem)
105
+ end
106
+
107
+ task :readme do
108
+ samples = ''
109
+ prompt = '~ > '
110
+ lib = This.lib
111
+ version = This.version
112
+
113
+ Dir['sample*/*'].sort.each do |sample|
114
+ samples << "\n" << " <========< #{ sample } >========>" << "\n\n"
115
+
116
+ cmd = "cat #{ sample }"
117
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
118
+ samples << Util.indent(`#{ cmd }`, 4) << "\n"
119
+
120
+ cmd = "ruby #{ sample }"
121
+ samples << Util.indent(prompt + cmd, 2) << "\n\n"
122
+
123
+ cmd = "ruby -e'STDOUT.sync=true; exec %(ruby -Ilib #{ sample })'"
124
+ samples << Util.indent(`#{ cmd } 2>&1`, 4) << "\n"
125
+ end
126
+
127
+ template =
128
+ if test(?e, 'readme.erb')
129
+ Template{ IO.read('readme.erb') }
130
+ else
131
+ Template {
132
+ <<-__
133
+ NAME
134
+ #{ lib }
135
+
136
+ DESCRIPTION
137
+
138
+ INSTALL
139
+ gem install #{ lib }
140
+
141
+ SAMPLES
142
+ #{ samples }
143
+ __
144
+ }
145
+ end
146
+
147
+ open("README", "w"){|fd| fd.puts template}
148
+ end
149
+
150
+
151
+ task :clean do
152
+ Dir[File.join(This.pkgdir, '**/**')].each{|entry| Fu.rm_rf(entry)}
153
+ end
154
+
155
+
156
+ task :release => [:clean, :gemspec, :gem] do
157
+ gems = Dir[File.join(This.pkgdir, '*.gem')].flatten
158
+ raise "which one? : #{ gems.inspect }" if gems.size > 1
159
+ raise "no gems?" if gems.size < 1
160
+ cmd = "rubyforge login && rubyforge add_release #{ This.rubyforge_project } #{ This.lib } #{ This.version } #{ This.pkgdir }/#{ This.gem }"
161
+ puts cmd
162
+ system cmd
163
+ end
164
+
165
+
166
+
167
+
168
+
169
+ BEGIN {
170
+ $VERBOSE = nil
171
+
172
+ require 'ostruct'
173
+ require 'erb'
174
+ require 'fileutils'
175
+
176
+ Fu = FileUtils
177
+
178
+ This = OpenStruct.new
179
+
180
+ This.file = File.expand_path(__FILE__)
181
+ This.dir = File.dirname(This.file)
182
+ This.pkgdir = File.join(This.dir, 'pkg')
183
+
184
+ lib = ENV['LIB']
185
+ unless lib
186
+ lib = File.basename(Dir.pwd)
187
+ end
188
+ This.lib = lib
189
+
190
+ version = ENV['VERSION']
191
+ unless version
192
+ name = lib.capitalize
193
+ library = "./lib/#{ lib }"
194
+ program = "./bin/#{ lib }"
195
+ if test(?e, library)
196
+ require library
197
+ version = eval(name).send(:version)
198
+ elsif test(?e, program)
199
+ version = `#{ program } --version`.strip
200
+ end
201
+ abort('no version') if(version.nil? or version.empty?)
202
+ end
203
+ This.version = version
204
+
205
+ abort('no lib') unless This.lib
206
+ abort('no version') unless This.version
207
+
208
+ module Util
209
+ def indent(s, n = 2)
210
+ s = unindent(s)
211
+ ws = ' ' * n
212
+ s.gsub(%r/^/, ws)
213
+ end
214
+
215
+ def unindent(s)
216
+ indent = nil
217
+ s.each do |line|
218
+ next if line =~ %r/^\s*$/
219
+ indent = line[%r/^\s*/] and break
220
+ end
221
+ indent ? s.gsub(%r/^#{ indent }/, "") : s
222
+ end
223
+ extend self
224
+ end
225
+
226
+ class Template
227
+ def initialize(&block)
228
+ @block = block
229
+ @template = block.call.to_s
230
+ end
231
+ def expand(b=nil)
232
+ ERB.new(Util.unindent(@template)).result(b||@block)
233
+ end
234
+ alias_method 'to_s', 'expand'
235
+ end
236
+ def Template(*args, &block) Template.new(*args, &block) end
237
+
238
+ Dir.chdir(This.dir)
239
+ }
data/bin/id3rename ADDED
@@ -0,0 +1,101 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ require 'rubygems'
6
+ require 'main' # sudo gem install main
7
+ require 'mp3info' # sudo gem install ruby-mp3info
8
+
9
+ Main {
10
+ Version = '1.0.2'
11
+
12
+ description <<-__
13
+ id3rename is a program to do simple renaming of mp3 files
14
+ __
15
+
16
+ examples <<-__
17
+ . id3rename badly-named.mp3
18
+ . id3rename badly-named.mp3 another-badly-named.mp3
19
+ . id3rename directory/full/of/badly-named.mp3 directory/full/of/another-badly-named.mp3
20
+ __
21
+
22
+ argument('paths'){
23
+ arity -1
24
+ }
25
+
26
+ option('--noop', '-n'){
27
+ description 'do no work, simply show what would be done'
28
+ }
29
+
30
+ option('--version'){
31
+ description 'print version and exit'
32
+ }
33
+
34
+ def run
35
+ if params['version'].given?
36
+ puts Version
37
+ exit
38
+ end
39
+ @paths = params['paths'].values
40
+ @noop = params['noop'].given?
41
+ finding_files_in(*@paths) do |file|
42
+ id3rename(file)
43
+ end
44
+ end
45
+
46
+ def id3rename(path)
47
+ src = path
48
+ dst = renamed(path)
49
+ unless src==dst
50
+ y src => dst
51
+ unless @noop
52
+ File.rename src, dst
53
+ end
54
+ end
55
+ end
56
+
57
+ def finding_files_in(*paths, &block)
58
+ paths.flatten!
59
+ paths.compact!
60
+ paths.map! do |path|
61
+ path = File.expand_path(path)
62
+ test(?f, path) ? path : Dir[ File.join(path, '**', '**') ]
63
+ end
64
+ paths.flatten!
65
+ paths.map!{|path| Pathname.new(path).realpath.to_s}
66
+ paths.sort!
67
+ paths.uniq!
68
+ @seen = {}
69
+ paths.each do |path|
70
+ next if @seen[path]
71
+ @seen[path] = true
72
+ block.call(path) if(test(?f, path) and path =~ %r/\.mp3$/)
73
+ end
74
+ end
75
+
76
+ def renamed(path)
77
+ dirname, basename = File.split File.expand_path(path)
78
+ ext = basename.split(%r/\./).last
79
+ parts =
80
+ Mp3Info.open(path) do |mp3|
81
+ [
82
+ mp3.tag.artist,
83
+ #mp3.tag.album,
84
+ #mp3.tag.tracknum,
85
+ mp3.tag.title,
86
+ ]
87
+ end
88
+ parts.compact!
89
+ parts.delete_if{|part| part.empty?}
90
+ basename = parts.map{|part| slug_for(part)}.join('_') + '.' + ext
91
+ File.join(dirname, basename)
92
+ end
93
+
94
+ def slug_for(*args)
95
+ string = args.flatten.compact.join('-')
96
+ words = string.to_s.scan(%r/\w+/)
97
+ words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
98
+ words.delete_if{|word| word.nil? or word.strip.empty?}
99
+ words.join('-').downcase
100
+ end
101
+ }
data/id3rename.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ ## id3rename.gemspec
2
+ #
3
+
4
+ Gem::Specification::new do |spec|
5
+ spec.name = "id3rename"
6
+ spec.version = "1.0.2"
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.summary = "id3rename"
9
+
10
+ spec.files = ["bin", "bin/id3rename", "id3rename.gemspec", "Rakefile", "README"]
11
+ spec.executables = ["id3rename"]
12
+
13
+ spec.require_path = "lib"
14
+
15
+ spec.has_rdoc = true
16
+ spec.test_files = nil
17
+ #spec.add_dependency 'lib', '>= version'
18
+ #spec.add_dependency 'fattr'
19
+
20
+ spec.extensions.push(*[])
21
+
22
+ spec.rubyforge_project = "codeforpeople"
23
+ spec.author = "Ara T. Howard"
24
+ spec.email = "ara.t.howard@gmail.com"
25
+ spec.homepage = "http://github.com/ahoward/id3rename/tree/master"
26
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: id3rename
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ara T. Howard
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-23 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ara.t.howard@gmail.com
18
+ executables:
19
+ - id3rename
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - bin
26
+ - bin/id3rename
27
+ - id3rename.gemspec
28
+ - Rakefile
29
+ - README
30
+ has_rdoc: true
31
+ homepage: http://github.com/ahoward/id3rename/tree/master
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: codeforpeople
52
+ rubygems_version: 1.3.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: id3rename
56
+ test_files: []
57
+