Sha256: f5425c8451141d36877f3a552c80931758e037d79652ce6e8fad02cd552091d7

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

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

    def initialize(content)
      @content = content
    end

    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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
music_sanitizer-1.0.0 lib/music_sanitizer/processor.rb