Skip to content

Instantly share code, notes, and snippets.

@msuarz
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save msuarz/9425e136663fdfd39072 to your computer and use it in GitHub Desktop.

Select an option

Save msuarz/9425e136663fdfd39072 to your computer and use it in GitHub Desktop.

Revisions

  1. msuarz renamed this gist Aug 14, 2014. 1 changed file with 19 additions and 3 deletions.
    22 changes: 19 additions & 3 deletions cleanup.rb → music.rb
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,14 @@
    require 'fileutils'

    task :clean, :folder do |t, args|
    MusicCleaner.new(args.to_hash).cleanup
    Music.new(args.to_hash).cleanup
    end

    class MusicCleaner
    task :biggie, :folder do |t, args|
    Music.new(args.to_hash).show_by_size
    end

    class Music

    attr_reader :root

    @@ -21,6 +25,11 @@ def cleanup
    show_weird_names
    end

    def show_by_size
    sort(biggie).each { |x| show x }
    sort(smalls).each { |x| show x }
    end

    def cleanup_files
    puts 'no dirty files found!!' if dirty_files.empty?

    @@ -54,11 +63,18 @@ def all; Dir["#{root}/**/*"] end
    def all_dirs; all.select { |x| File.directory? x } end
    def empty_dirs; all_dirs.select { |x| Dir.entries(x).length == 2 } end

    def dir_sizes; @dir_sizes ||= `du #{root} --max-depth=1 -h`.lines end
    def biggie; dir_sizes.select { |x| x.split[0].end_with? 'G' } end
    def smalls; dir_sizes.select { |x| x.split[0].end_with? 'M' } end
    def size dir; dir.split[0].to_f end
    def sort dirs; dirs.sort_by { |x| -size(x) } end
    def show dir; puts dir.gsub /#{root}\//, '' end

    def dirty_files; @dirty_files ||= (all_files - mp3s) end
    def all_files; all.reject { |x| File.directory? x } end
    def mp3s; Dir["#{root}/**/*.mp3"] end

    WEIRD = /[^\p{Alnum}\p{Space}\.\/\-&'\[\]\(\)\!\+_,#%^\$\@~\?\{\};`\=ëöüéóâñè]/
    WEIRD = /[^\p{Alnum}\p{Space}\.\/\-&'\[\]\(\)\!\+_,#%^\$\@~\?\{\};`\=ëöüáéíóâñè]/
    def weird_names; all.select { |x| is_weird? x } end
    def is_weird? x; x.match WEIRD end
    def fix_name x; x.gsub WEIRD, '' end
  2. msuarz revised this gist Aug 14, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions cleanup.rb
    Original file line number Diff line number Diff line change
    @@ -52,9 +52,7 @@ def show_weird_names

    def all; Dir["#{root}/**/*"] end
    def all_dirs; all.select { |x| File.directory? x } end
    def empty_dirs
    all_dirs.select { |x| Dir.entries(x).length == 2 }
    end
    def empty_dirs; all_dirs.select { |x| Dir.entries(x).length == 2 } end

    def dirty_files; @dirty_files ||= (all_files - mp3s) end
    def all_files; all.reject { |x| File.directory? x } end
  3. msuarz created this gist Aug 14, 2014.
    68 changes: 68 additions & 0 deletions cleanup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #encoding: IBM437

    require 'rake'
    require 'fileutils'

    task :clean, :folder do |t, args|
    MusicCleaner.new(args.to_hash).cleanup
    end

    class MusicCleaner

    attr_reader :root

    def initialize opts
    @root = opts[:folder]
    end

    def cleanup
    cleanup_files
    cleanup_empty_folders
    show_weird_names
    end

    def cleanup_files
    puts 'no dirty files found!!' if dirty_files.empty?

    dirty_files.each do |crap|
    FileUtils.remove_file crap, true
    puts "bye bye #{crap}"
    end
    end

    def cleanup_empty_folders
    puts 'no empty folders found!!' if empty_dirs.empty?

    until empty_dirs.empty?
    empty_dirs.each do |x|
    FileUtils.remove_dir x, true
    puts "bye bye #{x}"
    end
    end
    end

    def show_weird_names
    puts 'no weird names found!!' if weird_names.empty?

    weird_names.each do |x|
    puts x
    puts fix_name x
    end
    end

    def all; Dir["#{root}/**/*"] end
    def all_dirs; all.select { |x| File.directory? x } end
    def empty_dirs
    all_dirs.select { |x| Dir.entries(x).length == 2 }
    end

    def dirty_files; @dirty_files ||= (all_files - mp3s) end
    def all_files; all.reject { |x| File.directory? x } end
    def mp3s; Dir["#{root}/**/*.mp3"] end

    WEIRD = /[^\p{Alnum}\p{Space}\.\/\-&'\[\]\(\)\!\+_,#%^\$\@~\?\{\};`\=ëöüéóâñè]/
    def weird_names; all.select { |x| is_weird? x } end
    def is_weird? x; x.match WEIRD end
    def fix_name x; x.gsub WEIRD, '' end

    end