You are not logged in.

#1 2009-08-18 19:06:01

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

nmpc - a simple netcat-powered MPD client

Help text:

nmpc - a simple netcat-powered MPD client.

Usage: nmpc <command> [<arg1> ... <argn>]

MPD Commands:
  These are just sent on to MPD as-is.

     Playback Commands
         next
         previous
         play
         pause
         stop
         crossfade
         setvol <int vol>

     Track Commands
         find <string type> <string what>
         list <metadata arg1> [<metadata arg2> <search term>]

     Playlist Commands
         clear
         shuffle
         add <string>
         delete <int song>
         save <string playlist name>
         rm <string playlist name>

     Database Commands
         update

NMPC Commands:
  These are more advanced commands handled by nmpc
     toggle              - toggle the play/pause state
     raw                 - send $2 as a command to MPD
     info                - information about MPD.
     playing             - information about the current playlist.
     playlist <playlist> - clear the playlist and load the specified playlist.
     playlists           - list the playlists.

Source code:

#!/bin/zsh
 
MPDSERVER="localhost"
MPDPORT="6600"
 
alias -g clean='sed "/^OK.*$/d"'
 
function help()
{
    echo "nmpc - a simple netcat-powered MPD client."
    echo
    echo "Usage: nmpc <command> [<arg1> ... <argn>]"
    echo
    echo "MPD Commands:"
    echo "  These are just sent on to MPD as-is."
    echo
    echo "     Playback Commands"
    echo "         clear"
    echo "         next"
    echo "         previous"
    echo "         play"
    echo "         pause"
    echo "         stop"
    echo "         shuffle"
    echo "         crossfade"
    echo "         setvol <int vol>"
    echo
    echo "     Track Commands"
    echo "         find <string type> <string what>"
    echo "         list <metadata arg1> [<metadata arg2> <search term>]"
    echo
    echo "     Playlist Commands"
    echo "         add <string>"
    echo "         delete <int song>"
    echo "         save <string playlist name>"
    echo "         rm <string playlist name>"
    echo
    echo "     Database Commands"
    echo "         update"
    echo
    echo "NMPC Commands:"
    echo "  These are more advanced commands handled by nmpc"
    echo "     toggle              - toggle the play/pause state"
    echo "     raw                 - send \$2 as a command to MPD"
    echo "     info                - information about MPD."
    echo "     playing             - information about the current playlist."
    echo "     playlist <playlist> - clear the playlist and load the specified playlist."
    echo "     playlists           - list the playlists."
}
 
function line()
{
    echo "$1" | grep -i "$2: " | sed "s/$2: //i"
}
 
function rawcmd()
{
    echo "$1\nclose" | netcat $MPDSERVER $MPDPORT
}
 
function mpdfunc()
{
    arglist=$1
 
    [ "$2" != "" ] &&                   arglist="$arglist $2"
    [ "$3" != "" ] && [ "$4" = "" ]  && arglist="$arglist \"$3\""
    [ "$3" != "" ] && [ "$4" != "" ] && arglist="$arglist $3 \"$4\""
 
    [ "$1" = "add" ] || [ "$1" = "save" ] || [ "$1" = "rm" ] && arglist="$1 \"$2\""
 
    rawcmd "$arglist" | clean
}
 
function nmpcfunc()
{
    case $1 in
        "toggle")
            if mpdfunc status | grep -q "state: play"; then
                rawcmd pause
            else
                rawcmd play
            fi;;
        "raw")
            rawcmd "$2";;
        "info")
            info=`rawcmd stats | clean`
 
            playtime=`line $info db_playtime`
            artists=`line $info artists`
            albums=`line $info albums`
            songs=`line $info songs`
 
            runtime=`printf "%02d:%02d:%02d\n" $[$playtime / 3600 ] $[$playtime / 60 % 60] $[$playtime % 60]`
            echo "$artists artists, $albums albums, $songs songs, $runtime play time.";;
        "playing")
            info=`rawcmd status | clean`
            sinfo=`rawcmd currentsong | clean`
 
            track=`line $sinfo title`
            artist=`line $sinfo artist`
            album=`line $sinfo album`
            state=`line $info state`
 
            volume=`line $info volume`
            repeat="on";    [ "`line $info repeat`" = "0" ] && repeat="off"
            random="on";    [ "`line $info random`" = "0" ] && random="off"
            crossfade="on"; [ "`line $info xfade`"  = "0" ] && crossfade="off"
            
            echo "$track, by $artist in $album [$state]"
            echo "$volume% volume, repeat $repeat, random $random, crossfade $crossfade";;
        "playlist")
            rawcmd clear | clean
            rawcmd "playlist \"$2\"" | clean;;
        "playlists")
            info=`rawcmd lsinfo | clean`
            echo $info | grep "playlist: " | sed "s/playlist: //";;
        esac
}
 
function error()
{
    echo "Unknown command: $*"
}
 
case $1 in
    "-h" | "--help")
        help;;
 
    "clear" | "next" | "previous" | "play" | "pause" | "stop" | "update" | "find" | "list" | "add" | "delete" | "save" | "rm" | "shuffle" | "crossfade" | "setvol")
        mpdfunc $*;;
 
    "toggle" | "raw" | "info" | "playing" | "playlist" | "playlists")
        nmpcfunc $*;;
 
    "")
        nmpcfunc playing;;
 
    *)
        error $*;;
esac

Latest source: http://github.com/Barrucadu/home/blob/master/bin/nmpc

Enjoy big_smile

I now use it exclusively instead of mpc (fun projects arise when bored).

Last edited by Barrucadu (2009-08-18 19:08:36)

Offline

#2 2016-03-26 19:17:46

Falstaff
Member
Registered: 2008-11-03
Posts: 80

Re: nmpc - a simple netcat-powered MPD client

I've managed to figure out who to use netcat by looking at your script. Good work!

Last edited by Falstaff (2016-03-26 19:18:02)

Offline

#3 2016-03-26 23:41:33

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: nmpc - a simple netcat-powered MPD client

Havent't tried it yet, but looking at the code it seems that it's not possible to use mpd servers with passwords set?


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB