diff --git a/plex-do b/plex-do new file mode 100644 index 0000000..3185826 --- /dev/null +++ b/plex-do @@ -0,0 +1,56 @@ +#!/bin/sh + +# automatic maintenance of PLEX +# will, optimize the database, Update the library, and empty the trash +# all the commands will work via curl over localhost +# please make sure that jq is installed + +# check to see if I'm root +# you can run this script as non root use as long as its ran as the same user that plex runs so that its able to read the Preferences.xml file +#if ! [ $(id -u) = 0 ]; then +# echo "Please run as root user" +# exit 1 +#fi + +# configs +PREF="/opt/plex/Library/Application Support/Plex Media Server/Preferences.xml" + +# get token +TOKEN=`cat "${PREF}" | sed -e 's;^.* PlexOnlineToken=";;' | sed -e 's;".*$;;' | tail -1` + +# print token for troubleshooting +# uncomment to put your token in the logs if you want to know what it is. +logger "[DEBUG PLEX] TOKEN: $TOKEN" +echo "[DEBUG PLEX] TOKEN: $TOKEN" + +# optimize database +logger "[DEBUG PLEX] optimizing library" +echo "[DEBUG PLEX] optimizing library" +## curl -X PUT -H "X-Plex-Token: ${TOKEN}" http://127.0.0.1:32400/library/optimize\?async=1&X-Plex-Token=${TOKEN} +curl -X PUT http://127.0.0.1:32400/library/optimize\?async=1\&X-Plex-Token=${TOKEN} + +# adding 120 sec sleep for things to settle +sleep 120 + +# run a loop against each library +for i in `curl -s -H "Accept: application/json" -H "X-Plex-Token: ${TOKEN}" http://127.0.0.1:32400/library/sections | jq -M -r '.MediaContainer.Directory[] | "\(.key)"'` +do + logger "[DEBUG PLEX] Running Update for key: ${i}" + echo "[DEBUG PLEX] Running Update for key: ${i}" +# OLD way +# curl -X PUT -H "X-Plex-Token: ${TOKEN}" http://127.0.0.1:32400/library/sections/${i}/refresh\?force=1&X-Plex-Token=${TOKEN} +# NEW way + curl -X PUT http://127.0.0.1:32400/library/sections/${i}/refresh\?force=1\&X-Plex-Token=${TOKEN} + # adding 30 sec sleep + sleep 30 +# curl -X PUT -H "X-Plex-Token: ${TOKEN}" http://127.0.0.1:32400/library/sections/${i}/emptyTrash?X-Plex-Token=${TOKEN} + logger "[DEBUG PLEX] Emptying Trash for key: ${i}" + echo "[DEBUG PLEX] Emptying Trash for key: ${i}" + curl -X PUT http://127.0.0.1:32400/library/sections/${i}/emptyTrash\?X-Plex-Token=${TOKEN} +done + +# clean PhotoTranscoder Cache +logger "[DEBUG PLEX] Deleting PhotoTranscoder Cache" +echo "[DEBUG PLEX] Deleting PhotoTranscoder Cache" +CACHEPATH="/opt/plex/Library/Application Support/Plex Media Server/Cache/PhotoTranscoder" +find "${CACHEPATH}" -type f -delete diff --git a/plex-token.sh b/plex-token.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/plex-token.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/plex-tools.sh b/plex-tools.sh index 5158c7a..dc89049 100644 --- a/plex-tools.sh +++ b/plex-tools.sh @@ -1,4 +1,5 @@ #!/bin/bash + echo "Welcome to plex-tools wrapper utility!" if ! command -v plex-tools &> /dev/null diff --git a/test b/test new file mode 100644 index 0000000..9b92a0e --- /dev/null +++ b/test @@ -0,0 +1,287 @@ +# Determine which host we are running on and set variables +HostConfig() { + + # On all hosts except Mac + PIDOF="pidof" + STATFMT="-c" + STATBYTES="%s" + + # Synology (DSM 7) + if [ -d /var/packages/PlexMediaServer ] && \ + [ -d "/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData/Plex Media Server" ]; then + + # Where is the software + PKGDIR="/var/packages/PlexMediaServer/target" + PLEX_SQLITE="$PKGDIR/Plex SQLite" + LOG_TOOL="logger" + + # Where is the data + AppSuppDir="/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + LOGFILE="$DBDIR/DBRepair.log" + + # We are done + HostType="Synology (DSM 7)" + + # We do have start/stop as root + HaveStartStop=1 + StartCommand="/usr/syno/bin/synopkg start PlexMediaServer" + StopCommand="/usr/syno/bin/synopkg stop PlexMediaServer" + return 0 + + # Synology (DSM 6) + elif [ -d "/var/packages/Plex Media Server" ] && \ + [ -f "/usr/syno/sbin/synoshare" ]; then + + # Where is the software + PKGDIR="/var/packages/Plex Media Server/target" + PLEX_SQLITE="$PKGDIR/Plex SQLite" + LOG_TOOL="logger" + + # Get shared folder path + AppSuppDir="$(synoshare --get Plex | grep Path | awk -F\[ '{print $2}' | awk -F\] '{print $1}')" + + # Where is the data + AppSuppDir="$AppSuppDir/Library/Application Support" + if [ -d "$AppSuppDir/Plex Media Server" ]; then + + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + LOGFILE="$DBDIR/DBRepair.log" + + HostType="Synology (DSM 6)" + + # We do have start/stop as root + HaveStartStop=1 + StartCommand="/usr/syno/bin/synopkg start PlexMediaServer" + StopCommand="/usr/syno/bin/synopkg stop PlexMediaServer" + return 0 + fi + + + # QNAP (QTS & QuTS) + elif [ -f /etc/config/qpkg.conf ]; then + + # Where is the software + PKGDIR="$(getcfg -f /etc/config/qpkg.conf PlexMediaServer Install_path)" + PLEX_SQLITE="$PKGDIR/Plex SQLite" + LOG_TOOL="/sbin/log_tool -t 0 -a" + + # Where is the data + AppSuppDir="$PKGDIR/Library" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + LOGFILE="$DBDIR/DBRepair.log" + + # Start/Stop + if [ -e /etc/init.d/plex.sh ]; then + HaveStartStop=1 + StartCommand="/etc/init.d/plex.sh start" + StopCommand="/etc/init.d/plex.sh stop" + fi + + HostType="QNAP" + return 0 + + # Standard configuration Linux host + elif [ -f /etc/os-release ] && \ + [ -d /usr/lib/plexmediaserver ] && \ + [ -d /var/lib/plexmediaserver ]; then + + # Where is the software + PKGDIR="/usr/lib/plexmediaserver" + PLEX_SQLITE="$PKGDIR/Plex SQLite" + LOG_TOOL="logger" + + # Where is the data + AppSuppDir="/var/lib/plexmediaserver/Library/Application Support" + # DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + # PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + + # Find the metadata dir if customized + if [ -e /etc/systemd/system/plexmediaserver.service.d ]; then + + # Glob up all 'conf files' found + NewSuppDir="$(cd /etc/systemd/system/plexmediaserver.service.d ; \ + cat override.conf local.conf *.conf 2>/dev/null | grep "APPLICATION_SUPPORT_DIR" | head -1)" + + if [ "$NewSuppDir" != "" ]; then + NewSuppDir="$(echo $NewSuppDir | sed -e 's/.*_DIR=//' | tr -d '"' | tr -d "'")" + + if [ -d "$NewSuppDir" ]; then + AppSuppDir="$NewSuppDir" + else + Output "Given application support directory override specified does not exist: '$NewSuppDir'. Ignoring." + fi + fi + fi + + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + LOGFILE="$DBDIR/DBRepair.log" + + HostType="$(grep ^PRETTY_NAME= /etc/os-release | sed -e 's/PRETTY_NAME=//' | sed -e 's/"//g')" + + HaveStartStop=1 + StartCommand="systemctl start plexmediaserver" + StopCommand="systemctl stop plexmediaserver" + return 0 + + # Netgear ReadyNAS + elif [ -e /etc/os-release ] && [ "$(cat /etc/os-release | grep ReadyNASOS)" != "" ]; then + + # Find PMS + if [ "$(echo /apps/plexmediaserver*)" != "/apps/plexmediaserver*" ]; then + + PKGDIR="$(echo /apps/plexmediaserver*)" + + # Where is the code + PLEX_SQLITE="$PKGDIR/Binaries/Plex SQLite" + AppSuppDir="$PKGDIR/MediaLibrary" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="Netgear ReadyNAS" + return 0 + fi + + # ASUSTOR + elif [ -f /etc/nas.conf ] && grep ASUSTOR /etc/nas.conf >/dev/null && \ + [ -d "/volume1/Plex/Library/Plex Media Server" ]; then + + # Where are things + PLEX_SQLITE="/volume1/.@plugins/AppCentral/plexmediaserver/Plex SQLite" + AppSuppDir="/volume1/Plex/Library" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="ASUSTOR" + return 0 + + # Containers: + # - Docker cgroup v1 & v2 + # - Podman (libpod) + elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] || + [ "$(grep libpod /proc/1/cgroup | wc -l)" -gt 0 ]; then + + # HOTIO Plex image structure is non-standard (contains symlink which breaks detection) + if [ -d "/app/usr/lib/plexmediaserver" ] && [ -d "/config/Plug-in Support" ]; then + PLEX_SQLITE="/app/usr/lib/plexmediaserver/Plex SQLite" + AppSuppDir="/config" + PID_FILE="$AppSuppDir/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="HOTIO" + return 0 + + # Docker (All main image variants except binhex and hotio) + elif [ -d "/config/Library/Application Support" ]; then + + PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" + AppSuppDir="/config/Library/Application Support" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + if [ -d "/var/run/service/svc-plex" ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /var/run/service/svc-plex" + StopCommand="s6-svc -d /var/run/service/svc-plex" + fi + + if [ -d "/var/run/s6/services/plex" ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /var/run/s6/services/plex" + StopCommand="s6-svc -d /var/run/s6/services/plex" + fi + + # lsio stop + if [ -d "/var/run/service/svc-plex" ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /var/run/service/svc-plex" + StopCommand="s6-svc -d /var/run/service/svc-plex" + fi + + # HOTIO + if [ -d /run/service/plex ]; then + HaveStartStop=1 + StartCommand="s6-svc -u /run/service/plex" + StopCommand="s6-svc -d /run/service/plex" + fi + + HostType="Docker" + return 0 + + # BINHEX Plex image + elif [-f /usr/lib/python3.10/binhex.py ] && [ -d "/config/Plex Media Server" ]; then + + PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite" + AppSuppDir="/config" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="BINHEX" + return 0 + + fi + + + # Western Digital (OS5) + elif [ -f /etc/system.conf ] && [ -d /mnt/HD/HD_a2/Nas_Prog/plexmediaserver ] && \ + grep "Western Digital Corp" /etc/system.conf >/dev/null; then + + # Where things are + PLEX_SQLITE="/mnt/HD/HD_a2/Nas_Prog/plexmediaserver/binaries/Plex SQLite" + AppSuppDir="$(echo /mnt/HD/HD*/Nas_Prog/plex_conf)" + PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + HostType="Western Digital" + return 0 + + # Apple Mac + elif [ -d "/Applications/Plex Media Server.app" ] && \ + [ -d "$HOME/Library/Application Support/Plex Media Server" ]; then + + # Where is the software + PLEX_SQLITE="/Applications/Plex Media Server.app/Contents/MacOS/Plex SQLite" + AppSuppDir="$HOME/Library/Application Support" + DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases" + PID_FILE="$DBDIR/dbtmp/plexmediaserver.pid" + LOGFILE="$DBDIR/DBRepair.log" + LOG_TOOL="logger" + + # MacOS uses pgrep and uses different stat options + PIDOF="pgrep" + STATFMT="-f" + STATBYTES="%z" + + # make the TMP directory in advance to store plexmediaserver.pid + mkdir -p "$DBDIR/dbtmp" + + # Remove stale PID file if it exists + [ -f "$PID_FILE" ] && rm "$PID_FILE" + + # If PMS is running create plexmediaserver.pid + PIDVALUE=$($PIDOF "Plex Media Server") + [ $PIDVALUE ] && echo $PIDVALUE > "$PID_FILE" + + HostType="Mac" + return 0 + fi + + # Unknown / currently unsupported host + return 1 +} diff --git a/token-local b/token-local new file mode 100644 index 0000000..68a7170 --- /dev/null +++ b/token-local @@ -0,0 +1,5 @@ +# configs +PREF="/opt/plex/Library/Application Support/Plex Media Server/Preferences.xml" + +# get token +TOKEN=`cat "${PREF}" | sed -e 's;^.* PlexOnlineToken=";;' | sed -e 's;".*$;;' | tail -1` diff --git a/token-remote b/token-remote new file mode 100644 index 0000000..cc369fa --- /dev/null +++ b/token-remote @@ -0,0 +1,38 @@ +if [ -z "$PLEX_LOGIN" ] || [ -z "$PLEX_PASSWORD" ]; then + PLEX_LOGIN=$1 + PLEX_PASSWORD=$2 +fi + +while [ -z "$PLEX_LOGIN" ]; do + >&2 echo -n 'Your Plex login (e-mail or username): ' + read PLEX_LOGIN +done + +while [ -z "$PLEX_PASSWORD" ]; do + >&2 echo -n 'Your Plex password: ' + read PLEX_PASSWORD +done + +>&2 echo 'Retrieving a X-Plex-Token using Plex login/password...' + +curl -qu "${PLEX_LOGIN}":"${PLEX_PASSWORD}" 'https://plex.tv/users/sign_in.xml' \ + -X POST -H 'X-Plex-Device-Name: PlexMediaServer' \ + -H 'X-Plex-Provides: server' \ + -H 'X-Plex-Version: 0.9' \ + -H 'X-Plex-Platform-Version: 0.9' \ + -H 'X-Plex-Platform: xcid' \ + -H 'X-Plex-Product: Plex Media Server'\ + -H 'X-Plex-Device: Linux'\ + -H 'X-Plex-Client-Identifier: XXXX' --compressed >/tmp/plex_sign_in +X_PLEX_TOKEN=$(sed -n 's/.*\(.*\)<\/authentication-token>.*/\1/p' /tmp/plex_sign_in) +if [ -z "$X_PLEX_TOKEN" ]; then + cat /tmp/plex_sign_in + rm -f /tmp/plex_sign_in + >&2 echo 'Failed to retrieve the X-Plex-Token.' + exit 1 +fi +rm -f /tmp/plex_sign_in + +>&2 echo "Your X_PLEX_TOKEN:" + +echo $X_PLEX_TOKEN