Sha256: f03d40e1cd29c451e4e89a5f21e8a542ba1f865cedc9c7e76863e81bbf5d0c74

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

require 'json'
require 'colorize'

require 'propre/blacklist'

module Propre
  def self.propify(arg)
    ban_words(ban_chars(sanitize(remove_patterns(arg.downcase)))).split.map(&:capitalize).join(' ')
  end

  def self.find_urls(arg)
    arg = arg.match '((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)'
    arg.to_s
  end

  def self.find_years(arg)
    arg = arg.match '((19|20)\d\d)'
    arg.to_s
  end

  def self.find_episode(arg)
    arg = arg.match '(s\d{1,2}e\d{2,3})'
    arg.to_s
  end

  def self.remove_patterns(arg)
    arg.slice! find_urls(arg)
    arg.slice! find_years(arg)
    arg.slice! find_episode(arg)
    arg
  end

  def self.sanitize(arg)
    arg.gsub('.', ' ').strip
  end

  def self.ban_chars(arg)
    arg.tr('^A-Za-z0-9 ', '').strip
  end

  def self.ban_words(arg)
    arg.split.each do |word|
      Blacklist::WORDS.select do |e|
        arg.slice! word if word.include? e
      end
    end
    arg.squeeze(' ')
  end

  def self.metadata(arg)
    arg = arg.downcase
    {
      year: find_years(arg),
      episode: find_episode(arg).upcase,
      website: find_urls(arg)
    }
  end

  def self.format(newname, metadata)
    metadata[:newname] = newname
    metadata[:year] = "(#{metadata[:year]})" unless metadata[:year].empty?

    format = '%{newname} %{episode} %{year}'
    formated = format % metadata
    formated.strip.squeeze(' ')
  end

  def self.basename_newname_metadata(arg)
    basename = File.basename(arg, File.extname(arg))
    [basename, format(propify(basename), metadata(basename)), metadata(basename)]
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
propre-0.2.2 lib/propre.rb
propre-0.2.1 lib/propre.rb
propre-0.2.0 lib/propre.rb