Merge branch 'master' into argument-changes

This commit is contained in:
Jon Shaulis 2016-10-06 01:24:43 -04:00
commit d911bfea2b
2 changed files with 36 additions and 37 deletions

View file

@ -24,7 +24,7 @@ Using git is way easier and recommended, if you ask me.
``` ```
git clone https://github.com/mrworf/plexupdate.git git clone https://github.com/mrworf/plexupdate.git
``` ```
Note that git is required (`sudo apt-get install git-all`) Note that git is required (`sudo apt-get install git`)
The main benefit with git clone is that you can update to latest version very easily. If you want to use the auto update feature, you must be using a git clone. The main benefit with git clone is that you can update to latest version very easily. If you want to use the auto update feature, you must be using a git clone.

View file

@ -67,6 +67,13 @@ REDHAT_INSTALL="yum -y install"
DEBIAN_INSTALL="dpkg -i" DEBIAN_INSTALL="dpkg -i"
DISTRO_INSTALL="" DISTRO_INSTALL=""
FILE_STDOUTLOG=$(mktemp /tmp/plexupdate.log.XXXX)
FILE_POSTDATA=$(mktemp /tmp/plexupdate.postdata.XXXX)
FILE_RAW=$(mktemp /tmp/plexupdate.raw.XXXX)
FILE_FAILCAUSE=$(mktemp /tmp/plexupdate.failcause.XXXX)
FILE_KAKA=$(mktemp /tmp/plexupdate.kaka.XXXX)
FILE_SHA=$(mktemp /tmp/plexupdate.sha.XXXX)
# Current pages we need - Do not change unless Plex.tv changes again # 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=https://plex.tv/api/downloads/1.json?channel=plexpass
@ -323,8 +330,7 @@ fi
if [ "${CRON}" = "yes" -a "${QUIET}" = "no" ]; then if [ "${CRON}" = "yes" -a "${QUIET}" = "no" ]; then
# If running in cron mode, redirect STDOUT to temporary file # If running in cron mode, redirect STDOUT to temporary file
STDOUTLOG="$(mktemp)" exec 3>&1 >"${FILE_STDOUTLOG}"
exec 3>&1 >"${STDOUTLOG}"
elif [ "${QUIET}" = "yes" ]; then elif [ "${QUIET}" = "yes" ]; then
# Redirect STDOUT to dev null. Use >&3 if you really, really, REALLY need to print to STDOUT # Redirect STDOUT to dev null. Use >&3 if you really, really, REALLY need to print to STDOUT
exec 3>&1 > /dev/null exec 3>&1 > /dev/null
@ -368,7 +374,7 @@ if [ "${AUTOUPDATE}" = "yes" ]; then
fi fi
# Sanity check # Sanity check
if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f /tmp/kaka ]; then if [ -z "${EMAIL}" -o -z "${PASS}" ] && [ "${PUBLIC}" = "no" ] && [ ! -f "${FILE_KAKA}" ]; then
echo "ERROR: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2 echo "ERROR: Need username & password to download PlexPass version. Otherwise run with -p to download public version." >&2
cronexit 1 cronexit 1
fi fi
@ -440,15 +446,15 @@ keypair() {
# Setup an cronexit handler so we cleanup # Setup an cronexit handler so we cleanup
function cleanup { function cleanup {
if [ "${CRON}" = yes -a "${RAWEXIT}" -ne 5 -a -f "${STDOUTLOG}" ]; then if [ "${CRON}" = yes -a "${RAWEXIT}" -ne 5 -a -f "${FILE_STDOUTLOG}" ]; then
exec 1>&3 exec 1>&3
cat "${STDOUTLOG}" cat "${FILE_STDOUTLOG}"
fi fi
rm "${STDOUTLOG}" 2>/dev/null >/dev/null rm "${FILE_POSTDATA}" 2>/dev/null >/dev/null
rm /tmp/postdata 2>/dev/null >/dev/null rm "${FILE_RAW}" 2>/dev/null >/dev/null
rm /tmp/raw 2>/dev/null >/dev/null rm "${FILE_FAILCAUSE}" 2>/dev/null >/dev/null
rm /tmp/failcause 2>/dev/null >/dev/null rm "${FILE_KAKA}" 2>/dev/null >/dev/null
rm /tmp/kaka 2>/dev/null >/dev/null rm "${FILE_SHA}" 2>/dev/null >/dev/null
} }
trap cleanup EXIT trap cleanup EXIT
@ -462,56 +468,51 @@ trap cleanup EXIT
# user[remember_me] 0 # user[remember_me] 0
# commit Sign in # commit Sign in
# Load previous token if stored
if [ -f /tmp/kaka_token ]; then
TOKEN=$(cat /tmp/kaka_token)
fi
if [ "${PUBLIC}" = "no" ]; then if [ "${PUBLIC}" = "no" ]; then
echo -n "Authenticating..." echo -n "Authenticating..."
# Clean old session # Clean old session
rm /tmp/kaka 2>/dev/null rm "${FILE_KAKA}" 2>/dev/null
# Build post data # Build post data
echo -ne >/tmp/postdata "$(keypair "user[login]" "${EMAIL}" )" echo -ne >"${FILE_POSTDATA}" "$(keypair "user[login]" "${EMAIL}" )"
echo -ne >>/tmp/postdata "&$(keypair "user[password]" "${PASS}" )" echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[password]" "${PASS}" )"
echo -ne >>/tmp/postdata "&$(keypair "user[remember_me]" "0" )" echo -ne >>"${FILE_POSTDATA}" "&$(keypair "user[remember_me]" "0" )"
# Authenticate (using Plex Single Sign On) # Authenticate (using Plex Single Sign On)
wget --header "X-Plex-Client-Identifier: 4a745ae7-1839-e44e-1e42-aebfa578c865" --header "X-Plex-Product: Plex SSO" --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_LOGIN}" --post-file=/tmp/postdata -q -S -O /tmp/failcause 2>/tmp/raw wget --header "X-Plex-Client-Identifier: 4a745ae7-1839-e44e-1e42-aebfa578c865" --header "X-Plex-Product: Plex SSO" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_LOGIN}" --post-file="${FILE_POSTDATA}" -q -S -O "${FILE_FAILCAUSE}" 2>"${FILE_RAW}"
# Delete authentication data ... Bad idea to let that stick around # Delete authentication data ... Bad idea to let that stick around
rm /tmp/postdata rm "${FILE_POSTDATA}"
# Provide some details to the end user # Provide some details to the end user
RESULTCODE=$(head -n1 /tmp/raw | grep -oe '[1-5][0-9][0-9]') RESULTCODE=$(head -n1 "${FILE_RAW}" | grep -oe '[1-5][0-9][0-9]')
if [ $RESULTCODE -eq 401 ]; then if [ $RESULTCODE -eq 401 ]; then
echo "ERROR: Username and/or password incorrect" >&2 echo "ERROR: Username and/or password incorrect" >&2
cronexit 1 cronexit 1
elif [ $RESULTCODE -ne 201 ]; then elif [ $RESULTCODE -ne 201 ]; then
echo "ERROR: Failed to login, debug information:" >&2 echo "ERROR: Failed to login, debug information:" >&2
cat /tmp/failcause >&2 cat "${FILE_FAILCAUSE}" >&2
cronexit 1 cronexit 1
fi fi
# If the system got here, it means the login was successfull, so we set the TOKEN variable to the authToken from the response # If the system got here, it means the login was successfull, so we set the TOKEN variable to the authToken from the response
# I use cut -c 14- to cut off the "authToken":" string from the grepped result, can probably be done in a different way # I use cut -c 14- to cut off the "authToken":" string from the grepped result, can probably be done in a different way
TOKEN=$(</tmp/failcause grep -ioe '"authToken":"[^"]*' | cut -c 14-) TOKEN=$(<"${FILE_FAILCAUSE}" grep -ioe '"authToken":"[^"]*' | cut -c 14-)
# Remove this, since it contains more information than we should leave hanging around # Remove this, since it contains more information than we should leave hanging around
rm /tmp/failcause rm "${FILE_FAILCAUSE}"
echo "OK" echo "OK"
elif [ "$PUBLIC" != "no" ]; then elif [ "$PUBLIC" != "no" ]; then
# It's a public version, so change URL and make doubly sure that cookies are empty # It's a public version, so change URL and make doubly sure that cookies are empty
rm 2>/dev/null >/dev/null /tmp/kaka rm 2>/dev/null >/dev/null "${FILE_KAKA}"
touch /tmp/kaka touch "${FILE_KAKA}"
URL_DOWNLOAD=${URL_DOWNLOAD_PUBLIC} URL_DOWNLOAD=${URL_DOWNLOAD_PUBLIC}
fi fi
if [ "${LISTOPTS}" = "yes" ]; then if [ "${LISTOPTS}" = "yes" ]; then
opts="$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -oe '"label"[^}]*' | grep -v Download | sed 's/"label":"\([^"]*\)","build":"\([^"]*\)","distro":"\([^"]*\)".*/"\3" "\2" "\1"/' | uniq | sort)" opts="$(wget --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -oe '"label"[^}]*' | grep -v Download | sed 's/"label":"\([^"]*\)","build":"\([^"]*\)","distro":"\([^"]*\)".*/"\3" "\2" "\1"/' | uniq | sort)"
eval opts=( "DISTRO" "BUILD" "DESCRIPTION" "======" "=====" "==============================================" $opts ) eval opts=( "DISTRO" "BUILD" "DESCRIPTION" "======" "=====" "==============================================" $opts )
BUILD= BUILD=
@ -539,7 +540,7 @@ fi
echo -n "Finding download URL to download..." echo -n "Finding download URL to download..."
# Set "X-Plex-Token" to the auth token, if no token is specified or it is invalid, the list will return public downloads by default # Set "X-Plex-Token" to the auth token, if no token is specified or it is invalid, the list will return public downloads by default
RELEASE=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -m1 -i "\"build\":\"${BUILD}\"") RELEASE=$(wget --header "X-Plex-Token:"${TOKEN}"" --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${URL_DOWNLOAD}" -O - 2>/dev/null | grep -ioe '"label"[^}]*' | grep -i "\"distro\":\"${DISTRO}\"" | grep -m1 -i "\"build\":\"${BUILD}\"")
DOWNLOAD=$(echo ${RELEASE} | grep -m1 -ioe 'https://[^\"]*') DOWNLOAD=$(echo ${RELEASE} | grep -m1 -ioe 'https://[^\"]*')
CHECKSUM=$(echo ${RELEASE} | grep -ioe '\"checksum\"\:\"[^\"]*' | sed 's/\"checksum\"\:\"//') CHECKSUM=$(echo ${RELEASE} | grep -ioe '\"checksum\"\:\"[^\"]*' | sed 's/\"checksum\"\:\"//')
echo "OK" echo "OK"
@ -555,7 +556,7 @@ if [ $? -ne 0 ]; then
cronexit 3 cronexit 3
fi fi
echo "${CHECKSUM} ${DOWNLOADDIR}/${FILENAME}" >"${DOWNLOADDIR}/${FILENAME}.sha" echo "${CHECKSUM} ${DOWNLOADDIR}/${FILENAME}" >"${FILE_SHA}"
if [ "${PRINT_URL}" = "yes" ]; then if [ "${PRINT_URL}" = "yes" ]; then
if [ "${QUIET}" = "yes" ]; then if [ "${QUIET}" = "yes" ]; then
@ -586,7 +587,7 @@ fi
if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then
if [ "${FORCE}" != "yes" -a "${FORCEALL}" != "yes" ]; then if [ "${FORCE}" != "yes" -a "${FORCEALL}" != "yes" ]; then
sha1sum --status -c "${DOWNLOADDIR}/${FILENAME}.sha" sha1sum --status -c "${FILE_SHA}"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "File already exists (${FILENAME}), won't download." echo "File already exists (${FILENAME}), won't download."
if [ "${AUTOINSTALL}" != "yes" ]; then if [ "${AUTOINSTALL}" != "yes" ]; then
@ -600,7 +601,7 @@ if [ -f "${DOWNLOADDIR}/${FILENAME}" ]; then
elif [ "${FORCEALL}" == "yes" ]; then elif [ "${FORCEALL}" == "yes" ]; then
echo "Note! File exists, but asked to overwrite with new copy" echo "Note! File exists, but asked to overwrite with new copy"
else else
sha1sum --status -c "${DOWNLOADDIR}/${FILENAME}.sha" sha1sum --status -c "${FILE_SHA}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Note! File exists but fails checksum. Redownloading." echo "Note! File exists but fails checksum. Redownloading."
else else
@ -615,7 +616,7 @@ fi
if [ "${SKIP_DOWNLOAD}" = "no" ]; then if [ "${SKIP_DOWNLOAD}" = "no" ]; then
echo -ne "Downloading release \"${FILENAME}\"..." echo -ne "Downloading release \"${FILENAME}\"..."
ERROR=$(wget --load-cookies /tmp/kaka --save-cookies /tmp/kaka --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1) ERROR=$(wget --load-cookies "${FILE_KAKA}" --save-cookies "${FILE_KAKA}" --keep-session-cookies "${DOWNLOAD}" -O "${DOWNLOADDIR}/${FILENAME}" 2>&1)
CODE=$? CODE=$?
if [ ${CODE} -ne 0 ]; then if [ ${CODE} -ne 0 ]; then
echo -e "\n !! Download failed with code ${CODE}, \"${ERROR}\"" echo -e "\n !! Download failed with code ${CODE}, \"${ERROR}\""
@ -624,7 +625,7 @@ if [ "${SKIP_DOWNLOAD}" = "no" ]; then
echo "OK" echo "OK"
fi fi
sha1sum --status -c "${DOWNLOADDIR}/${FILENAME}.sha" sha1sum --status -c "${FILE_SHA}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Downloaded file corrupt. Try again." echo "Downloaded file corrupt. Try again."
cronexit 4 cronexit 4
@ -650,8 +651,6 @@ fi
if [ "${AUTODELETE}" = "yes" ]; then if [ "${AUTODELETE}" = "yes" ]; then
if [ "${AUTOINSTALL}" = "yes" ]; then if [ "${AUTOINSTALL}" = "yes" ]; then
rm -rf "${DOWNLOADDIR}/${FILENAME}" rm -rf "${DOWNLOADDIR}/${FILENAME}"
# Also delete the SHA1 file
rm -rf "${DOWNLOADDIR}/${FILENAME}.sha"
echo "Deleted \"${FILENAME}\"" echo "Deleted \"${FILENAME}\""
else else
echo "Will not auto delete without [-a] auto install" echo "Will not auto delete without [-a] auto install"