From e15213c24ba8e849bdcafcc9331c4debf8448705 Mon Sep 17 00:00:00 2001 From: Justin E Date: Thu, 17 Jan 2019 23:07:12 -0800 Subject: [PATCH 01/20] Added multi-channel scripts: viewer/api plus bash script to generate html files. --- main-dir/generate-html-schedules.sh | 72 +++++++++ main-dir/multi-channel-api.php | 64 ++++++++ main-dir/multi-channel-viewer.php | 217 ++++++++++++++++++++++++++++ 3 files changed, 353 insertions(+) create mode 100644 main-dir/generate-html-schedules.sh create mode 100644 main-dir/multi-channel-api.php create mode 100644 main-dir/multi-channel-viewer.php diff --git a/main-dir/generate-html-schedules.sh b/main-dir/generate-html-schedules.sh new file mode 100644 index 0000000..3440e89 --- /dev/null +++ b/main-dir/generate-html-schedules.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +# file: generate-html-schedules.sh + +#---- +# Simple script to generate the ONLY the html schedule for each individual channel. Useful when using the multi-channel-viewer.php file. +# +#---- + +#---- +# To Use: +# ./generate-html-schedules.sh +#---- + +#----BEGIN EDITABLE VARS---- + +SCRIPT_TO_EXECUTE_PLUS_ARGS='PseudoChannel.py -m' + +OUTPUT_PREV_CHANNEL_PATH=. + +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" | sort -t"$CHANNEL_DIR_INCREMENT_SYMBOL" -n) ) + +# If this script see's there are multiple channels, +# then loop through each channel and run the daily schedule generator +if [ "${#CHANNEL_DIR_ARR[@]}" -gt 1 ]; then + + echo "+++++ There are ${#CHANNEL_DIR_ARR[@]} channels detected." + + for channel in "${CHANNEL_DIR_ARR[@]}" + do + + # If the .pid file exists for this channel, skip it because it updated the html when running. + if [ ! -f "$channel/running.pid" ]; then + + echo "+++++ Trying to generate HTML schedule: ""$PYTHON_TO_USE" ./"$channel"/$SCRIPT_TO_EXECUTE_PLUS_ARGS + + cd "$channel" && $PYTHON_TO_USE $SCRIPT_TO_EXECUTE_PLUS_ARGS + + echo "+++++ Generated: $channel - HTML schedule." + + sleep 1 + + cd ../ + + sleep 1 + + fi + + done + +fi + +exit 0 \ No newline at end of file diff --git a/main-dir/multi-channel-api.php b/main-dir/multi-channel-api.php new file mode 100644 index 0000000..4411aa3 --- /dev/null +++ b/main-dir/multi-channel-api.php @@ -0,0 +1,64 @@ + +$output"; +?> \ No newline at end of file diff --git a/main-dir/multi-channel-viewer.php b/main-dir/multi-channel-viewer.php new file mode 100644 index 0000000..12681e4 --- /dev/null +++ b/main-dir/multi-channel-viewer.php @@ -0,0 +1,217 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $value) + { + echo $key." ".$value."
"; + }*/ + echo " Update Complete"; + } + if (isset($_GET['update'])) { + echo 'Generating new html schedules.'; + updateNowPlayingSchedules(); + } + ?> + +
">
+ +

Multi-Channel

+ +
+ + Generate Now Playing Schedules +
+ +
+ < +

