R
R
Roman Bolshukhin2021-08-07 20:10:07
bash
Roman Bolshukhin, 2021-08-07 20:10:07

How to add Plex autoplay?

Hello. There is a script found on the Internet. Sometimes the Plex app crashes. Now the script checks for updates and installs them. Outside of the update functions, it is necessary to add a check of the status of the running application, if it has fallen, then launch it.

#!/usr/bin/env bash

function help() { cat <<HELP
Auto Update Plex Media Server on Synology NAS
Version $(version)
Download latest release from
https://github.com/cowboy/synology-update-plex
Adapted from work first published at
https://forums.plex.tv/t/script-to-auto-update-plex-on-synology-nas-rev4/479748
Usage: $(basename "$0") [options...]
Options:
  --plex-pass  Enable early access / beta releases (requires Plex Pass)
  --version    Display the script release version
  --help       Display this help message
HELP
}

function version() { echo "(in-development)"; } # value auto-generated during release

function header() { echo -e "\n[ [email protected] ]"; }
function warn() { echo "WARN: [email protected]" >&2; }
function fail() { echo "FAIL: [email protected]"; exit 1; }

function process_args() {
  while ; do
    case $1 in
      -h|-\?|--help)
        help
        exit
        ;;
      -v|--version)
        version
        exit
        ;;
      --plex-pass)
        plex_pass=1
        ;;
      *)
        warn "Unknown option (ignored): $1"
        ;;
    esac
    shift
  done
}

function cleanup() {
  code=$?
  if ; then
    header 'Cleaning up'
    echo "Removing $tmp_dir"
    rm -rf $tmp_dir
  fi
  echo
  if ; then
    echo 'Done!'
  else
    notify PlexUpdateError
    echo 'Done, with errors!'
  fi
}

function notify() {
  synonotify $1 '{"%PLEX_VERSION%":"'${available_version:-(unknown)}'"}'
}

function init_notifications() {
  local lang="$(source /etc/synoinfo.conf; echo "$maillang")"
  local mails_file=/var/cache/texts/$lang/mails
  if ; then
    header "Initializing notification system"
    echo "Notifications disabled (file $mails_file not found)"
    return
  fi
  if ; then
    header "Initializing notification system"
    cp $mails_file $mails_file.bak
    cat << 'EOF' >> $mails_file
[PlexUpdateInstalled]
Subject: Successfully updated Plex to %PLEX_VERSION% on %HOSTNAME%
Dear user,
Successfully updated Plex to %PLEX_VERSION% on %HOSTNAME%
---
https://github.com/cowboy/synology-update-plex
[PlexUpdateError]
Subject: Unable to update Plex to %PLEX_VERSION% on %HOSTNAME%
Dear user,
Unable to update Plex to %PLEX_VERSION% on %HOSTNAME%.
If this error persists, enable saving output results in Task Scheduler and file an issue at https://github.com/cowboy/synology-update-plex/issues including the script output.
---
https://github.com/cowboy/synology-update-plex
EOF
    echo 'Notifications installed'
  fi
}

function get_dsm_major_version() {
  dsm_major_version=$(grep -oP 'majorversion="\K[^"]+' /etc.defaults/VERSION)
  if ; then
    fail 'Unable to retrieve DSM major version data'
  else
    echo "DSM Major Version found: $dsm_major_version"
  fi
}

function build_downloads_url() {
  downloads_url='https://plex.tv/api/downloads/5.json'

  if ; then
    header "Enabling Plex Pass releases"

    local pms_dir="$(echo /volume*"/Plex/Library/Application Support/Plex Media Server")"
    if ; then
      pms_dir="$(find /volume* -type d -name 'Plex Media Server' -execdir test -e "{}/Preferences.xml" \; -print -quit)"
    fi

    if ; then
      fail 'Unable to find "Plex Media Server" directory'
    fi

    local prefs_file="$pms_dir/Preferences.xml"
    if ; then
      fail 'Unable to find Preferences.xml file'
    fi

    local token=$(grep -oP 'PlexOnlineToken="\K[^"]+' "$prefs_file" || true)
    if ; then
      fail 'Unable to find Plex Token'
    fi

    echo "Found Plex Token"
    downloads_url="$downloads_url?channel=plexpass&X-Plex-Token=$token"
  fi
}

function retrieve_version_data() {
  header 'Retrieving version data'
    
  get_dsm_major_version
  
  downloads_json="$(curl -s "$downloads_url")"
  if ; then
    fail 'Unable to retrieve version data'
  fi
}

