Skip to content

Instantly share code, notes, and snippets.

@infertux
Created November 5, 2011 19:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save infertux/1341895 to your computer and use it in GitHub Desktop.
Save infertux/1341895 to your computer and use it in GitHub Desktop.
A tiny Ruby wrapper that deletes the song currently played by MPD
#!/usr/bin/env ruby
# Sometimes, you realize you have really rubbish songs in your library for some reason.
# This script allows you to get rid of them just by hitting ./mpd-delete-current-song.rb on your command line.
# It will backup the file to TRASH, then remove it from MPD's library and finally skip to next song.
# https://gist.github.com/1341895
require 'socket'
require 'fileutils'
HOST = '1.2.3.4'
PORT = 6600
PASSWORD = 'MPD-FTW'
LIBRARY = '/srv/music'
TRASH = '/srv/trash'
class MPDSocket < TCPSocket
def open(&args)
s = open args
raise "Can't connect!" unless s.gets.chop == 'OK'
s
end
def send(command)
puts command
buffer = []
while line = gets and not ['OK', 'ACK'].include? line.chop
buffer << line
end
buffer = buffer.join
raise "Did not get OK response for #{buffer}." unless line.chop == 'OK'
buffer
end
end
s = MPDSocket.open(HOST, PORT)
s.send "password #{PASSWORD}"
song = s.send 'currentsong'
filename = id = ''
song.each do |line|
key, value = line.chop.split(': ', 2)
filename = value if key == 'file'
id = value if key == 'Id'
end
raise "WTF" if filename.empty? or id.empty?
FileUtils.move "#{LIBRARY}/#{filename}", "#{TRASH}/"
puts "Trashed #{filename}."
s.send "deleteid #{id}"
puts "Removed ID #{id} form playlist."
s.send "next"
s.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment