Class: DirName

Inherits:
Object
  • Object
show all
Includes:
SanitizePlugin
Defined in:
lib/what_cd/sanitize_plugins/dir_name.rb

Instance Method Summary collapse

Constructor Details

#initializeDirName

Returns a new instance of DirName.



9
10
11
12
13
14
15
16
17
# File 'lib/what_cd/sanitize_plugins/dir_name.rb', line 9

def initialize
  @log = Logging.logger[self]
  @log.appenders = Logging.appenders.stdout
  if $verbose
    @log.level = :debug
  else
    @log.level = :info
  end
end

Instance Method Details

#get_first_mp3_path(path) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/what_cd/sanitize_plugins/dir_name.rb', line 54

def get_first_mp3_path(path)
  Dir.entries(path).each do |f|
    if !File.directory?(f) and File.extname(f) == ".mp3"
      file_path = path + f
      return file_path
    end
  end

  return nil
end

#get_quality_string(bitrate, vbr) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/what_cd/sanitize_plugins/dir_name.rb', line 65

def get_quality_string(bitrate, vbr)
  if vbr
    if bitrate > 230 
      return "[MP3 V0]"
    elsif bitrate > 190
      return "[MP3 V2]"
    end
  else
    return "[MP3 #{bitrate}]"
  end
end

#sanitize(context) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/what_cd/sanitize_plugins/dir_name.rb', line 19

def sanitize(context)
  path = context[:path]
  # To get release info we need to look at an mp3
  file_path = get_first_mp3_path(path)

  if file_path
    @log.debug "Opening file_path #{file_path}"
    Mp3Info.open(file_path) do |mp3|
      if mp3.tag.album
        new_dir = mp3.tag.album
        
        # Add year if available
        if mp3.tag.year 
          new_dir = new_dir + " [#{mp3.tag.year}]"
        end

        quality = get_quality_string(mp3.bitrate, mp3.vbr)
        new_dir = new_dir + " #{quality}"

        parts = path.split("/")
        parts[-1] = new_dir
        new_path = parts.join("/")
        # Add trailing '/' that was removed from splitting
        new_path << '/'
        if new_dir != path.chomp("/")
          @log.debug "Renaming directory to #{new_dir}"
          File.rename(path, new_path)
          context[:path] = new_path
        end
      end
    end
  end
  return context
end