Skip to content

Instantly share code, notes, and snippets.

@bse666
Last active May 5, 2019 20:45
Show Gist options
  • Select an option

  • Save bse666/66ce6ca8c62a06f9460f1df78e3cec8d to your computer and use it in GitHub Desktop.

Select an option

Save bse666/66ce6ca8c62a06f9460f1df78e3cec8d to your computer and use it in GitHub Desktop.

Revisions

  1. bse666 revised this gist May 5, 2019. 1 changed file with 99 additions and 0 deletions.
    99 changes: 99 additions & 0 deletions mpc_add
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,99 @@
    #!/bin/bash

    # stdin check
    if (( "$#" < 2 )); then
    echo "Usage: `basename $0` HOSTNAME MIN_PLAYLIST_LENGTH PLAYLIST [NEXT_ON_ERROR (y/N)]"
    exit 65
    fi

    # variables
    host=$1
    length=$2
    playlist="$3"
    state_file="$host.state"

    export MPD_HOST=$host

    if [ "$3" ]; then
    echo Playlist: $playlist;
    playlist_items=`mpc -f %file% playlist $playlist | wc -l`
    echo Playlist_items: $playlist_items
    fi

    if [ "$4" ]; then
    cont=${4^^}
    cont=${cont:0:1}
    else
    cont="N"
    fi

    # go go go
    echo "Mpd_Add v.0.6"
    echo "Host: $host / Playlist length: $length"
    echo "Next on error: $cont"

    # loop
    loop () {
    while true; do
    if `mpc version 2>/dev/null | grep -q 'mpd version:'`; then
    setState

    while (( `mpc playlist | grep -c ^` < $length )); do
    if [ "$playlist_items" ]; then
    echo adding songPL;
    addSongPl;
    else
    echo adding normal song;
    addSong;
    fi

    sleep 1
    done

    mpc idle &>/dev/null
    else
    sleep 30
    fi
    done
    }

    # output current status to file
    # does conversion from utf8 to iso-8859-15 for compatability
    #+with samurize on windows
    setState() {
    if `mpc | grep -q '\[playing\]'`; then
    mpc --format "%artist% (%date%) %album% / %track% / %title%" current | iconv -s -c -f UTF-8 -t iso-8859-15 -o $state_file
    elif `mpc | grep -q '\[paused\]'`; then
    echo "pause" > $state_file

    if `mpc | grep -q 'single: on'`; then
    mpc -q single off && mpc -q stop && echo "not playing" > $state_file
    fi
    else
    if [ $cont = "Y" ]; then
    mpc -q play
    else
    echo "not playing" > $state_file
    fi
    fi
    }

    # add one random song from mpd library
    addSongPl () {
    song=`mpc -f %file% playlist $playlist | sed -n $[RANDOM % $playlist_items+1]p`
    echo Song: $song
    if [ "$song" ]; then
    mpc -q add "$song"
    fi
    }
    addSong () {
    song=`mpc -f %file% listall | sed -n $[RANDOM % $(mpc stats | grep Songs | awk '{print $2}')+1]p`
    echo Song: $song
    if [ "$song" ]; then
    mpc -q add "$song"
    fi
    }

    # backgrounds the loop and exits main thread
    loop
    exit 0
  2. bse666 created this gist May 5, 2019.
    10 changes: 10 additions & 0 deletions mpd_add_random.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    #!/bin/sh
    # add one random song from mpd library
    addSong () {
    song=`mpc listall | sed -n $[RANDOM % $(mpc stats | grep Songs | awk '{print $2}')+1]p`

    if [ "$song" ]; then
    mpc -q add "$song"
    fi
    }
    addSong