+ > +
+ + + +
+ + + \ No newline at end of file From bda5e2efa61283e9e4bff5d09c3db29e38068213 Mon Sep 17 00:00:00 2001 From: Moe Fwacky Date: Thu, 17 Jan 2019 23:19:46 -0800 Subject: [PATCH 02/20] url change --- main-dir/install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main-dir/install.sh b/main-dir/install.sh index 5c00006..b9a852a 100644 --- a/main-dir/install.sh +++ b/main-dir/install.sh @@ -20,11 +20,11 @@ if [ $# -gt 1 ]; then exit 9999 elif [ "$1" == "develop" ]; then echo "Downloading the develop branch" - SCRIPT_TO_EXECUTE_PLUS_ARGS='git clone https://github.com/mutto233/pseudo-channel . --branch develop' + SCRIPT_TO_EXECUTE_PLUS_ARGS='git clone https://github.com/FakeTV/pseudo-channel . --branch develop' else echo "Downloading the master branch" - SCRIPT_TO_EXECUTE_PLUS_ARGS='git clone https://github.com/mutto233/pseudo-channel . --branch master' + SCRIPT_TO_EXECUTE_PLUS_ARGS='git clone https://github.com/FakeTV/pseudo-channel . --branch master' fi OUTPUT_PREV_CHANNEL_PATH=. From 9bf2a78d11ea94ecfabdd992852103855098f579 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 17 Jan 2019 23:41:42 -0800 Subject: [PATCH 03/20] Update multi-channel-api.php --- main-dir/multi-channel-api.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/main-dir/multi-channel-api.php b/main-dir/multi-channel-api.php index 4411aa3..f2cc139 100644 --- a/main-dir/multi-channel-api.php +++ b/main-dir/multi-channel-api.php @@ -4,6 +4,9 @@ multi-channel-api.php This file triggers bash-scripts based on a url string. Simply navigate your browser or use cURL or some other method to pass in URL params to trigger corresponding scripts. +*Be sure to update the directory on line 35 to your directory path: +chdir('/home/justin/channels/'); + To use: 1) Install PHP sudo apt install php @@ -61,4 +64,4 @@ if ($command == "KEY_PLAY"){ } chdir($old_path); echo "
$output
"; -?> \ No newline at end of file +?> From bd85cfbcc3195390f20046ce78ff2db91f776f9a Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 22 Jan 2019 11:01:41 -0800 Subject: [PATCH 04/20] Fixed the plex client prompt / library name prompts by appending the server-token to the URL. --- main-dir/install.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main-dir/install.sh b/main-dir/install.sh index b9a852a..5c0a3ef 100644 --- a/main-dir/install.sh +++ b/main-dir/install.sh @@ -115,7 +115,7 @@ read -p 'Number of Channels: ' number_of_channels echo "PLEX AUTH TOKEN is $server_token" echo "SELECT the PLEX CLIENT for this install or ENTER one manually" # DISPLAY A LIST OF CONNECTED CLIENTS AND ALLOW THE USER TO SELECT ONE OR ENTER ONE THAT ISN'T DISPLAYED - clientlist=$(xmllint --xpath "//Server/@name" "http://$server_ip:$server_port/clients" | sed "s|name=||g" | sed "s|^ ||g" && echo -e " Other") + clientlist=$(xmllint --xpath "//Server/@name" "http://$server_ip:$server_port/clients/?X-Plex-Token=$server_token" | sed "s|name=||g" | sed "s|^ ||g" && echo -e " Other") eval set $clientlist select ps_client_entry in "$@" do @@ -137,7 +137,7 @@ read -p 'Number of Channels: ' number_of_channels do #read -p 'TV Show Library Name: ' tv_library_entry #echo -n "\"$tv_library_entry\"" >> tv-libraries.temp - library_list_tv=$(xmllint --xpath "//Directory[@type=\"show\"]/@title" "http://$server_ip:$server_port/library/sections" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") + library_list_tv=$(xmllint --xpath "//Directory[@type=\"show\"]/@title" "http://$server_ip:$server_port/library/sections/?X-Plex-Token=$server_token" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") eval set $library_list_tv select tv_library_entry in "$@" do @@ -169,7 +169,7 @@ read -p 'Number of Channels: ' number_of_channels do #read -p 'Movie Library Name: ' movie_library_entry #echo -n "\"$movie_library_entry\"" >> movie-libraries.temp - library_list_movie=$(xmllint --xpath "//Directory[@type=\"movie\"]/@title" "http://$server_ip:$server_port/library/sections" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") + library_list_movie=$(xmllint --xpath "//Directory[@type=\"movie\"]/@title" "http://$server_ip:$server_port/library/sections/?X-Plex-Token=$server_token" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") eval set $library_list_movie select movie_library_entry in "$@" do @@ -212,7 +212,7 @@ read -p 'Number of Channels: ' number_of_channels while [[ "$enter_commercials" == @(Y|y|Yes|yes|YES) ]] do #read -p 'Commercial Library Name: ' commercial_library_entry - library_list_commercial=$(xmllint --xpath "//Directory[@type=\"movie\"]/@title" "http://$server_ip:$server_port/library/sections" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") + library_list_commercial=$(xmllint --xpath "//Directory[@type=\"movie\"]/@title" "http://$server_ip:$server_port/library/sections/?X-Plex-Token=$server_token" | sed "s|title=||g" | sed "s|^ ||g" && echo -e " Other") eval set $library_list_commercial select commercial_library_entry in "$@" do From 7f3727cc1312a9ce11854ebfc9f3fe9fda4021a5 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 23 Jan 2019 11:44:14 -0800 Subject: [PATCH 05/20] Updated the install.sh script to install a cron task in the /etc/cron.d/ dir. --- main-dir/install.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main-dir/install.sh b/main-dir/install.sh index 5c0a3ef..3701019 100644 --- a/main-dir/install.sh +++ b/main-dir/install.sh @@ -251,10 +251,9 @@ read -p 'Number of Channels: ' number_of_channels reset_time_minute="${reset_time_formatted: -2}" reset_time_minute=$(echo $reset_time_minute | sed "-e s|^[0]||") # SET UP CRON JOB TO RUN DAILY RESET - sudo crontab -e & - sudo crontab -l | grep -v 'daily-cron.sh' | crontab - + sudo echo "$reset_time_minute $reset_time_hour * * * root $PWD/daily-cron.sh" >> pseudo-channel + sudo chown root:root pseudo-channel && sudo chmod 600 pseudo-channel && sudo mv pseudo-channel /etc/cron.d/ sudo echo \#\!/bin/bash > ./daily-cron.sh && echo "cd $PWD" >> ./daily-cron.sh && echo "sudo ./generate-channels-daily-schedules.sh" >> ./daily-cron.sh - ( sudo crontab -l ; echo "$reset_time_minute $reset_time_hour * * * $PWD/daily-cron.sh" ) | sudo crontab - #### WRITE VARIABLES TO TOKEN AND CONFIG FILES #### reset_time="\"$reset_time_entry\"" From 72404e6e7966f5af3562929713a0213664444aca Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Thu, 24 Jan 2019 13:06:52 -0800 Subject: [PATCH 06/20] Started a bare-bones README with updated instructions. --- README.md | 179 ++++-------------------------------------------------- 1 file changed, 13 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index a861080..5bd63aa 100644 --- a/README.md +++ b/README.md @@ -1,192 +1,39 @@ # PseudoChannel.py - Your Home-Brewed TV Channels -Update 7/4/2018 - Minor changes made to the updating process -- Your daily_schedules will now be preserved across global updates, so if you update mid-day you won't lose the set of shows you were watching -- Selective updating is now available through the use of -um, -utv, and -uc flags. Any amount of these can be called from PseudoChannel.py OR Global_DatabaseUpdate.py. This will only update your movies, tv, or commercial libraries respectively. - -Update 06/30/2018 - At last, some more updates to this great program! Listed below are some of the fixes: -- Channel naming is now flexible, feel free to use as many as you want! -- Channel updating can now be conducted globally through the use of Global_DatabaseUpdate. No need to run for every single channel! -- Pesky problems regarding midnight, such as scheduling shows and starting a show after midnight, have been fixed. -- A more "client-friendly" installer has been made in update-channels-from-git.sh -IF THIS IS YOUR FIRST TIME: Welcome! Simply download "update-channels-from-git.sh" from the "main-dir" folder, place in the folder you would like to place pseudochannel, and change the variable FIRST_RUN to true. Then run! You'll have your first five channels ready to go. -IF YOU'VE DONE THIS BEFORE: Same idea, but place "update-channels-from-git.sh" from the "main-dir" folder in your local "channels" folder, then run. It will update all necessary files. If you have made any custom files along the way, no need to fear; this installer will not delete your own files that you made. It will also preserve your databases, config files, and tolken file. - -*Update 12/03/2017 - Enabled Support for Custom Library Names - If already running PseudoChannel.py, the local DB must be deleted & then rebuilt running the "-u" option. The TV Shows queue can be preserved by using the "-e" & "-i" options, all together: `python PseudoChannel.py -e -u -i`* - -Joined by the author of [Fake TV](https://medium.com/@Fake.TV), this project aims at tackling one issue: creating a fake tv channel experience with your own media library (movies, tv shows, commercials, etc.). The idea is super simple... when you turn on your TV, rather than hopping straight to Netflix, you can choose to watch your own channel of curated media like a real channel, with randomized movie time blocks, weekend morning cartoons, 90's commercials to fill up gaps and more. We aim to add a ton of neat features but the basic idea is to have something that feels like a real TV channel. That being said it isn't supposed to "pause" nor are you supposed to intervene too much. Just like a real channel you are presented with a channel that you define once and let it go as it advances in series episodes, playing random movies where specified (defined by various parameters like genre, "Kevin Bacon", etc.). Think: weekday movie nights @ 8:00 PM. Or perhaps you want to further specify your weekly Wednesday evening movie be a movie in your Plex library that stars "Will Smith". PseudoChannel is built to interface with the Plex media server. So if you want to have your very own PseudoChannel, you first need to set up your home server using [Plex](https://www.plex.tv/). After that you can come back here to learn how to setup everything else. Although this app runs using Python and the command line, we aim to make all of it as easy as possible to understand for those who are intimidated by this sort of technology. If you have a question, are troubleshooting or have feature ideas, just leave an 'issue' here in this repository. - - -![Generated HTML schedule](http://i.imgur.com/uTGRYIp.png) +![Generated HTML schedule](https://i.imgur.com/SNJ3DXC.gif) ## How to Use: -- The instructions below are all for configuring the **"controller"** device (i.e. a laptop or raspberry pi running linux). This is the device this app runs on to control the Plex client. The **"client"** device should be a Raspberry Pi running Rasplex hooked up to your TV via HDMI - although I'm sure other devices work great too (never tried). +- The instructions below are all for configuring the **"controller"** device (i.e. a laptop or raspberry pi running linux). This is the device this app runs on to control the Plex client. The **"client"** device should be a Raspberry Pi running Rasplex hooked up to your TV via HDMI - although I'm sure other clients work great too. -1. PseudoChannel uses Python 2.7. The recommended method of setting up most python environments is to use [virtualenv](http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/). This keeps all your pip packages / python versions separated on a per project basis. I find this method extremely useful but also somewhat unintuitive, especially at first. Whether you choose to use "virtualenv" to isolate your project environment or not, you can install all the PseudoChannel.py dependencies by running the following command after downloading this repository: +0. Update: ```bash -% pip install -r requirements.txt +% sudo apt update ``` -*You need to run the previous command using `sudo` if not in a virtualenv.* -2. In order to tell PseudoChannel.py how to connect to your Plex server, create an empty file named, `plex_token.py` just outside of the project directory. Within that file add your plex server url / [plex token](https://support.plex.tv/hc/en-us/articles/204059436-Finding-an-authentication-token-X-Plex-Token) like so: +1. Install python-pip & Git: ```bash -token = '' -baseurl = 'http://192.168.1.28:32400' +% sudo apt install python-pip git -y ``` -*This file is important as it tells PseudoChannel.py how/where to connect to your Plex server. It should sit just outside of this /pseudo-channel/ directory.* -3. Edit the `pseudo_config.py` / `the pseudo_schedule.xml` to your liking. You can specify your plex media library names within the `pseudo_config.py` file... If you do not intend on using commercials just set the `useCommercialInjection` flag to `False`. There are a few other experimental options like using Google Calendar rather than an XML. It is an arduous process to initially set up and I've found the XML method to be the easiest method for organizing your schedule - so stick with that for now. Finally, setup your schedule in the xml file. There are some detailed instructions commented at the top of that file. - -4. Run the `PseudoChannel.py` file with the following flags: +2. Create "/channels" dir, change to that directory, and download the install.sh script to the new directory: ```bash -% python PseudoChannel.py -u -xml -g -m -r +% mkdir ./channels && cd ./channels && wget https://raw.githubusercontent.com/FakeTV/pseudo-channel/develop/main-dir/install.sh . ``` -*You can also run `-h` to view all the options. Use `-c` to find the name(s) of your Plex client(s) to add to the config.* -- The `-u` flag will prepare & update (& create if not exists) the local `pseudo-channel.db`, you only need to run this once in the beginning or later when you have added new media to your Plex libraries. -- The `-xml` flag will update the newly created local db with your schedule from the xml file - you should run this everytime you make changes to the xml. -- The `-g` file will generate the daily schedule (for today) based on the xml. This is useful for the first run or testing (or manually advancing the daily queue forward). Running this flag say, 15 times will advance the play queue forward 15 days. It is automatically run every night at midnight to generate the daily schedule. -- The `-m` flag makes both the .html/.xml files and starts a simple html web server in the `./schedules` directory. -- Finally, the `-r` flag will run the app, checking the time / triggering the playstate of any media that is scheduled. It will also update the daily schedule when the clock hits 11.59 (or whatever time you've configured in the config file). If you see errors, check your entries in the xml first. Check your times, check for overlaps & make sure your are using ascii characters to replace foreign characters like umlauts and '&' characters, etc. Make sure all of your movie names / TV Series names are correct. - -You can run `% python PseudoChannel.py` with the following options. The order is important depending on what you are doing (i.e. `% python PseudoChannel.py -u -xml -g -m -r`): - -| Flag | Description | -| ------------------------|--------------| -| -u, --update | Manually update (or create if not exists) the local db when new media is added to your Plex server. | -| -um, --update_movies | Manually update (or create if not exists) the MOVIES local db when new media is added to your Plex server. | -| -utv, --update_tv | Manually update (or create if not exists) the TV local db when new media is added to your Plex server. | -| -uc, --update_comm | Manually update (or create if not exists) the COMMERCIALS local db when new media is added to your Plex server. | -| -xml, --xml | After making any edits your .xml schedule. Run this to populate the local db. | -| -g, --generate_schedule | Manually generate the daily schedule. This is useful for testing / first run. | -| -r, --run | Run PsuedoChannel.py. | -| -c, --show_clients | Show connected Plex clients. | -| -s, --show_schedule | Output the generated "Daily Schedule" to your terminal. | -| -m, --make_html | Manually generate both html / xml docs based on the "Daily Schedule". | -| -e, --export | Export the current queue of your "TV Shows" episodes. Useful when redoing your local DB. | -| -i, --import | Import the previously exported queue of your "TV Shows" episodes. | -| -eds, --export_daily_schedule | Export the daily schedule. | -| -ids, --import_daily_schedule | Import the daily schedule. | - -## `startstop.sh` - Alternative Way of Running the Application: - -Within the app directory, there is a file named, `startstop.sh`. This bash script is a convenient way to start/stop the application. When run, it will start the application and save the "pid" of the process to the application directory. When run again, it will check to see if that process is running, if so it will stop it. All you have to do is run: +3. Make the "install.sh" executable: ```bash -% ./startstop.sh +% chmod +x ./install.sh ``` -When you start the application with this bash script, you can close your terminal as it will keep running in the background. Later, when you come back and want to stop it... you can just execute that file once more and it will stop the running process. Please note: It's good to test the application and your configurations using the manual process above before running this bash script. Although there is a `pseudo-channel.log` that is created within the application directory, it is easier to just view the output in your terminal window - something that won't happen when using the bash script. - -## Setting Up the Generate-Daily-Schedule Cron Task - -Every day (usually at midnight) PseudoChannel needs to generate a new schedule. It will not do this automatically if the app is already running. This task triggers the `-g` flag which generates the new schedule for the following day. This method, as described below, only works when using the `startstop.sh` script. How it works is, it looks for a `running.pid` file in the channel directory which the `startstop.sh` script creates when the channel is run. If it exists, the script will stop the channel, run the `-g` flag, then restart the channel. If it doesn't exist, then the script will just run the `-g` flag without doing anything else. In both cases, the channel generates a new schedule for the upcoming day. Feel free to change the update times, just be sure to choose a time past midnight - so that the app knows if it is a weekend, or what weekday to generate a schedule for. If you usually watch PseudoChannel until 2:00am, set the update time to 3:00am in the crontab task. - -Setup a cron task: -```bash -crontab -e -``` - -...then setup your new task to trigger the included `generate_daily_sched.sh` file: -```bash -0 0 * * * cd /home/pi/channel_01 && ./generate_daily_sched.sh > /dev/null 2>&1 -``` - -This will trigger the `generate_daily_sched.sh` bash script every day at midnight. Be sure to alter the paths in the above crontab task to match your PseudoChannel's paths. - - - -## Futher Info: - -Features are being added to the xml but as of now there are a few. Within the XML `