Professional Documents
Culture Documents
Create Playlist For Button
Create Playlist For Button
/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
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