Class: FileName

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

Instance Method Summary collapse

Constructor Details

#initializeFileName

Returns a new instance of FileName.



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

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_fixed_file_name(file_path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/what_cd/sanitize_plugins/file_name.rb', line 37

def get_fixed_file_name(file_path)
  # Return the original file_path by default
  new_file_path = file_path

  Mp3Info.open(file_path) do |mp3|
    if mp3.tag.title and mp3.tag.artist and mp3.tag.tracknum
      @log.debug "Reconstructing file name for #{file_path}"
      title = mp3.tag.title
      artist = mp3.tag.artist
      tracknum = mp3.tag.tracknum.to_s
      # Append a 0 to tracknum if necessary
      if tracknum.length == 1
        tracknum = "0" + tracknum
      end

      # Append a period to the trackname
      tracknum = tracknum + "."

      # Handle any remix parenthesis
      title_parts = title.split('-')
      if title_parts.length > 1
        title_part = title_parts[0].strip
        remix_part = title_parts[1].strip
        
        title = title_part + " (#{remix_part})"
      end

      new_file_path = "#{tracknum} #{artist} - #{title}" + File.extname(file_path)
    end
  end

  return new_file_path
end

#sanitize(context) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/what_cd/sanitize_plugins/file_name.rb', line 20

def sanitize(context)
  path = context[:path]
  Dir.chdir(path) { Dir["*.mp3"] }.each do |f|
    file_name = f
    file_path = path + file_name

    new_file_name = get_fixed_file_name(file_path)

    if new_file_name != file_name
      new_file_path = path + new_file_name
      File.rename(file_path, new_file_path)
    end
  end

  return context
end