3.1.4 mppopm Filter Commands

Having accumulated play counts, ratings &c in one’s sticker database one can now begin acting on that information. For instance, here is a bash shell script to add all un-rated songs to the play queue:

exec 3<>/dev/tcp/localhost/6600;
head -1 <&3

declare -A rated
last_file=""
echo "sticker find song \"\" unwoundstack.com:rating" >&3
while true; do
    read -r -u 3 line
    case $line in
        OK*)
            break;;
        ACK*)
            echo "$line" >&2;
            exit 1;;
        file:*)
            last_file="${line:6}";;
        sticker:*)
            rated["$last_file"]="${line:33}";
            last_file="";;
        *)
            echo "error: $line" >&2;
            exit 1;;
    esac
done

exec 4<>/dev/tcp/localhost/6600;
head -1 <&4
echo "command_list_begin" >&4

echo "listall" >&3
while true; do
    read -r -u 3 line
    case $line in
        OK*)
            break;;
        ACK*)
            echo "$line" >&2;
            exit 1;;
        file:*)
            if test -z "${rated[${line:6}]}" -o "${rated[${line:6}]}" == "0"; then
                echo "adding ${line:6}"
                echo "add \"${line:6}\"" >&4
            fi;;
    esac
done

exec 3<&-
echo "command_list_end" >&4
head -1 <&4
exec 4<&-

In search of a more ergonomic way to make use of this information, mpdpopm now has support for filters. Filters are an MPD feature for searching one’s song database; one can select songs for various operations by filters such as “(artist =~ ’pogues’)”, for instance. mpdpopm’s contribution is to extend the MPD filter syntax to add ratings, play counts & last-played timestamps (“rating”, “playcount” & “lastplayed” resp.) to the filter syntax.

The first two filter-related commands added to mppopm are searchadd and findadd. Each accepts a filter as an argument and adds the selected songs to the the play queue. The only difference is that searchadd works case-insensitively & findadd respects case.

Consequently, the shell script above can now be replaced by the command mppopm findadd ``(rating == 0)''. NB. filter syntax requires textual values to be quoted, so you’ll need to escape them for the shell. For instance, to select songs with an artist like “pogues”, the filter syntax would be (artist =~ “pogues”), so to specify this on the command line, you would have to escape the double quotes: mppopm searchadd ``(artist =~ \"pogues\")''.

See below for a detailed description of filter syntax & quoting issues.