Technology
 

Client talk:Mpc

From Music Player Daemon Community Wiki

#!/usr/bin/perl
# 
# only for "display"
# needs TERM:READKEY
# apt-get install libterm-readkey-perl
# 
# 


use Term::ReadKey;


my $last = "";
my $sek = 1;
#my $nextsong = false; - develop

sub display_status
	{
	my ($sek, $nextsong) = @_;
	
	ReadMode 4; 
	while (not defined ($key = ReadKey(-1)))
		{
		print `clear`;
		print `mpc status`;
		#print "$nextsong";
		sleep $sek;
	       }
	print `clear`;
	ReadMode 1; 
	}


	print `clear`;

while(<STDIN>)
	{
	print `clear`;

	s/\r?\n//;


	if ((/^display$/)) { display_status (1,false); $last ="status"; next; }
	
	if ((/^n$/))
		{
		$last = "next";
		print `mpc next`;
		next;
		}
	
	if ((/^p$/))
		{
		$last = "prev";
		print `mpc prev`;
		next;
		}
		
	if ((/^find$/) or (/^f$/))
		{
		$last = "status";
		print "Enter String: ";
		$string = <STDIN>;
		$string =~ s/\r?\n//;
		@words = split(/\ /,$string);
		$cmd = "mpc playlist ";
		foreach $word (@words)
			{
			$cmd = "$cmd | grep $word ";
			}
		print `$cmd`;	
		next;
		}

		
	if ((/^quit$/) or (/^exit$/) or (/^q$/))
		{
		print `clear`;
		exit 0;
		}

	if (/^$/)
		{
		print `mpc $last`;
		next;
		}

	$last = $_;
	print `mpc $_`; 
	}


i use it - and love it- heil_eris (at) gmx (dot) net

#!/usr/bin/python

# By: Kuukunen
# Remember to change the plcache file & maybe other stuff
# Usage:
# search [regexp1] [regexp2] ...
# it will search for songs on the playlist matching all python regular expression patterns (note you might have to escape the strings)

plcache="/plcache" #change!
statefile="/var/lib/mpd/state"
port="6600"
host="localhost"

import sys
import os
import re

load = False
try:
    a=os.popen("echo -e \"status\\nclose\" | nc "+host+" "+port+" | sed -e '/^OK MPD/d;/^OK$/d'|head -n 4|tail -n 1|cut -c 11-")
    statedate = os.stat(statefile)[9]
    cachedate = os.stat(plcache)[9]
    plversion=int(a.read())
    f=open(plcache)
    cacheversion = int(f.readline())
    f.close()
except:
    load = True

if load or cachedate < statedate or cacheversion != plversion:
    os.system("echo "+str(plversion)+" > "+plcache)
    os.system("mpc --format '%time% &[%album%: ][[%artist% - ]%title%]|%time% %file%' playlist >> "+plcache)

f=open(plcache)
a = f.readlines()
for arg in sys.argv[1:]:
    i = 0
    reg = re.compile(arg, re.IGNORECASE)
    a = [i for i in a if reg.search(i)]

for i in a:
    print i[:-1]

For searching playlist items. No guarantees on working on your system.

#!/bin/bash

# By: Kuukunen
# Usage:
# enqueue number [place]
# Enqueue playlist item #number to play next
# or with the optional place argument, play Nth, (default 1)

a=`mpc | tail -n 2 | head -n 1 | cut -b12- | cut -d'/' -f 1`
if [ $2 ]
then
    let "b = $a + $2"
else
    let "b = $a + 1"
fi
if [ $a -gt $1 ]
then
    let "b = $b - 1"
fi
mpc move $1 $b
mpc

For enqueuing items on the playlist.
Kuukunen@irc.freenode.net