#!/usr/bin/ruby # Simple moodbar file management utility by Joe Rabinoff # This is also my first ever ruby script so bear with me $moodbar_folder = "#{ENV['HOME']}/.kde/share/apps/amarok/moods/" def usage() print "This is the moodbar file management utility\n" print "\n" print "Usage is:\n" print " moodbar_util.rb -rename Rename mood files" \ + " from any old naming scheme to the current one\n" \ + " (not applicable when mood" \ + " files are stored with music)\n" print " moodbar_util.rb -calcall Calculate all un-calculated" \ + " mood files\n" exit(1) end def check_amarok_running() result = `dcop 'amarok*'`.split("\n") return (result.length > 0) end def get_devices() result = `dcop amarok collection query 'SELECT id, lastmountpoint FROM devices;'`.split("\n") ret = Hash.new() result.each_index do |i| ret[result[i].to_i] = result[i+1] if i % 2 == 0 end return ret end def mood_name(url, deviceid) ret = url.gsub(/\//, ',') return deviceid.to_s + "," + ret end def rename() print "Renaming mood files from outdated naming schemes...\n" # As far as I can tell, the best way to do this is just select all # tracks, check the old possible moodbar names, and rename them tracks = `dcop amarok collection query 'SELECT url, deviceid FROM tags;'`.split("\n") devices = get_devices 0.step(tracks.length-1, 2) do |i| url = tracks[i].sub(/\.[^\.]*$/, '.mood') deviceid = tracks[i+1].to_i path = url.sub(/\./, '') if devices.has_key?(deviceid) path = devices[deviceid] + path end moodfile = $moodbar_folder + mood_name(url, deviceid) moodfile1 = $moodbar_folder + path.gsub(/\//, ',') next if FileTest.exists?(moodfile) if FileTest.exists?(moodfile1) system("mv", "-f", moodfile1, moodfile) print "Moved ", moodfile1, " to ", moodfile, "\n" end end end def calcall() print "Calculating all nonexisting moodbars...\n\n" tracks = `dcop amarok collection query 'SELECT url, deviceid FROM tags;'`.split("\n") devices = get_devices withMusic = (`dcop amarok script readConfig MoodsWithMusic`.chomp == "true") 0.step(tracks.length-1, 2) do |i| url = tracks[i] deviceid = tracks[i+1].to_i moodfile = "" songpath = url.sub(/\./, '') if devices.has_key?(deviceid) songpath = devices[deviceid] + songpath end if withMusic moodfile = songpath.sub(/\.[^\.]*$/, '.mood') moodfile = File.dirname(moodfile) + "/." + File.basename(moodfile) else moodfile = $moodbar_folder \ + mood_name(url.sub(/\.[^\.]*$/, '.mood'), deviceid) end next if FileTest.exists?(moodfile) && FileTest.size(moodfile) > 0 next unless FileTest.exists?(songpath) print "Saving moodbar for ", songpath, "\n" print " to file ", moodfile, "\n" system("moodbar", "-o", moodfile, songpath) print "\n" end end $mode = ARGV[0] if !check_amarok_running print "Amarok is not running! Please start Amarok and re-run " \ "this script.\n" end if $mode == "-rename" rename elsif $mode == "-calcall" calcall else usage end