justinemter / pseudo-channel

This is a python based cli-app using the python-plex-api to control a plex-client and act like a real TV channel with show scheduling, commercial breaks, movie nights, etc. "Home-Brewed TV Channel(s) for Hackers"
GNU General Public License v3.0
130 stars 18 forks source link

[SCRIPT] Generate all channel schedules #51

Closed irodimus closed 7 years ago

irodimus commented 7 years ago

Here's the script I pieced together to update all the channels. The script loops through the channels and updates all of them unless it finds the running.pid file in the channel directory, in which case it will skip it and move to the next. The channel that is skipped will update according to the value in its channel's pseudo_config.py file so it helps to set this script to run as a cron job at that same time.

#!/bin/bash

# file: generate-channels-daily-schedules.sh

#----
# Simple script to generate the daily schedule for each individual channel. 
# 
#----

#---- 
# To Use:
# This script needs to be setup with a crontab entry that runs everyday at midnight.
#----

#----BEGIN EDITABLE VARS----

SCRIPT_TO_EXECUTE_PLUS_ARGS='PseudoChannel.py -g -m'

CHANNEL_DIR_INCREMENT_SYMBOL="_"

PYTHON_TO_USE="$(which python)"

# If using 'virtualenv' with python, specify the local virtualenv dir.
VIRTUAL_ENV_DIR="env"

#----END EDITABLE VARS-------

# If virtualenv specified & exists, using that version of python instead.
if [ -d "$VIRTUAL_ENV_DIR" ]; then

    PYTHON_TO_USE="$VIRTUAL_ENV_DIR/bin/python"

    echo "+++++ Virtualenv found, using: $VIRTUAL_ENV_DIR/bin/python"

fi

# Scan the dir to see how many channels there are, store them in an arr.
CHANNEL_DIR_ARR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL"'[[:digit:]]*' -printf "%P\n") )

# Scan for channels then loop through each channel and run the daily schedule generator
# unless running.pid is found, then it skips channel and continues
if [ "${#CHANNEL_DIR_ARR[@]}" -gt 0 ]; then

    echo "+++++ There are ${#CHANNEL_DIR_ARR[@]} channels detected."

    for channel in "${CHANNEL_DIR_ARR[@]}"
    do

        if [ ! -e $channel/running.pid ]; then

            cd "$channel" && echo "+++++ Trying to generate daily schedule: $PYTHON_TO_USE $SCRIPT_TO_EXECUTE_PLUS_ARGS"

            $PYTHON_TO_USE $SCRIPT_TO_EXECUTE_PLUS_ARGS

            echo "+++++ Generated: $channel - new schedule."

            cd ..

        else

            echo "+++++ Trying to generate daily schedule: $PYTHON_TO_USE $SCRIPT_TO_EXECUTE_PLUS_ARGS"

            echo "+++++ Skipping: $channel - currently playing"

        fi

        sleep 1

    done

fi

exit 0
justinemter commented 7 years ago

Ok cool, I just re-purposed my old updatechannels.sh to do the job but realize I am not checking for running pid's! I'll incorporate your logic, or if yours works out of the box as you say I'll just use that!

irodimus commented 7 years ago

I figured that already having PseudoChannel.py running, it would already update that channel anyway.

irodimus commented 7 years ago

Oh, and feel free to add, modify, remove, dissect, etc any part of this. I just couldn't wait and thought I'd share it for those who were looking for something :)

justinemter commented 7 years ago

Sounds great! I will absolutely give credit where credits due. Without testing it, it reads great. I may just add your script, put your username up top and add it to the scripts dir. Can you confirm it works with crontab? If so, does the user need to make crontab do anything special... like:

0 0 * * * cd /home/pi/channels/ bash ./generate-channels-daily-schedule.sh > /dev/null 2>&1

...I know often times cron needs to change dirs to make it work right (usually for py scripts). Maybe that's unnecessary?

irodimus commented 7 years ago

Honestly, as a macOS user, there are some small changes I made to the shell scripts for my use case to resolve issues that would occur when I attempted to run them in my setup. I use launchd to set the calendar interval on when to run these scripts and not necessarily crontab so I can't say for sure since PseudoChannel is run on my Mac.

It would probably be better all around to adjust these scripts to run on your setup so others who download the pseudo-channel project don't have to make changes like I did. I tried my best to use put those variables and commands back to the original as possible as seen in your other shell scripts.

Having said that, they should still work fine without any major tweaking.