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
128 stars 18 forks source link

[SCRIPT] Manual Channel Changer #49

Closed irodimus closed 6 years ago

irodimus commented 6 years ago

This is a really rough script for manually switching a channel by including it as an argument. It has no error check so if you give it an invalid channel, it will break. For the most part it works for me but I figured I throw it out there if you wanted to test and use it or improve it. This is heavily based off of the channelup/down.sh scripts.

To run, use './manual.sh [channel]'

#!/bin/bash

# file: manual.sh

#----
# Simple script to change to specific channel given - triggering start / stop. 
#----

#---- 
# To Use:
# Run script by including the channel you'd like to run as an argument: ex. ./manual.sh 2, ./manual.sh 9
#
# Configure something (a tv remote or alexa) to trigger this script. Make sure you move this script just 
# outside of the pseudo-channel directories:
# -------------------
# -channels/
# --pseudo-channel_1/
# ---startstop.sh
# --pseudo-channel_2/
# ---startstop.sh
# --pseudo-channel_3/
# ---startstop.sh
# --manual.sh <--- on the same level as the 3 channels. 
#----

# Make sure that each channel dir ends with a "_" + an incrementing number as seen above.

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

SCRIPT_TO_EXECUTE='startstop.sh'

OUTPUT_PREV_CHANNEL_PATH=.

OUTPUT_PREV_CHANNEL_FILE=".prevplaying"

CHANNEL_DIR_INCREMENT_SYMBOL="_"

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

FIRST_RUN=false

# 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"'*' -printf "%P\n") )

# If the previous channel txt file doesn't exist already create it (first run?)
if [ ! -e "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE" ]; then

    #FIRST_RUN_NUM=$((${#CHANNEL_DIR_ARR[@]}))
    echo 1 > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE"

    echo "First run: $FIRST_RUN_NUM"

    FIRST_RUN=true

fi

# If this script see's there are multiple channels, 
# then read file, get prevchannel and nextchannel, and trigger next channel:
if [ "${#CHANNEL_DIR_ARR[@]}" -gt 1 ]; then

    NEXT_CHANNEL=$1

    NEXT_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$NEXT_CHANNEL" -printf "%P\n") )

    PREV_CHANNEL_FOUND=false

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

    PREV_CHANNEL=$(<$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE)

    PREV_CHANNEL_DIR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL""$PREV_CHANNEL" -printf "%P\n") )

    echo "+++++ It looks like the previous channel was: $PREV_CHANNEL"

    echo "+++++ The next channel is: $NEXT_CHANNEL"

    # Write next channel to previous channel file to reference later
    echo "$NEXT_CHANNEL"  > "$OUTPUT_PREV_CHANNEL_PATH/$OUTPUT_PREV_CHANNEL_FILE"

    # Finally let's trigger the startstop script in both the previous channel and the next channel dirs.
    # This will stop the previous channels playback & trigger the next channels playback

    if [ "$FIRST_RUN" = false ]; then
        cd "$OUTPUT_PREV_CHANNEL_PATH"/"$PREV_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
        cd ../"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"
    else

        cd "$OUTPUT_PREV_CHANNEL_PATH"/"$NEXT_CHANNEL_DIR" && ./"$SCRIPT_TO_EXECUTE"

    fi

    sleep 1

fi

exit 0

I had to change how the script found the channels as it would only show 9. I have 12 channels setup and it would skip channels 10 and up. This was my case so I'm not sure if you have run into this.

justinemter commented 6 years ago

Thanks irodimus. I don't actually have multi-channel setup so cannot test this at the moment, @MoeFwacky

Also, what did you change so I can update the old channelup/channeldown scripts to work with more than 9 channels? Unless I misunderstood you.

irodimus commented 6 years ago

I changed

# 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") )

to

# 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"'*' -printf "%P\n") )

I replaced '[[:digit:]]' with '*' since it was still matching directories that included the "_". I'm not sure why the original code did not find any channel directories past 1-9. I had to make the same change to the updatechannels.sh script to update the databases.

Is there plans to restructure the project to have PseudoChannel use one database when setting up multiple channels instead of as many channels as there are? It would make for easier and faster updating.

Edit: After reading your most recent commits to the README file, I now understand why each channel currently requires it's own DB.

justinemter commented 6 years ago

This is great! Thanks for that. I'll make the updates to the other scripts as well.

justinemter commented 6 years ago

Is there plans to restructure the project to have PseudoChannel use one database when setting up multiple channels instead of as many channels as there are? It would make for easier and faster updating.

