Przeglądaj źródła

PMS cleanups for Baldwin

Zach Isbach 7 lat temu
rodzic
commit
26426b0109
8 zmienionych plików z 69 dodań i 64 usunięć
  1. 15 44
      pms/PMSCheck.sh
  2. 2 0
      pms/com.uberzach.PMS.check.plist
  3. 25 0
      pms/curl.sh
  4. 5 1
      pms/emptyTrash.sh
  5. 8 6
      pms/generate.sh
  6. 4 1
      pms/optimize.sh
  7. 5 11
      pms/scan.sh
  8. 5 1
      pms/sections.sh

+ 15 - 44
pms/PMSCheck.sh

@@ -1,36 +1,18 @@
 #!/bin/bash
 
+# Build URL and cURL opts
+source ~/bin/video/pms/curl.sh
+
 # Config
+RETRY_DELAY=30
 RESTART_DELAY=600
-UNWATCHED_SECTION="2"
+UNWATCHED_SECTION="28"
 UNWATCHED_SLEEP=30
 UNWATCHED_RETRIES=3
-MIN_UNWATCHED_COUNT=10
+MIN_UNWATCHED_COUNT=5
 MAX_MEM=$(( 14 * 1024 * 1024 )) # GB in kB
 ADMIN_EMAIL="zach@kotlarek.com"
 IFS=''
-CURL_TIMEOUT=15
-CURL_OPTS=(--silent --connect-timeout 5 --max-time $CURL_TIMEOUT)
-
-# Construct URL components from the environment
-if [ -z "${PMS_URL}" ]; then
-	if [ -z "${PMS_HOST}" ]; then
-		PMS_HOST="localhost"
-	fi
-	if [ -z "${PMS_PORT}" ]; then
-		PMS_PORT=32400
-	fi
-	PMS_URL="https://${PMS_HOST}:${PMS_PORT}"
-fi
-if [ -z "${PMS_TOKEN}" ]; then
-	echo "No PMS_TOKEN provided" 1>&2
-fi
-CURL_OPTS+=(-H "X-Plex-Token: ${PMS_TOKEN}")
-
-# Heady allows 0 unwatched
-if hostname | grep -qi heady; then
-	MIN_UNWATCHED_COUNT=0
-fi
 
 # Command-line config
 LOOP=-1
@@ -64,22 +46,22 @@ while [ $LOOP -ne 0 ]; do
 	fi
 
 	# Ask Plex for a list of unwatched TV series
-	UNWATCHED_TIMEOUT=$(( $CURL_TIMEOUT ))
 	if [ -z "${FAILED}" ]; then
-		UNWATCHED_URL="${PMS_URL}/library/sections/${UNWATCHED_SECTION}/all?type=2&unwatched=1&sort=titleSort:asc&X-Plex-Container-Start=0&X-Plex-Container-Size=10000"
 		TRY=1
 		FAILED="Too few unwatched series"
-		while [ $TRY -le $UNWATCHED_RETRIES ] && [ -n "${FAILED}" ]; do
-			TRY=$(( $TRY + 1 ))
-			PAGE="`curl ${CURL_OPTS[@]} --max-time "${UNWATCHED_TIMEOUT}" "${UNWATCHED_URL}" 2>&1`"
+		UNWATCHED_URL="${PMS_URL}/library/sections/${UNWATCHED_SECTION}/all?type=2&unwatched=1"
+			UNWATCHED_URL+="&X-Plex-Container-Start=0&X-Plex-Container-Size=$(( $MIN_UNWATCHED_COUNT + 2 ))"
+		while [ $TRY -le $UNWATCHED_RETRIES ]; do
+			PAGE="`curl ${CURL_OPTS[@]} "${UNWATCHED_URL}" 2>&1`"
 			COUNT="`echo "${PAGE}" | grep '</Directory>' | wc -l | awk '{print $1}'`"
 			if [ $COUNT -ge $MIN_UNWATCHED_COUNT ]; then
 				FAILED=""
-			else
-				sleep $UNWATCHED_SLEEP
-				FAILED="${FAILED} ${COUNT}"
-				UNWATCHED_TIMEOUT=$(( $UNWATCHED_TIMEOUT + $CURL_TIMEOUT ))
+				break
 			fi
+
+			sleep $RETRY_DELAY
+			FAILED="${FAILED} ${COUNT}"
+			TRY=$(( $TRY + 1 ))
 		done
 	fi
 
@@ -108,17 +90,6 @@ while [ $LOOP -ne 0 ]; do
 
 		# Give plex a breather to get restarted before we check again
 		sleep "${RESTART_DELAY}"
-
-		# Re-index (in the background)
-		~/bin/video/pms/optimize.sh &
-
-	# If Plex was super slow, optimize it
-	elif [ $UNWATCHED_TIMEOUT -gt $(( $CURL_TIMEOUT * $(( $UNWATCHED_RETRIES / 2 )) )) ]; then
-		ERR_MSG="PMS is slow (${UNWATCHED_TIMEOUT}). Optimizing..."
-		echo "${ERR_MSG}" 1>&2
-
-		# Re-index (in the background)
-		~/bin/video/pms/optimize.sh &
 	fi
 
 	# Sleep for the next loop or exit

+ 2 - 0
pms/com.uberzach.PMS.check.plist

@@ -14,6 +14,8 @@
 	<dict>
 		<key>PMS_TOKEN</key>
 		<string>foo</string>
+		<key>PMS_HOST</key>
+		<string>plex.local</string>
 	</dict>
 	<key>ProgramArguments</key>
 	<array>

+ 25 - 0
pms/curl.sh

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+# Init CURL_OPTS if it's not set
+if [ -z ${CURL_OPTS+x} ]; then
+	IFS=''
+	CURL_OPTS=(--silent --connect-timeout 5 --max-time 30)
+fi
+
+# Construct URL
+if [ -z "${PMS_URL}" ]; then
+	if [ -z "${PMS_HOST}" ]; then
+		PMS_HOST="localhost"
+	fi
+	if [ -z "${PMS_PORT}" ]; then
+		PMS_PORT=32400
+	fi
+	PMS_URL="https://${PMS_HOST}:${PMS_PORT}"
+fi
+
+# Append the token
+if [ -z "${PMS_TOKEN}" ]; then
+	echo "No PMS_TOKEN provided" 1>&2
+else
+	CURL_OPTS+=(-H "X-Plex-Token: ${PMS_TOKEN}")
+fi

+ 5 - 1
pms/emptyTrash.sh

@@ -1,5 +1,8 @@
 #!/bin/bash
 
+# Build URL and cURL opts
+source ~/bin/video/pms/curl.sh
+
 # Bail if the media share isn't mounted
 if ! ~/bin/video/isMediaMounted; then
 	exit 0
@@ -11,6 +14,7 @@ if ~/bin/video/pms/isScanning.sh; then
 fi
 
 # Empty the trash for each section
+IFS=$'\n'
 for i in `~/bin/video/pms/sections.sh`; do
-	curl --silent --upload-file /dev/null "http://localhost:32400/library/sections/${i}/emptyTrash"
+	curl ${CURL_OPTS[@]} --upload-file /dev/null "${PMS_URL}/library/sections/${i}/emptyTrash"
 done

+ 8 - 6
pms/generate.sh

@@ -1,8 +1,7 @@
 #!/bin/bash
 
-BASE_URL="http://localhost:32400"
-
-IFS=$'\n'
+# Build URL and cURL opts
+source ~/bin/video/pms/curl.sh
 
 SECTION="${1}"
 if [ -z "${SECTION}" ] || [ $SECTION -lt 1 ] || [ $SECTION -gt 100 ]; then
@@ -10,17 +9,20 @@ if [ -z "${SECTION}" ] || [ $SECTION -lt 1 ] || [ $SECTION -gt 100 ]; then
 	exit 1
 fi
 
-SERIES="`curl --silent "${BASE_URL}/library/sections/${SECTION}/all" | \
+SERIES="`curl ${CURL_OPTS[@]} "${PMS_URL}/library/sections/${SECTION}/all" | \
 	grep 'key="/library/metadata/[0-9]*/children"' | \
 	sed 's%^.*key="\(/library/metadata/[0-9]*/children\)".*$%\1%'`"
 
+IFS=$'\n'
 for i in $SERIES; do
-	SEASONS="`curl --silent "${BASE_URL}${i}" | \
+	SEASONS="`curl ${CURL_OPTS[@]} "${PMS_URL}${i}" | \
 	grep 'key="/library/metadata/[0-9]*/children"' | \
 	sed 's%^.*key="\(/library/metadata/[0-9]*/children\)".*$%\1%'`"
 
+	IFS=$'\n'
 	for j in $SEASONS; do
-		EPISODES="`curl --silent "${BASE_URL}${j}" | \
+		IFS=''
+		EPISODES="`curl ${CURL_OPTS[@]} "${PMS_URL}${j}" | \
 		grep '<Video ' | \
 		sed 's%^.*ratingKey="\([0-9]*\)".*$%\1%'`"
 

+ 4 - 1
pms/optimize.sh

@@ -1,5 +1,8 @@
 #!/bin/bash
 
+# Build URL and cURL opts
+source ~/bin/video/pms/curl.sh
+
 # Skip
 if [ -z "${2}" ]; then
 	echo "CLI optimization disabled" 1>&2
@@ -27,4 +30,4 @@ while ~/bin/video/pms/isScanning.sh; do
 done
 
 # Optimize
-exec curl --silent --upload-file /dev/null 'http://localhost:32400/library/optimize'
+exec curl ${CURL_OPTS[@]} --upload-file /dev/null "${PMS_URL}/library/optimize"

+ 5 - 11
pms/scan.sh

@@ -22,21 +22,15 @@ if echo "${@}" | grep -q -E -- '--(section|item|directory|file)'; then
 	"${SCAN_CMD}" ${@}
 else
 	for i in `~/bin/video/pms/sections.sh`; do
-		"${SCAN_CMD}" --section "${i}" ${@}
+		if [ -n "${QUIET}" ]; then
+			"${SCAN_CMD}" --section "${i}" ${@} >/dev/null 2>&1
+		else
+			"${SCAN_CMD}" --section "${i}" ${@}
+		fi
 	done
 fi
 
-# Check the return code; kill the PMS if things did not go well
-if [ $? -ne 0 ]; then
-	~/bin/video/pms/killPMS.sh
-fi
-
 # Empty the trash if we scanned and conditions are safe
 if echo "${@}" | grep -q -- '--scan'; then
 	~/bin/video/pms/emptyTrash.sh
 fi
-
-# Force an optimization after deep scans
-if echo "${@}" | grep -q -- '--deep'; then
-	~/bin/video/pms/optimize.sh
-fi

+ 5 - 1
pms/sections.sh

@@ -1,4 +1,8 @@
 #!/bin/bash
 
-curl --silent 'http://localhost:32400/library/sections' | \
+# Build URL and cURL opts
+source ~/bin/video/pms/curl.sh
+
+IFS=''
+curl ${CURL_OPTS[@]} "${PMS_URL}/library/sections" | \
 	grep '<Directory ' | sed 's%^.* key="\([0-9]*\)".*$%\1%'