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="https://downloads.plex.tv/plex-media-server-new/1.21.1.3830-6c22540d5/debian/plexmediaserver_1.21.1.3830-6c22540d5_amd64.deb"
newlink=$(sudo wget https://plex.tv/pms/downloads/5.json && 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":"https://downloads.plex.tv/plex-media-server-new/.*/debian/plexmediaserver_.*_amd64.deb","checksum":".*"},{"label":"Ubuntu (16.04+) / Debian (8+) - ARMv8' "$path/5.json" | grep -o 'https://.*.deb')
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
fi
Explanation: 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!