How-To: Script for updating Plex Media Server

Posted on
Sun Apr 23, 2017 10:12 am
davinci offline

How-To: Script for updating Plex Media Server

If you're running Plex ou are probably familiar with Plex Media Server. One issue I had is it doesn't update automatically. So to make our lives easier I scripted a python script which updates it if required. You can run this script at night for example.

I tested it on macOS Sierra and it works properly. The script runs natively.

Remember to edit first two lines according to your installation.

Use at own risk and feel free to contribute.
@jay: I have often scripts that I want to make available for other users. Where is the appropriate place for this?


Code: Select all
targetDirectoryZip = "/Users/USERNAME/Downloads/PlexUpdate.zip"
targetDirectory = "/Users/USERNAME/Downloads"
applicationDirectory = "/Applications"

import sys
import os
import subprocess
import urllib
import json

url = "https://plex.tv/api/downloads/1.json"
response = urllib.urlopen(url)
data = json.loads(response.read())
indigo.server.log("PLEX: Check for latest version")

#dumps the json object into an element
json_str = json.dumps(data)

#load the json to a string
resp = json.loads(json_str)

#extract an element in the response
updateUrl = resp['computer']['Mac']['releases'][0]['url']
versionNew = resp['computer']['Mac']['version']

#Use only the number of the build
sep = '-'
versionNew = versionNew.split(sep, 1)[0]
indigo.server.log("PLEX: Latest version is "+ versionNew)

import plistlib
versionOld = plistlib.readPlist("/Applications/Plex Media Server.app/Contents/Info.plist")
versionOld = versionOld["CFBundleVersion"]
indigo.server.log("PLEX: Installed version is "+ versionOld)


if versionOld != versionNew:
    indigo.server.log("PLEX: Update required - downloading")
       
    import urllib
       
    testfile = urllib.URLopener()
    testfile.retrieve(updateUrl, targetDirectoryZip)
    indigo.server.log("PLEX: Download complete")

    subprocess.call(['osascript', '-e', 'tell application "Plex Media Server" to quit'])
    indigo.server.log("PLEX: Quit Plex Media Server")
   
    os.system("rm -rf '/Applications/Plex Media Server.app'")
    indigo.server.log("PLEX: Remove old version")

    os.system("unzip " + targetDirectoryZip + " -d '/Applications'")
    indigo.server.log("PLEX: Extraction complete")
   
    os.remove(targetDirectoryZip)
    indigo.server.log("PLEX: Remove temporary files")

    subprocess.call(['osascript', '-e', 'tell application "Plex Media Server" to launch'])
    indigo.server.log("PLEX: Starting Plex")

else:
    indigo.server.log("PLEX: Update is not required")


Posted on
Sun Apr 23, 2017 7:22 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: How-To: Script for updating Plex Media Server

davinci wrote:
@jay: I have often scripts that I want to make available for other users. Where is the appropriate place for this?


The Contribution Library. If you want to post about it, then posting in this forums is fine.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 11 guests

cron