Skip to content

Instantly share code, notes, and snippets.

@naelstrof
Forked from infertux/mpd-delete-current-song.rb
Last active December 22, 2015 11:48
Show Gist options
  • Save naelstrof/6467950 to your computer and use it in GitHub Desktop.
Save naelstrof/6467950 to your computer and use it in GitHub Desktop.
#!/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'
# Most people don't have an actual server to host MPD on, defaults to localhost configuration.
HOST = 'localhost'
PORT = 6600
# Leave blank if you have no password
PASSWORD = ''
LIBRARY = '~/Music'
TRASH = '~/Rejects'
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)
# This'll keep the script from locking up if MPD doesn't require a password.
if PASSWORD.length > 0
s.send "password #{PASSWORD}"
end
song = s.send 'currentsong'
filename = id = ''
# Requires each_line, otherwise ruby 2.0.0_p247 errors out.
song.each_line 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?
# Use File.expand_path() to interpret ~'s.
FileUtils.move File.expand_path("#{LIBRARY}/#{filename}"), File.expand_path("#{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