Skip to content

Instantly share code, notes, and snippets.

@J0J0
Created September 22, 2012 16:56
Show Gist options
  • Save J0J0/3766755 to your computer and use it in GitHub Desktop.
Save J0J0/3766755 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#encoding: utf-8
#v0.2
if RUBY_VERSION.start_with?("1.8")
$KCODE = "u"
require 'rubygems'
end
require 'taglib'
require 'uri'
require 'htmlentities'
FMT = "[%4i] %s - %s"
TYPES = ["Mp3", "Ogg", "Flac"]
TYPES_MULTICASE = TYPES.map(&:downcase) + TYPES.map(&:upcase) + TYPES
TYPES_GLOB = TYPES_MULTICASE.map{|ext| "*.#{ext}" }
TYPES_GLOB_REC = TYPES_MULTICASE.map{|ext| "**/*.#{ext}" }
input = `kdialog --textinputbox "Verzeichnisse in die Textbox ziehen:" ""`
exit(1) if $?.exitstatus != 0
dirs = input.split("\n").map{|s| URI.decode(s[%r{file://(.+)$},1]) }
if dirs.uniq.size != dirs.size
exit(0) unless system('kdialog --warningyesno "Gleiche Verzeichnisse als Eingabe bekommen.\n'\
'Mehrfachnennungen werden ignoriert. Fortsetzen?"')
end
changes = []
longest = -1
dirs.uniq.each do |dir|
Dir.chdir(dir)
match = Dir[*TYPES_GLOB].first || Dir[*TYPES_GLOB_REC].first
unless match
$stderr.puts "Konnte keine Musikdatei in #{dir} (oder Unterverzeichnissen) finden."
next
end
TagLib::FileRef.open(match){|f|
tags = f.tag
changes << [ dir, FMT % [tags.year, tags.artist, tags.album] ]
longest = [longest,changes.last.last.size].max
}
end
str = "<table>"
longest = -1
coder = HTMLEntities.new
changes.each{|dold,dnew|
str << "<tr><td><pre>" << coder.encode(dold.ljust(longest)) <<
"</pre></td><td>&nbsp;&nbsp;→&nbsp;&nbsp;</td><td><pre>" <<
coder.encode(dnew) << "</pre></td></tr>\n"
}
str << "</table>"
if system("kdialog --yesno \"<b>Bitte bestätigen:</b>\n#{str}\"")
changes.each{|dold,dnew|
File.rename(dold, dold[%r{^(.+/)[^/]*$},1] << dnew)
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment