summaryrefslogtreecommitdiff
path: root/omptagger
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-10-16 21:35:38 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2010-10-16 21:35:38 -0700
commit389bebecc054b0013d2a08dd5dc4aeddf37da631 (patch)
treed37bee307a2ea597c3eb9387838ca5448e777636 /omptagger
parent56fbaf8931a71ac0bddeeed50d59626989020008 (diff)
downloadomptagger-389bebecc054b0013d2a08dd5dc4aeddf37da631.tar.gz
omptagger-389bebecc054b0013d2a08dd5dc4aeddf37da631.tar.xz
Near-complete generate method.
Diffstat (limited to 'omptagger')
-rwxr-xr-xomptagger58
1 files changed, 49 insertions, 9 deletions
diff --git a/omptagger b/omptagger
index 739072e..e28ea2f 100755
--- a/omptagger
+++ b/omptagger
@@ -93,7 +93,7 @@ Schemes:
* Schemes must be specified prior to actions that use them.
* Example: omptagger --scheme '%a - %t' --generate
* %a - Artist %* - Wildcard
- %b - Album %% - Per cent sign
+ %b - Album
%d - Date
%n - Track
%t - Title
@@ -111,6 +111,14 @@ Tag Generation:
end
end
+class Array
+ def longest_element_length
+ self.inject(0) do |longest, key|
+ key.length > longest ? key.length : longest
+ end
+ end
+end
+
class Hash
def longest_key_length
self.keys.inject(0) do |longest, key|
@@ -209,6 +217,34 @@ class Metadata
@write = true
end
+ def generate(scheme)
+ Output.action('Generating fields')
+
+ regexp = Regexp.escape(scheme)
+ regexp = regexp.gsub(/%([#{keys.keys.join}]|\\\*)/, '([^/]*?)')
+ regexp = /#{regexp}\Z/
+
+ fields = scheme.scan(regexp).flatten
+ values = @filename.chomp(File.extname(@filename)).scan(regexp).flatten
+
+ unless fields.length == values.length
+ Output.info('Filename does not match naming scheme.')
+ throw :next
+ end
+
+ fields.collect! do |field|
+ keys[field[1,1]]
+ end
+
+ longest = fields.compact.longest_element_length
+
+ fields.zip(values).each do |field, value|
+ unless field.nil?
+ Output.field(field, value, longest - field.length)
+ end
+ end
+ end
+
private
def next_if_no_fields_set
@@ -234,6 +270,14 @@ class Metadata
end
class VorbisComment < Metadata
+ def keys
+ Hash['a' => 'ARTIST',
+ 'b' => 'ALBUM',
+ 'd' => 'DATE',
+ 'n' => 'TRACKNUMBER',
+ 't' => 'TITLE']
+ end
+
def save
return unless @write
@@ -254,6 +298,10 @@ class VorbisComment < Metadata
private
+ def read
+ metadata.fieldListMap.hash
+ end
+
def valid_field?(field)
valid = (32..125).collect do |character|
character.chr
@@ -279,10 +327,6 @@ class FLAC < VorbisComment
def metadata
@file.xiphComment
end
-
- def read
- metadata.fieldListMap.hash
- end
end
class Vorbis < VorbisComment
@@ -295,10 +339,6 @@ class Vorbis < VorbisComment
def metadata
@file.tag
end
-
- def read
- metadata.fieldListMap.hash
- end
end
class MP3 < Metadata