Yes, that was Marks original idea, but setting it up this way was just out-of-the-box more or less. I did write a bash script that you can program to a remote button that will run -u on every channel. So if you add new media to your Plex lib, you can trigger that script and it will just asynchronously step through and update all 12 databases for your 12 channels... not ideal as it will take a long time especially on a pi but it should do the trick. That script needs some testing but I remember it worked on the few channels I tested it with...

irodimus commented 6 years ago

I also hacked up a quick script to also generate the daily schedules for all the channels whether PseudoChannel is running or not. Before I was manually updating the daily schedules for each channel. I just set it to run as cron job at 12am but my schedules start at 1:30am in case there is something playing on a channel. I'm still testing it as I just put it together quickly.

justinemter commented 6 years ago

I also hacked up a quick script to also generate the daily schedules for all the channels whether PseudoChannel is running or not. Before I was manually updating the daily schedules for each channel. I just set it to run as cron job at 12am but my schedules start at 1:30am in case there is something playing on a channel. I'm still testing it as I just put it together quickly.

Awesome! Please share when it's ready. I'll add you to the creds :)

MoeFwacky commented 6 years ago

I wasn't able to get this working on my system.

To start I got this error manual.sh: 43: manual.sh: Syntax error: "(" unexpected

My first thought was that this was a syntax difference between mac and linux, so I removed the outer parenthesis set and it continued until I got this error:

manual.sh: 63: manual.sh: Syntax error: "(" unexpected (expecting "fi")

So I did the same there and for this one

manual.sh: 71: manual.sh: Syntax error: "(" unexpected (expecting "fi")

Which got me down to this:

+++++ There are 50 channels detected.
+++++ It looks like the previous channel was:
+++++ The next channel is: 1
manual.sh: 84: manual.sh: ./startstop.sh: not found
manual.sh: 85: cd: can't cd to ../pseudo-channel_1
irodimus commented 6 years ago

I wasn't able to get this working on my system.

To start I got this error manual.sh: 43: manual.sh: Syntax error: "(" unexpected

Line 43 looks like the line I had to change for my use case. Does it work if you replace it with this

CHANNEL_DIR_ARR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL"'[[:digit:]]*' -printf "%P\n") )

?

MoeFwacky commented 6 years ago

Comes back with this one again

manual.sh: 43: manual.sh: Syntax error: "(" unexpected

irodimus commented 6 years ago

Ok last change that I can think of.

CHANNEL_DIR_ARR=( $(find . -maxdepth 1 -type d -name '*'"$CHANNEL_DIR_INCREMENT_SYMBOL"'[[:digit:]]' -printf "%P\n") )

The only thing different is removing the '*' after [[:digit:]].

MoeFwacky commented 6 years ago

Same error again

irodimus commented 6 years ago

That is weird because, that line is copy and pasted from the other channelup.sh and channeldown.sh files. The error might be elsewhere. There's nothing in there I can see that is mac or linux specific.

MoeFwacky commented 6 years ago

Figured it out. This needs to be run as ./manual.sh 1, instead of sh manual.sh 1

Now, for some reason, out of 3 channels, if I pick channel 1, I get 3, if I pick 3, I get 1, if I pick 2, I get all three at the same time.

Here's the output on channel 2

pi@controller:~/channels $ sudo ./manual.sh 2
+++++ There are 3 channels detected.
+++++ It looks like the previous channel was: 1
+++++ The next channel is: 2
Started PseudoChannel.py -m -r @ Process: 8541
Created running.pid file in . dir
Started PseudoChannel.py -m -r @ Process: 8545
Created running.pid file in . dir
pi@controller:~/channels $ pgrep python
8364
8541
8545
irodimus commented 6 years ago

That sounds like the issue with the channelup and channeldown you had at one point. Does deleting the .prevplaying and running.pid files help? I had that happen once and that fixed it for me.

MoeFwacky commented 6 years ago

Yeah looks like that did the trick, now to map all of these to my remote and add more channels.

irodimus commented 6 years ago

Great! I'm thinking of getting one of those remotes since now I can change to specific channels. Is it possible to press the numbers and then press 'Enter' if there is button and call this script, sending the number value to it?

MoeFwacky commented 6 years ago

My remote recognizes as a USB keyboard and I use openbox to configure the remote from there. In theory, you could configure it for any behavior you can configure a keyboard to do.

justinemter commented 6 years ago

I think @irodimus nailed this one. Cheers!