CreatePlaylistForButton.sh

CreatePlaylistForButton.sh

Copy everything below into a text editor and save it as CreatePlaylistForButton.sh 
You can use any editor, If you download Brackets or Pheonix code editor you will be able to edit the .sh file easier.

#!/bin/bash
###################################################################################
# CreatePlaylistForButton.sh - cycle through playlists which have been randomized #
# Only operate during a specific time frame #
# Each day a different list of randomized playlists #
# already playing, the script will play not be interrupted #
###################################################################################
shopt -s nullglob
cd ${MEDIADIR}/playlists
 
############ VARIABLES ##################
# Change these values to suit
BEGIN=$(date --date="10:00" +%s)
END=$(date --date="20:00" +%s)
BUFFER=$(date --date="20:04" +%s)
NOW=$(date +%s)
HOST="http://localhost/api"
# Time to re-randomise the list in minutes set this for longer than show length
# so the next day it will refresh a new list randomised.
# time is in minutes 240 is 4 hours 4 * 60mins
TIME="240"
#PLAYLISTS=("all.json" "all1.json" "all2.json" "all3.json")
PLAYLISTS=("One.json" "Two.json" "Three.json" "Four.json")
# Put name of your during show playlist here
STATICPLAYLIST="Static During"
# Put name of your post show playlist here
STATICPLAYLISTEND="Static Post"
 
############ END VARIABLES ##################
 
function fpp_status() {
  echo "Checking fpp status..."s
    STATUS=$(curl -s "${HOST}"/fppd/status | jq -r '.status_name')
    RAW_STATUS=$(curl -s "${HOST}"/fppd/status | jq -r '.current_playlist.playlist')
  # Check that we got something meaningful
  if [ -z "${STATUS}" ]; then
    echo "Error with status value" >&2
    exit 1
  fi
  echo "Fpp raw status is: $RAW_STATUS"
  echo "Fpp status is: $STATUS"
  return "$STATUS"
}
 
swap_files_round()
{
# Get the first line of the txt file
    line=$(head -n 1 ${database})
    echo "this the first line $line"
    # Remove the first line
    printf '%s' "$(sed '1d' ${database})" > ${database}
    # add first line to end of list
    echo "$line">> ${database}
     # print list for testing This can be removed
    while IFS="" read -r p || [ -n "$p" ]; do
        printf '%s' "$p"
    done < $database
}
 
create_list_of_file()
{
# -cmin for change time
# -amin for access time
# -mmin for modification time
# check if file has been updated within a time frame - if not must be
# a new day so start a new randomised list
# time is in minutes 240 is 4 hours 4* 60mins
if test `find "${database}" -amin +"$TIME"`; then
    echo "Database is Old enough to Delete"
    rm -f ${database}
    else
    echo "Database is within Time Limit"
fi
# if [ -s diff.txt ]; then # The file is not-empty.
# if [ ! -s diff.txt ]; then # The file is empty.
# if database is empty then fill with playlist
if [ ! -s ${database} ]; then
    TEMP=$(mktemp)
    #rm -f ${database}
    echo "database doesn't exist, so create a new one"
    # shuffle list and add to txt file
    (ls -1 "${PLAYLISTS[@]}") | shuf > ${TEMP}
    cat ${TEMP} >> ${database}
    rm -f ${TEMP}
    # Get the first line of the txt file
    swap_files_round
 else
    # if file exists get the first record and put it to the back of the queue
    echo "database does exist and is within time limit, do nothing"
    # Get the first line of the txt file
    # this will also be the playlist played next
    swap_files_round
fi
}
 
 
# If the current time is after the start time AND before the end time...
if [[ "$BEGIN" < "$NOW" ]] && [[ "$NOW" < "$BUFFER" ]]; then
# If the time is after your end time check but before the buffer time
    if [[ "$END" < "$NOW" ]] && [[ "$NOW" < "$BUFFER" ]]; then
     while ! fpp_status; do
# If Post sequence is playing do nothing
        if [ "$RAW_STATUS" = "$STATICPLAYLISTEND" ]; then
            echo "Current playing: $RAW_STATUS"
            exit 0
        else
# If Static During sequence is playing swap to Post Playlist
            if [ "$RAW_STATUS" = "$STATICPLAYLIST" ]; then
                echo "play list playing now is ${RAW_STATUS}"
                echo "play list playing now is ${STATICPLAYLISTEND}"
                fpp -p "${STATICPLAYLISTEND}"
                exit 0
 
            else
# If a song playlist is playing do nothing
                echo "Must be playing the last Playlist"
                exit 0
            fi
        fi
        done
    fi
 while ! fpp_status; do
        echo "Checking what playlist is playing now..."
        echo "Current playing: $RAW_STATUS"
        echo "static list in operation is currently: $STATICPLAYLIST"
        if [ "$RAW_STATUS" = "$STATICPLAYLIST" ]; then
            echo "Switch is now active!"
            # Run this once at the beginning in case this is the first time we
            # are running this script. In that case we will populate the database
            database=$(dirname $(mktemp -u))/playlist_db.txt
            create_list_of_file
            # Get our next playlist as the first in our database
            # get next playlist without the file extention
            next_playlist="${line%.*}"
            # Start playlist
            curl "${HOST}"/playlist/"${next_playlist}"/start
            echo "playlist playing now is ${next_playlist}"
            exit 0
        else
            if [ "$RAW_STATUS" = "" ]; then
                echo "Time Is Live but no Static playlist:$STATICPLAYLIST or any playlist is playing"
                exit 0
            else
               exit 0
            fi
        fi
    done
else
    echo "Not within time scope"
fi
Back to blog