129 lines
4.2 KiB
Desktop File
129 lines
4.2 KiB
Desktop File
#!/usr/bin/env bash
|
|
scriptVersion="1.5"
|
|
scriptName="InvalidSeriesAutoCleaner"
|
|
|
|
log () {
|
|
m_time=`date "+%F %T"`
|
|
echo $m_time" :: $scriptName :: $scriptVersion :: "$1
|
|
}
|
|
|
|
logfileSetup () {
|
|
# auto-clean up log file to reduce space usage
|
|
if [ -f "/config/logs/$scriptName.txt" ]; then
|
|
find /config/logs -type f -name "$scriptName.txt" -size +1024k -delete
|
|
fi
|
|
|
|
if [ ! -f "/config/logs/$scriptName.txt" ]; then
|
|
touch "/config/logs/$scriptName.txt"
|
|
chmod 666 "/config/logs/$scriptName.txt"
|
|
fi
|
|
}
|
|
|
|
# Create Log, start writing...
|
|
logfileSetup
|
|
exec &> >(tee -a "/config/logs/$scriptName.txt")
|
|
|
|
verifyConfig () {
|
|
#### Import Settings
|
|
source /config/extended.conf
|
|
|
|
if [ "$enableInvalidSeriesAutoCleaner" != "true" ]; then
|
|
log "Script is not enabled, enable by setting enableInvalidSeriesAutoCleaner to \"true\" by modifying the \"/config/extended.conf\" config file..."
|
|
log "Sleeping (infinity)"
|
|
sleep infinity
|
|
fi
|
|
|
|
if [ -z "$invalidSeriesAutoCleanerScriptInterval" ]; then
|
|
invalidSeriesAutoCleanerScriptInterval="1h"
|
|
fi
|
|
}
|
|
|
|
getArrAppInfo () {
|
|
# Get Arr App information
|
|
if [ -z "$arrUrl" ] || [ -z "$arrApiKey" ]; then
|
|
arrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)"
|
|
if [ "$arrUrlBase" == "null" ]; then
|
|
arrUrlBase=""
|
|
else
|
|
arrUrlBase="/$(echo "$arrUrlBase" | sed "s/\///g")"
|
|
fi
|
|
arrName="$(cat /config/config.xml | xq | jq -r .Config.InstanceName)"
|
|
arrApiKey="$(cat /config/config.xml | xq | jq -r .Config.ApiKey)"
|
|
arrPort="$(cat /config/config.xml | xq | jq -r .Config.Port)"
|
|
arrUrl="http://127.0.0.1:${arrPort}${arrUrlBase}"
|
|
fi
|
|
}
|
|
|
|
verifyApiAccess () {
|
|
until false
|
|
do
|
|
arrApiTest=""
|
|
arrApiVersion=""
|
|
if [ "$arrPort" == "8989" ] || [ "$arrPort" == "7878" ]; then
|
|
arrApiVersion="v3"
|
|
elif [ "$arrPort" == "8686" ] || [ "$arrPort" == "8787" ]; then
|
|
arrApiVersion="v1"
|
|
fi
|
|
arrApiTest=$(curl -s "$arrUrl/api/$arrApiVersion/system/status?apikey=$arrApiKey" | jq -r .instanceName)
|
|
if [ "$arrApiTest" == "$arrName" ]; then
|
|
break
|
|
else
|
|
log "$arrName is not ready, sleeping until valid response..."
|
|
sleep 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
InvalidSeriesAutoCleanerProcess () {
|
|
# auto-clean up log file to reduce space usage
|
|
if [ -f "/config/logs/SeriesAutoDelete.txt" ]; then
|
|
find /config/logs -type f -name "InvalidSeriesAutoCleaner.txt" -size +1024k -delete
|
|
fi
|
|
|
|
if [ ! -f "/config/logs/InvalidSeriesAutoCleaner.txt" ]; then
|
|
touch "/config/logs/InvalidSeriesAutoCleaner.txt"
|
|
chmod 666 "/config/logs/InvalidSeriesAutoCleaner.txt"
|
|
fi
|
|
exec &> >(tee -a "/config/logs/InvalidSeriesAutoCleaner.txt")
|
|
|
|
# Get invalid series tvdb id's
|
|
seriesTvdbId="$(curl -s --header "X-Api-Key:"$arrApiKey --request GET "$arrUrl/api/v3/health" | jq -r '.[] | select(.source=="RemovedSeriesCheck") | select(.type=="error")' | grep "message" | grep -o '[[:digit:]]*')"
|
|
|
|
if [ -z "$seriesTvdbId" ]; then
|
|
log "No invalid series (tvdbid) reported by Sonarr health check, skipping..."
|
|
return
|
|
fi
|
|
|
|
# Process each invalid series tvdb id
|
|
for tvdbId in $(echo $seriesTvdbId); do
|
|
seriesData="$(curl -s --header "X-Api-Key:"$arrApiKey --request GET "$arrUrl/api/v3/series" | jq -r ".[] | select(.tvdbId==$tvdbId)")"
|
|
seriesId="$(echo "$seriesData" | jq -r .id)"
|
|
seriesTitle="$(echo "$seriesData" | jq -r .title)"
|
|
seriesPath="$(echo "$seriesData" | jq -r .path)"
|
|
|
|
log "$seriesId :: $seriesTitle :: $seriesPath :: Removing and deleting invalid Series (tvdbId: $tvdbId) based on Sonarr Health Check error..."
|
|
|
|
# Send command to Sonarr to delete series and files
|
|
arrCommand=$(curl -s --header "X-Api-Key:"$arrApiKey --request DELETE "$arrUrl/api/v3/series/$seriesId?deleteFiles=true")
|
|
|
|
|
|
# trigger a plex scan to rmeove the deleted series
|
|
folderToScan="$(dirname "$seriesPath")"
|
|
log "Using PlexNotify.bash to update Plex.... ($folderToScan)"
|
|
bash /config/extended/PlexNotify.bash "$folderToScan" "true"
|
|
done
|
|
}
|
|
|
|
for (( ; ; )); do
|
|
let i++
|
|
logfileSetup
|
|
log "Script starting..."
|
|
verifyConfig
|
|
getArrAppInfo
|
|
verifyApiAccess
|
|
InvalidSeriesAutoCleanerProcess
|
|
log "Script sleeping for $invalidSeriesAutoCleanerScriptInterval..."
|
|
sleep $invalidSeriesAutoCleanerScriptInterval
|
|
done
|
|
|
|
exit
|