You are not logged in.

#1 2009-11-15 18:15:24

Andrwe
Member
From: Leipzig/Germany
Registered: 2009-06-17
Posts: 322
Website

mpchpl: a totally senseless script

Hi,

I was working on a script for about 2 weeks.
It is called mpchpl (mpc-based handler of playlists).
And how the name implies it is designed to handle playlists of mpd using mpd means you can ex-/import all playlists you have.
Today I've finished it and after finishing I've read that there is a directory called playlists defined in mpd.conf where all playlists are saved in .m3u files.
Means you can easily copy the lists from one system to an other.

So my script is really and absolutely, totaly senseless and 2 weeks of hard thinking and working went down the river of time without having any effect.

But because I don't want to lose the time for nothing I'm posting the script here.
If you want to you can leave some notes concering the programming about it here so I can improve my bash skills.

#!/bin/bash

# Copyright (C) 2009 Andrwe Lord Weber
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Version: 0.1.0

# conditions
#                         filename =~ title-tag
#                         e.g.: filename = foo.bar & title-tag = foo => true
#                                     filename = foo.bar & title-tag = bla => false
#
#                         structure of music directory of import system should be the same as export system

HOST="localhost"
PORT=6600
DEBUG="/dev/null"

# Change the following values only if you know what you do
tmp_lists=/tmp/lists.$$
cur_playlist=$$
cur_song=/tmp/song.$$
implist="/tmp/imp-list.$$"


function get_playlists
{
    echo "$(mpc lsplaylists)" > ${tmp_lists}
    while read line
    do
        list="$(echo ${line} | sed 's/ /!/g')"
        lists="${lists} ${list}"
    done <    ${tmp_lists}
    rm ${tmp_lists}
}

function create_songlist
{
    echo -e "# file genereated by mpchpl\n" > "${1}"
    echo -e "Generating songlist and saving to ${1} ...\n"
    for list in ${lists}
    do
        list="$(echo ${list} | sed 's/!/ /g')"
        echo -e "[${list}]\n" >> "${1}"
        mpc clear > "${DEBUG}"
        echo -e "... saving ${list} ..."
        mpc load "${list}" > "${DEBUG}"
        mpc playlist > "${tmp_lists}"
        while read line
        do
            if [[ ${line} =~ ^https?://.* ]]
            then
                echo -e "${line}" >> "${1}"
            else
                mpc listall | grep "${line}" >> "${1}"
            fi
        done < "${tmp_lists}"
        mpc clear > "${DEBUG}"
        echo -e "\n" >> "${1}"
        echo -e "... ${list} saved ...\n"
        echo ";;;" >> "${1}"
    done
    echo -e "... songlist generated.\n"
}

function read_songlist
{
    [ ! -r "${1}" ] && echo "File ${1} is not readable." && load_state && exit 1
    get_playlists
    save_state
    cp "${1}" "${implist}"
    for exp_list in ${lists}
    do
        exp_list="$(echo ${exp_list} | sed 's/!/ /g')"
        sed -i '/\['"${exp_list}"'\]/,/^;;;$/ D' "${implist}"
    done
    sed -i '/^#.*/d;/^$/d' "${implist}"
    while read line
    do
        if [[ ${line} =~ ^\[.*\]$ ]]
        then
            [ "${playlist}" ] && mpc save "${playlist}" > "${DEBUG}"
            mpc clear > "${DEBUG}"
            playlist="$(echo ${line} | sed 's/\[//1;s/\]$//g')"
            continue
        fi
        [ "${line}" -a "${line}" != ";;;" ] && mpc add "${line}"
    done < ${implist}
    [ "${playlist}" ] && mpc save "${playlist}" > "${DEBUG}"
    rm "${implist}"
}

function save_state
{
    mpc stop > "${DEBUG}"
    echo -e "\nSaving current playlist."
    mpc save ${cur_playlist}
    echo -e "Saving current playing song.\n"
    echo $(mpc | grep '] \#' | sed -r 's/.*#//g' | sed -r 's/\/.*//') > ${cur_song}
}

function load_state
{
    mpc clear > "${DEBUG}"
    echo -e "\nLoading current playlist."
    mpc load ${cur_playlist} > "${DEBUG}"
    mpc rm ${cur_playlist} > "${DEBUG}"
    echo -e "Starting current song."
    mpc play $(cat ${cur_song}) > "${DEBUG}"
    rm ${cur_song}
}

function ping_host()
{
    if [[ "${HOST}" =~ [^localhost|127\.0\.0\.\d+] ]]
    then
        [ $(ping -c 4 ${HOST} | grep "packet loss" | awk -F ',' '{print $3}' | sed 's/%.*//g' | sed 's/ //g') = 100 ] && echo -e "Destination ${HOST} unreachable. Please check the value of \${HOST}." && exit 1
        if [ $(ping -c 4 ${HOST} | grep "packet loss" | awk -F ',' '{print $3}' | sed 's/%.*//g' | sed 's/ //g') > 0 ]
        then
            echo -e "The connection to ${HOST} isn't stable. Should the script continue? (y|n)\n"
            read choice
            [ "$choice" != "y" ] && exit 1
        fi
    fi
}

case "${1}" in
    export)
        [ ! "${2}" ] && echo "  You need to specify the file in which the playlists were saved." && exit 1
        ping_host
        export MPD_HOST="${HOST}"
        export MPD_PORT="${PORT}"
        get_playlists
        save_state
        create_songlist "${2}"
        load_state
        ;;
    import)
        [ ! "${2}" ] && echo "  You need to specify the file in which the playlists should be saved." && exit 1
        ping_host
        export MPD_HOST="${HOST}"
        export MPD_PORT="${PORT}"
        read_songlist "${2}"
        load_state
        mpc update > "${DEBUG}"
        ;;
    *)
        echo -e "This is a script to handle playlists of MPD using MPC and bash.\n     Usage:\n         "${0}" import|export file"
esac

Offline

Board footer

Powered by FluxBB