2

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!

2
  • hi. you should not sleep12hours then recall the script. Youd better use cron. About getting the number of active users, you could look at the plex CLI or try to download the plex dashboard with a headless browser session.
    – cmak.fr
    Dec 21, 2020 at 16:28
  • Hi Cas. It's good that you give a lot of context, so we can provide accurate answers. Nevertheless, you should highlight a little bit more what is the main question, as this is much reading until then.
    – pLumo
    Dec 21, 2020 at 16:55

1 Answer 1

2

Some suggestions and directions that are too much for comment:

  • as @cmak.fr pointed out, you should use a cron instead of sleep for this:

    Run:

    $ crontab -e
    

    And add a line like:

    0 */12 * * * /path/to/my/script
    

    Then your script will run once every 12th hour. Make sure to exit your script if there is no new version.

  • Use curl or wget -O- and grep the output directly instead of using downloading a file.

  • As this is json output, forget what I just said. Do not use grep! Use a json parser (e.g. jq)

    Install:

    sudo apt install jq
    

    Usage:

    newlink=$(curl plex.tv/... | jq '.url')
    

    (something like that, I don't know the full plex json output)

  • 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 http://ip:port/status/sessions
    

    "This will retrieve the "Now Playing" Information of the PMS."

2
  • @Cas : Very good advices : (cron off course) and jq and queryring plex via the api --- Determining if a new version is available could perhaps also be done via the plex api (?) It would be quite elegant if that were the case. (see X-Plex-Version header). There is also a push notification system which could be usefull
    – cmak.fr
    Dec 22, 2020 at 3:43
  • pLumo's answer worked for what I'm trying to do. I have a script on crontab that runs at 2am and shutdowns my server, curl status/sessions work, but you will need an authentication token / X-Plex-Token support.plex.tv/articles/… curl http://IP:32400/status/sessions?X-Plex-Token=yourtokenhere outputs something like <?xml version="1.0" encoding="UTF-8"?> <MediaContainer size="0"> </MediaContainer> if there's anything running you'll see it in "MediaContainer size" May 1, 2021 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .