mirror of
https://github.com/mrworf/plexupdate.git
synced 2025-08-20 13:23:21 -07:00
Extract functions into plexupdate-core
This commit is contained in:
parent
c7573c2fc8
commit
c39041a3aa
2 changed files with 110 additions and 89 deletions
|
@ -1,7 +1,34 @@
|
||||||
#!/bin/bash
|
#!/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_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() {
|
getPlexToken() {
|
||||||
if [ -n "$TOKEN" ]; then
|
if [ -n "$TOKEN" ]; then
|
||||||
[ "$VERBOSE" = "yes" ] && echo "Fetching token from config"
|
[ "$VERBOSE" = "yes" ] && echo "Fetching token from config"
|
||||||
|
@ -38,31 +65,7 @@ getPlexToken() {
|
||||||
[ -n "$TOKEN" ] # simulate exit status
|
[ -n "$TOKEN" ] # simulate exit status
|
||||||
}
|
}
|
||||||
|
|
||||||
# Useful functions
|
# GPS
|
||||||
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<strlen ; pos++ )); do
|
|
||||||
c=${string:$pos:1}
|
|
||||||
case "$c" in
|
|
||||||
[-_.~a-zA-Z0-9] ) o="${c}" ;;
|
|
||||||
* ) printf -v o '%%%02x' "'$c"
|
|
||||||
esac
|
|
||||||
encoded+="${o}"
|
|
||||||
done
|
|
||||||
echo "${encoded}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Worker functions
|
|
||||||
getPlexServerToken() {
|
getPlexServerToken() {
|
||||||
if [ -f /etc/default/plexmediaserver ]; then
|
if [ -f /etc/default/plexmediaserver ]; then
|
||||||
source /etc/default/plexmediaserver
|
source /etc/default/plexmediaserver
|
||||||
|
@ -86,6 +89,7 @@ getPlexServerToken() {
|
||||||
[ -n "$TOKEN" ] # simulate exit status
|
[ -n "$TOKEN" ] # simulate exit status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# GPW
|
||||||
getPlexWebToken() {
|
getPlexWebToken() {
|
||||||
FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX)
|
FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX)
|
||||||
FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX)
|
FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX)
|
||||||
|
@ -126,6 +130,83 @@ getPlexWebToken() {
|
||||||
[ -n "$TOKEN" ] # simulate exit status
|
[ -n "$TOKEN" ] # simulate exit status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# HELPERS
|
||||||
|
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<strlen ; pos++ )); do
|
||||||
|
c=${string:$pos:1}
|
||||||
|
case "$c" in
|
||||||
|
[-_.~a-zA-Z0-9] ) o="${c}" ;;
|
||||||
|
* ) printf -v o '%%%02x' "'$c"
|
||||||
|
esac
|
||||||
|
encoded+="${o}"
|
||||||
|
done
|
||||||
|
echo "${encoded}"
|
||||||
|
}
|
||||||
|
|
||||||
|
trimQuotes() {
|
||||||
|
local __buffer=$1
|
||||||
|
|
||||||
|
# Remove leading single quote
|
||||||
|
__buffer=${__buffer#\'}
|
||||||
|
# Remove ending single quote
|
||||||
|
__buffer=${__buffer%\'}
|
||||||
|
|
||||||
|
echo $__buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
# RNNG
|
||||||
|
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 '<MediaContainer size="0">'
|
||||||
|
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
|
if [ "$(basename "$0")" = "get-plex-token" ]; then
|
||||||
[ -f /etc/plexupdate.conf ] && source /etc/plexupdate.conf
|
[ -f /etc/plexupdate.conf ] && source /etc/plexupdate.conf
|
||||||
getPlexToken && echo "Token = $TOKEN"
|
getPlexToken && echo "Token = $TOKEN"
|
|
@ -71,41 +71,12 @@ CHECKUPDATE=yes
|
||||||
NOTIFY=no
|
NOTIFY=no
|
||||||
CHECKONLY=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_SHA=$(mktemp /tmp/plexupdate.sha.XXXX)
|
||||||
FILE_WGETLOG=$(mktemp /tmp/plexupdate.wget.XXXX)
|
FILE_WGETLOG=$(mktemp /tmp/plexupdate.wget.XXXX)
|
||||||
FILE_LOCAL=$(mktemp /tmp/plexupdate.local.XXXX)
|
FILE_LOCAL=$(mktemp /tmp/plexupdate.local.XXXX)
|
||||||
FILE_REMOTE=$(mktemp /tmp/plexupdate.remote.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() {
|
usage() {
|
||||||
echo "Usage: $(basename $0) [-acdfFhlpPqsuU] [<long options>]"
|
echo "Usage: $(basename $0) [-acdfFhlpPqsuU] [<long options>]"
|
||||||
|
@ -139,41 +110,10 @@ usage() {
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
running() {
|
if ! source "$(dirname "$0")"/plexupdate-core; then
|
||||||
local DATA="$(wget --no-check-certificate -q -O - https://$1:$3/status/sessions?X-Plex-Token=$2)"
|
echo "ERROR: plexupdate-core can't be found. Please redownload plexupdate and try again." >2
|
||||||
local RET=$?
|
exit 1
|
||||||
if [ ${RET} -eq 0 ]; then
|
fi
|
||||||
if [ -z "${DATA}" ]; then
|
|
||||||
# Odd, but usually means noone is watching
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
echo "${DATA}" | grep -q '<MediaContainer size="0">'
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
# Setup an exit handler so we cleanup
|
# Setup an exit handler so we cleanup
|
||||||
cleanup() {
|
cleanup() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue