Class: MusicSanitizer::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/music_sanitizer/processor.rb

Constant Summary collapse

IGNORE =
YAML.load_file(File.join(MusicSanitizer.root, "lists/ignore.yml"))
EXCLUDE =
YAML.load_file(File.join(MusicSanitizer.root, "lists/exclude.yml"))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Processor

Returns a new instance of Processor.



6
7
8
# File 'lib/music_sanitizer/processor.rb', line 6

def initialize(content)
  @content = content
end

Class Method Details

.ignore?(this, compare = nil) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/music_sanitizer/processor.rb', line 58

def self.ignore?(this, compare = nil)
  IGNORE.
    reject{ |value| compare.to_s.match(/#{value}/i) }.
    map{ |value| this.match(/#{value}/i) }.any?
rescue ArgumentError
  return false
end

Instance Method Details

#processObject



10
11
12
13
14
15
16
17
18
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
53
54
55
56
# File 'lib/music_sanitizer/processor.rb', line 10

def process
  string = @content.strip

  EXCLUDE.each do |exclude|
    string = string.gsub(/[^ ]*#{exclude}.*$/i, "")
  end

  # Sub
  string = string.gsub(/ยด/, "'").gsub(/`/, "'")

  # Song - A "abc def" => Song - A
  # Song - A [B + C] => Song - A
  # Song A B.mp3 => Song A B
  # 10. Song => Song
  [
    /\.mp3$/, 
    /\[[^\]]*\]/, 
    /".*"/,  
    /(\s+|^)'.*'(\s+|$)/
  ].each do |reg|
    string = string.gsub(reg, " ").strip
  end
  
  [
    /\(.*?(\)|$)/m, 
    /[^a-z0-9]feat(.*?)\s*[^\s]+/i, 
    /[-]+/, 
    /[\s]+/m, 
    /\_/
  ].each do |reg|
    string = string.gsub(reg, " ").strip
  end

  # Split
  # A ft. B => A
  string = string.split(/ft\.\s+/i).first
  # A + B => A
  string = string.split(/\s+\+\s+/).first

  EXCLUDE.each do |exclude|
    string = string.gsub(/#{exclude}.*$/i, "")
  end
  
  string.gsub(/\A\s|\s\z/, "").gsub(/\s+/, " ").strip.downcase
rescue Encoding::CompatibilityError, Encoding::UndefinedConversionError, ArgumentError
  return string
end