From c39041a3aa5335fbade6381149c42aada9f1e868 Mon Sep 17 00:00:00 2001 From: Alex Malinovich Date: Sat, 25 Feb 2017 19:40:35 -0800 Subject: [PATCH] Extract functions into plexupdate-core --- extras/get-plex-token => plexupdate-core | 131 ++++++++++++++++++----- plexupdate.sh | 68 +----------- 2 files changed, 110 insertions(+), 89 deletions(-) rename extras/get-plex-token => plexupdate-core (69%) diff --git a/extras/get-plex-token b/plexupdate-core similarity index 69% rename from extras/get-plex-token rename to plexupdate-core index 6d58676..a296c29 100755 --- a/extras/get-plex-token +++ b/plexupdate-core @@ -1,7 +1,34 @@ #!/bin/bash +######## INDEX ######## +# GPT -> getPlexToken +# GPS -> getPlexServerToken +# GPW -> getPlexWebToken +# HELPERS -> keypair, rawurlencode, trimQuotes +# RNNG -> running +# SHARED -> warn, info, warn +######## CONSTANTS ######## +# Current pages we need - Do not change unless Plex.tv changes again URL_LOGIN='https://plex.tv/users/sign_in.json' +URL_DOWNLOAD='https://plex.tv/api/downloads/1.json?channel=plexpass' +URL_DOWNLOAD_PUBLIC='https://plex.tv/api/downloads/1.json' +# Default options for package managers, override if needed +REDHAT_INSTALL="dnf -y install" +DEBIAN_INSTALL="dpkg -i" +DISTRO_INSTALL="" + +#URL for new version check +UPSTREAM_GIT_URL="https://raw.githubusercontent.com/${GIT_OWNER:-mrworf}/plexupdate/${BRANCHNAME:-master}/plexupdate.sh" + +#Files "owned" by plexupdate, for autoupdate +PLEXUPDATE_FILES="plexupdate.sh plexupdate-core extras/installer.sh extras/cronwrapper" + + +######## FUNCTIONS ######## +#### Token Management ##### + +# GPT getPlexToken() { if [ -n "$TOKEN" ]; then [ "$VERBOSE" = "yes" ] && echo "Fetching token from config" @@ -38,31 +65,7 @@ getPlexToken() { [ -n "$TOKEN" ] # simulate exit status } -# Useful functions -keypair() { - local key="$( rawurlencode "$1" )" - local val="$( rawurlencode "$2" )" - - echo "${key}=${val}" -} - -rawurlencode() { - local string="${1}" - local strlen=${#string} - local encoded="" - - for (( pos=0 ; pos' + if [ $? -eq 1 ]; then + # not found means that one or more medias are being played + return 0 + fi + return 1 + elif [ ${RET} -eq 4 ]; then + # No response, assume not running + return 1 + else + # We do not know what this means... + warn "Unknown response (${RET}) from server >>>" + warn "${DATA}" + return 0 + fi +} + +# Shared functions + +# SHARED +warn() { + echo "WARNING: $@" >&1 +} + +info() { + echo "$@" >&1 +} + +error() { + echo "ERROR: $@" >&2 +} + + if [ "$(basename "$0")" = "get-plex-token" ]; then [ -f /etc/plexupdate.conf ] && source /etc/plexupdate.conf getPlexToken && echo "Token = $TOKEN" diff --git a/plexupdate.sh b/plexupdate.sh index c42ad16..7842c39 100755 --- a/plexupdate.sh +++ b/plexupdate.sh @@ -71,41 +71,12 @@ CHECKUPDATE=yes NOTIFY=no CHECKONLY=no -# Default options for package managers, override if needed -REDHAT_INSTALL="dnf -y install" -DEBIAN_INSTALL="dpkg -i" -DISTRO_INSTALL="" - -# Current pages we need - Do not change unless Plex.tv changes again -URL_LOGIN='https://plex.tv/users/sign_in.json' -URL_DOWNLOAD='https://plex.tv/api/downloads/1.json?channel=plexpass' -URL_DOWNLOAD_PUBLIC='https://plex.tv/api/downloads/1.json' - -#URL for new version check -UPSTREAM_GIT_URL="https://raw.githubusercontent.com/${GIT_OWNER:-mrworf}/plexupdate/${BRANCHNAME:-master}/plexupdate.sh" - -#Files "owned" by plexupdate, for autoupdate -PLEXUPDATE_FILES="plexupdate.sh extras/installer.sh extras/cronwrapper" - FILE_SHA=$(mktemp /tmp/plexupdate.sha.XXXX) FILE_WGETLOG=$(mktemp /tmp/plexupdate.wget.XXXX) FILE_LOCAL=$(mktemp /tmp/plexupdate.local.XXXX) FILE_REMOTE=$(mktemp /tmp/plexupdate.remote.XXXX) ###################################################################### -# Functions for rest of script - -warn() { - echo "WARNING: $@" >&1 -} - -info() { - echo "$@" >&1 -} - -error() { - echo "ERROR: $@" >&2 -} usage() { echo "Usage: $(basename $0) [-acdfFhlpPqsuU] []" @@ -139,41 +110,10 @@ usage() { exit 0 } -running() { - local DATA="$(wget --no-check-certificate -q -O - https://$1:$3/status/sessions?X-Plex-Token=$2)" - local RET=$? - if [ ${RET} -eq 0 ]; then - if [ -z "${DATA}" ]; then - # Odd, but usually means noone is watching - return 1 - fi - echo "${DATA}" | grep -q '' - if [ $? -eq 1 ]; then - # not found means that one or more medias are being played - return 0 - fi - return 1 - elif [ ${RET} -eq 4 ]; then - # No response, assume not running - return 1 - else - # We do not know what this means... - warn "Unknown response (${RET}) from server >>>" - warn "${DATA}" - return 0 - fi -} - -trimQuotes() { - local __buffer=$1 - - # Remove leading single quote - __buffer=${__buffer#\'} - # Remove ending single quote - __buffer=${__buffer%\'} - - echo $__buffer -} +if ! source "$(dirname "$0")"/plexupdate-core; then + echo "ERROR: plexupdate-core can't be found. Please redownload plexupdate and try again." >2 + exit 1 +fi # Setup an exit handler so we cleanup cleanup() {