138 lines
5.1 KiB
Text
138 lines
5.1 KiB
Text
#!/usr/bin/with-contenv bash
|
|
scriptVersion="1.6"
|
|
scriptName="QueueCleaner"
|
|
|
|
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 [ "$enableQueueCleaner" != "true" ]; then
|
|
log "Script is not enabled, enable by setting enableQueueCleaner to \"true\" by modifying the \"/config/extended.conf\" config file..."
|
|
log "Sleeping (infinity)"
|
|
sleep infinity
|
|
fi
|
|
|
|
if [ -z "$queueCleanerScriptInterval" ]; then
|
|
queueCleanerScriptInterval="15m"
|
|
fi
|
|
}
|
|
|
|
QueueCleanerProcess () {
|
|
# 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
|
|
|
|
# Sonarr
|
|
if [ "$arrPort" == "8989" ]; then
|
|
arrQueueData="$(curl -s "$arrUrl/api/v3/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownSeriesItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
|
fi
|
|
|
|
# Radarr
|
|
if [ "$arrPort" == "7878" ]; then
|
|
arrQueueData="$(curl -s "$arrUrl/api/v3/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownMovieItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
|
fi
|
|
|
|
# Lidarr
|
|
if [ "$arrPort" == "8686" ]; then
|
|
arrQueueData="$(curl -s "$arrUrl/api/v1/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownArtistItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
|
fi
|
|
|
|
# Readarr
|
|
if [ "$arrPort" == "8787" ]; then
|
|
arrQueueData="$(curl -s "$arrUrl/api/v1/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownAuthorItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
|
fi
|
|
|
|
arrQueueCompletedIds=$(echo "$arrQueueData" | jq -r 'select(.status=="completed") | select(.trackedDownloadStatus=="warning") | .id')
|
|
arrQueueIdsCompletedCount=$(echo "$arrQueueData" | jq -r 'select(.status=="completed") | select(.trackedDownloadStatus=="warning") | .id' | wc -l)
|
|
arrQueueFailedIds=$(echo "$arrQueueData" | jq -r 'select(.status=="failed") | .id')
|
|
arrQueueIdsFailedCount=$(echo "$arrQueueData" | jq -r 'select(.status=="failed") | .id' | wc -l)
|
|
arrQueuedIds=$(echo "$arrQueueCompletedIds"; echo "$arrQueueFailedIds")
|
|
arrQueueIdsCount=$(( $arrQueueIdsCompletedCount + $arrQueueIdsFailedCount ))
|
|
|
|
if [ $arrQueueIdsCount -eq 0 ]; then
|
|
log "No items in queue to clean up"
|
|
else
|
|
for queueId in $(echo $arrQueuedIds); do
|
|
arrQueueItemData="$(echo "$arrQueueData" | jq -r "select(.id==$queueId)")"
|
|
arrQueueItemTitle="$(echo "$arrQueueItemData" | jq -r .title)"
|
|
if [ "$arrPort" == "8989" ]; then
|
|
arrEpisodeId="$(echo "$arrQueueItemData" | jq -r .episodeId)"
|
|
arrEpisodeData="$(curl -s "$arrUrl/api/v3/episode/$arrEpisodeId?apikey=${arrApiKey}")"
|
|
arrEpisodeTitle="$(echo "$arrEpisodeData" | jq -r .title)"
|
|
arrEpisodeSeriesId="$(echo "$arrEpisodeData" | jq -r .seriesId)"
|
|
if [ "$arrEpisodeTitle" == "TBA" ]; then
|
|
log "$queueId ($arrQueueItemTitle) :: ERROR :: Episode title is \"$arrEpisodeTitle\" and prevents auto-import, refreshing series..."
|
|
refreshSeries=$(curl -s "$arrUrl/api/$arrApiVersion/command" -X POST -H 'Content-Type: application/json' -H "X-Api-Key: $arrApiKey" --data-raw "{\"name\":\"RefreshSeries\",\"seriesId\":$arrEpisodeSeriesId}")
|
|
continue
|
|
fi
|
|
fi
|
|
log "$queueId ($arrQueueItemTitle) :: Removing Failed Queue Item from $arrName..."
|
|
deleteItem=$(curl -sX DELETE "$arrUrl/api/$arrApiVersion/queue/$queueId?removeFromClient=true&blocklist=true&apikey=${arrApiKey}")
|
|
done
|
|
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
|
|
}
|
|
|
|
for (( ; ; )); do
|
|
let i++
|
|
logfileSetup
|
|
verifyConfig
|
|
log "Starting..."
|
|
QueueCleanerProcess
|
|
log "Sleeping $queueCleanerScriptInterval..."
|
|
sleep $queueCleanerScriptInterval
|
|
done
|
|
|
|
exit
|