function set_available_version() {
  echo "Looking for Available Version (DSM $dsm_major_version)"
  case $dsm_major_version in
  6)
    available_version=$(jq -r '.nas[] | select(.id == "synology") | .version' <<< "$downloads_json")
  ;;
  7)
    available_version=$(jq -r '.nas[] | select(.id == "synology-dsm7") | .version' <<< "$downloads_json")
  ;;
  *)
    fail "DSM $dsm_major_version not currently Supported!"
  ;;
  esac
  echo "Available version: $available_version"
}

function set_installed_version() {
  echo "Looking for Installed Version (DSM $dsm_major_version)"
  case $dsm_major_version in
  6)
    installed_version=$(/var/packages/Plex\ Media\ Server/target/Plex\ Media\ Server --version)
  ;;
  7)
    installed_version=$(/var/packages/PlexMediaServer/target/Plex\ Media\ Server --version)
  ;;
  *)
    fail "DSM $dsm_major_version not currently Supported!"
  ;;
  esac

  installed_version=${installed_version:1}
  echo "Installed version: $installed_version"
}

# https://stackoverflow.com/a/4024263
function version_lte() {
  
}

function check_up_to_date() {
  set_available_version
  set_installed_version

  echo
  if version_lte "$available_version" "$installed_version"; then
    if ; then
      echo 'The installed version of Plex is newer than the available version. If' \
        'you have Plex Pass, be sure to run this script with the --plex-pass option.'
    fi
    echo 'Plex is up-to-date.'
    exit
  fi

  echo 'New version available!'
}


function get_arch() {
  local arch machine=$1 hw_version=$2
  if ; then
    declare -A model_machine_map
    model_machine_map[DS414j]=armv7hf_neon
    model_machine_map[DS115j]=armv7hf
    model_machine_map[RS815]=armv7hf
    model_machine_map[DS216se]=armv7hf
    model_machine_map[DS215jv10-j]=armv7hf_neon
    if ; then
      arch=${model_machine_map[$hw_version]}
    elif ; then
      arch=armv7hf_neon
    else
      arch=armv7hf
    fi
  elif ; then
    arch=x86
  else
    arch=$machine
  fi
  echo $arch
}

function find_release() {
  header 'Finding release'
  local hw_version=$(</proc/sys/kernel/syno_hw_version)
  local machine=$(uname -m)
  local arch=$(get_arch "$machine" "$hw_version")
  
  echo "Getting Release information (DSM $dsm_major_version)"
  case $dsm_major_version in
  6)
    releases_json="$(jq '.nas[] | select(.id == "synology") | {releases}' <<< "$downloads_json")"
  ;;
  7)
    releases_json="$(jq '.nas[] | select(.id == "synology-dsm7") | {releases}' <<< "$downloads_json")"
  ;;
  *)
    fail "DSM $dsm_major_version not currently Supported!"
  ;;
  esac
  
  if ; then
    fail "Unable to find releases for DSM $dsm_major_version"
  fi
  
  release_json="$(jq '.releases[] | select(.build == "linux-'$arch'")' <<< "$releases_json")"
  if ; then
    fail "Unable to find release for $hw_version/$machine/$arch"
  fi
}

function download_package() {
  header 'Downloading release package'
  local package_url="$(jq -r .url <<< "$release_json")"
  tmp_dir=$(mktemp -d --tmpdir plex.XXXXXX)
  wget --no-verbose "$package_url" -P $tmp_dir

  package_file=$(echo $tmp_dir/*.spk)
  if ; then
    fail "Unable to download package file"
  fi
}

function verify_checksum() {
  header 'Verifying checksum'
  local expected_checksum="$(jq -r .checksum <<< "$release_json")"
  local actual_checksum=$(sha1sum $package_file | cut -f1 -d' ')
  if ; then
    fail "Checksum $actual_checksum invalid"
  fi
  echo "Checksum valid!"
}

function install_package() {
  header 'Installing package'
  synopkg install $package_file
}

function restart_plex() {
  header 'Restarting Plex Media Server'
  synopkg start 'Plex Media Server'
}

function main() {
  set -o errexit
  set -o pipefail
  set -o nounset
  # set -o xtrace

  shopt -s nullglob

  plex_pass=
  process_args "[email protected]"

  tmp_dir=
  trap cleanup EXIT

  echo 'Checking for a Plex Media Server update...'

  if ; then
    fail 'This script must be run as root'
  fi

  init_notifications

  build_downloads_url
  retrieve_version_data

  check_up_to_date

  find_release
  echo "$release_json"

  download_package
  verify_checksum

  install_package
  restart_plex

  notify PlexUpdateInstalled
}

if ; then
  main "[email protected]"
fi

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2021-08-09
@saboteur_kiev

If there is a script that does other things altogether, why should it be changed?
Just write another script.
You know how to write a little and something does not work out - ask a specific question.
If you don't know how to write anything at all, go freelancing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question