Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#!

/bin/bash
################################################################
# CycleRandomPlaylists.sh - Randomly cycle through playlists #
# #
# The playlists are queued to be played once. If something is #
# already playing, the script will exit immediately. #
################################################################
shopt -s nullglob
cd ${MEDIADIR}/playlists

# The only configuration expected by the user is to set the


# PLAYLISTS variable here at the top of the script. Here are
# examples on ways to set that variable:

# File glob to include all playlists


############ VARIABLES ##################
# Change these values to suit
BEGIN=$(date --date="16:00" +%s)
END=$(date --date="20:00" +%s)
NOW=$(date +%s)
# 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="240"
PLAYLISTS=("One.json" "Two.json" "Three.json" "Four.json" "Five.json" "Six.json"
"Seven.json" "Eight.json" "Nine.json" "Ten.json" "Eleven.json" "Twelve.json"
"Thirteen.json" "Fourteen.json")
STATICPLAYLIST="Static During"

#PLAYLISTS=("all.json" "all1.json" "all2.json" "all3.json")


# f4 - playlist title including .fseq
# f7 - playlist mp3, if blank just sequence running
#-f check if exists
#-s check if empty
function fpp_status() {
echo "Checking fpp status..."
STATUS=$(fpp -s | cut -d',' -f2)
RAW_STATUS=$(fpp -s | cut -d',' -f4)

# 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\n' "$(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\n' "$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, 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" -le "$NOW" -a "$NOW" -le "$END" ]; then
while ! fpp_status; do
echo "Checking what playlist is playing now..."
echo "Current playing: $RAW_STATUS"
echo "static list: $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
fpp -P "${next_playlist}"
echo "play list playing now is ${next_playlist}"
exit 0
else
echo "Must be playing a Playlist"
exit 0
fi
done
else
echo "Not within time scope"
fi

You might also like