How to recognise if someone is watching plex media for my bash script
Emily Wong
Hey I have a (for me at least) quite difficult question.
I have plex media server installed on my Ubuntu server. I'm writing a script that will automatically check if there's an update for the plex server every 12 hours and when there is, it will automatically download it. It is written in bash, as that's the only language I know.
#! /bin/bash
ogpath=$(pwd)
path=$(locate -br '^12hpmscheck.sh$' | xargs dirname)
cd "$path"
#step 1
oldlink=""
newlink=$(sudo wget && sudo chmod 777 "$path/5.json" && sudo chmod a+rwx "$path/5.json" && grep -o 'Ubuntu (16.04+) / Debian (8+) - Intel/AMD 64-bit","build":"linux-x86_64","distro":"debian","url":"","checksum":".*"},{"label":"Ubuntu (16.04+) / Debian (8+) - ARMv8' "$path/5.json" | grep -o ')
if [[ "$oldlink" == "$newlink" ]]
then echo "no new version" rm "$path/5.json" cd "$ogpath" sleep 12h $path/12hpmscheck.sh
else echo "new version" #step 3 wget "$newlink" echo "downloaded new version" sed -i "s|$oldlink|$newlink|" "$path/12hpmscheck.sh" echo "updated old link" #step 4 sudo systemctl stop plexmediaserver.service echo "stopped plexmediaserver.service" #step 5 sudo dpkg -i plexmediaserver*.deb echo "installed new version" #step 6 sudo systemctl start plexmediaserver.service echo "started plexmediaserver.service" #step 7 rm 5.json rm plexmediaserver*.deb cd "$ogpath" sleep 12h $path/12hpmscheck.sh
fiExplanation: It first checks the path to the script and to path it was run from (vars ogpath and path). These vars are needed for some commands.
Then we have the var oldlink. That link is the newest link unless there is a new one found. This one doesn't change unless there is a new version.
Then we have the var newlink. This variable will be updated every 12 hours. Long story short, it downloads the plex website for the download of the server and finds the link for Ubuntu 64bit. The outcome will look the same as oldlink aside from the version numbers if there's an update.
if-then-else part: If oldlink and newlink are the same (aka there isn't a new version), it sleeps 12 hours and runs again after that. If they don't match (aka there is a new version), it downloads newlink, stops pms, updates pms, starts pms, updates oldlink to become the new version and then sleeps 12 hours again.
Now you may have noticed the #step 1,3,4,5,6,7. Those are there because I made a note for myself before making the script. I made steps and what needs to happen in those steps. This makes it clear for me what I need to do. But there isn't a step 2. That is because step 2 was the following: If someone is watching plex right now (two other people have access to the server and can watch the content), wait 20min and run the script again. If there isn't a person watching, continue.
I had planned to check this by wget-ing the link to the plex-dashboard (where you can see if someone is watching) and grep a string that is only there when someone is watching. In the code for that page the following string can be found when someone is watching (it can not be found when nobody is watching): NowPlaying-nowPlayingList. I would grep that string and when the output matches "NowPlaying-nowPlayingList" then someone is watching and it waits 20 min. When the output is empty, it continues.
The problem is that I can't wget that dashboard page as it has # in it and people told me that it means that it is a "dynamic java" page. I can't wget it. So I searched for other ways to check and came up with the idea that I could grep that string from the local code. Pms is installed on my server, so there has to be code for it on the machine. If I find the code that has to do with that dashboard, I could search there for a string.
The problem is: I can't find any code in general that is related to the pure functionality of pms, let alone the code for the dashboard. Where can I find the code or is there a different way to check this that I haven't thought of. I prefer to use bash but if I have to, I'm okey to move to a different language. Though I'd have to rewrite the complete script.
And if there's a way to write other parts of the code better/faster, those suggestions are also welcome!
Hopefully you can help. Thanks!
21 Answer
Some suggestions and directions that are too much for comment:
as @cmak.fr pointed out, you should use a
croninstead ofsleepfor this:Run:
$ crontab -eAnd add a line like:
0 */12 * * * /path/to/my/scriptThen your script will run once every 12th hour. Make sure to exit your script if there is no new version.
Use
curlorwget -O-andgrepthe output directly instead of using downloading a file.As this is
jsonoutput, forget what I just said. Do not usegrep! Use ajsonparser (e.g.jq)Install:
sudo apt install jqUsage:
newlink=$(curl | jq '.url')(something like that, I don't know the full plex
jsonoutput)Scraping content on websites loaded by some Javascript is possible but not easy and mostly not necessary. The Javascript will query some API, that you should query directly.
Open the plex.tv site and check the "Network"-tab of the Developer Tools for content that gets loaded apart from the main html.
-or better-
Instead of trying and guessing, you could check the plex API manual: ;-)
Try,
curl"This will retrieve the "Now Playing" Information of the PMS."