2023-07-08 18:18:37 +02:00
|
|
|
#!/usr/bin/with-contenv bash
|
2024-06-07 12:57:33 +02:00
|
|
|
scriptVersion="1.9"
|
2023-07-12 15:16:05 +02:00
|
|
|
scriptName="QueueCleaner"
|
2023-07-08 18:18:37 +02:00
|
|
|
|
2023-07-20 13:18:20 +02:00
|
|
|
#### Import Settings
|
|
|
|
source /config/extended.conf
|
|
|
|
#### Import Functions
|
|
|
|
source /config/extended/functions
|
|
|
|
#### Create Log File
|
2023-07-19 20:12:12 +02:00
|
|
|
logfileSetup
|
2023-07-20 13:18:20 +02:00
|
|
|
#### Check Arr App
|
|
|
|
getArrAppInfo
|
|
|
|
verifyApiAccess
|
2023-07-19 20:12:12 +02:00
|
|
|
|
2023-07-14 21:30:36 +02:00
|
|
|
verifyConfig () {
|
2023-07-14 21:41:03 +02:00
|
|
|
#### Import Settings
|
|
|
|
source /config/extended.conf
|
|
|
|
|
2023-07-14 21:30:36 +02:00
|
|
|
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
|
2023-07-15 16:40:18 +02:00
|
|
|
|
|
|
|
if [ -z "$queueCleanerScriptInterval" ]; then
|
|
|
|
queueCleanerScriptInterval="15m"
|
|
|
|
fi
|
2023-07-14 21:30:36 +02:00
|
|
|
}
|
2023-07-08 18:18:37 +02:00
|
|
|
|
|
|
|
QueueCleanerProcess () {
|
2024-06-07 12:46:19 +02:00
|
|
|
arrQueueData=""
|
2024-06-07 12:57:33 +02:00
|
|
|
arrApp=""
|
|
|
|
|
2023-07-08 18:18:37 +02:00
|
|
|
# Sonarr
|
2024-06-07 12:46:19 +02:00
|
|
|
if [ -z "$arrQueueData" ]; then
|
2023-07-08 18:18:37 +02:00
|
|
|
arrQueueData="$(curl -s "$arrUrl/api/v3/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownSeriesItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
2024-06-07 12:57:33 +02:00
|
|
|
arrApp="sonarr"
|
2023-07-08 18:18:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Radarr
|
2024-06-07 12:46:19 +02:00
|
|
|
if [ -z "$arrQueueData" ]; then
|
|
|
|
arrQueueData="$(curl -s "$arrUrl/api/v3/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownMovieItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
2024-06-07 12:57:33 +02:00
|
|
|
arrApp="radarr"
|
2023-07-08 18:18:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Lidarr
|
2024-06-07 12:46:19 +02:00
|
|
|
if [ -z "$arrQueueData" ]; then
|
2023-07-08 18:18:37 +02:00
|
|
|
arrQueueData="$(curl -s "$arrUrl/api/v1/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownArtistItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
2024-06-07 12:57:33 +02:00
|
|
|
arrApp="lidarr"
|
2023-07-08 18:18:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Readarr
|
2024-06-07 12:46:19 +02:00
|
|
|
if [ -z "$arrQueueData" ]; then
|
2023-07-08 18:18:37 +02:00
|
|
|
arrQueueData="$(curl -s "$arrUrl/api/v1/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownAuthorItems=true&apikey=${arrApiKey}" | jq -r .records[])"
|
2024-06-07 12:57:33 +02:00
|
|
|
arrApp="readarr"
|
2023-07-08 18:18:37 +02:00
|
|
|
fi
|
2024-06-07 12:57:33 +02:00
|
|
|
|
2024-06-07 12:46:19 +02:00
|
|
|
arrQueueIdCount=$(echo "$arrQueueData" | jq -r ".id" | wc -l)
|
2023-07-08 18:18:37 +02:00
|
|
|
arrQueueCompletedIds=$(echo "$arrQueueData" | jq -r 'select(.status=="completed") | select(.trackedDownloadStatus=="warning") | .id')
|
2024-06-07 12:46:19 +02:00
|
|
|
arrQueueIdsCompletedCount=$(echo -n "$arrQueueCompletedIds" | wc -l)
|
2023-07-08 18:18:37 +02:00
|
|
|
arrQueueFailedIds=$(echo "$arrQueueData" | jq -r 'select(.status=="failed") | .id')
|
2024-06-07 12:46:19 +02:00
|
|
|
arrQueueIdsFailedCount=$(echo -n "$arrQueueFailedIds" | wc -l)
|
|
|
|
arrQueueStalledIds=$(echo "$arrQueueData" | jq -r 'select(.status=="stalled") | .id')
|
|
|
|
arrQueueIdsStalledount=$(echo -n "$arrQueueStalledIds" | wc -l)
|
|
|
|
arrQueuedIds=$(echo "$arrQueueCompletedIds"; echo "$arrQueueFailedIds"; echo "$arrQueueStalledIds")
|
|
|
|
arrQueueIdsCount=$(( $arrQueueIdsCompletedCount + $arrQueueIdsFailedCount + $arrQueueIdsStalledount ))
|
|
|
|
|
|
|
|
# Debugging
|
|
|
|
#echo "$arrQueueIdsCount :: $arrQueueIdsCompletedCount + $arrQueueIdsFailedCount + $arrQueueIdsStalledount"
|
|
|
|
#echo "$arrQueueCompletedIds"
|
|
|
|
#echo "$arrQueueFailedIds"
|
|
|
|
#echo "$arrQueueStalledIds"
|
2023-07-08 18:18:37 +02:00
|
|
|
|
|
|
|
if [ $arrQueueIdsCount -eq 0 ]; then
|
2024-06-07 12:46:19 +02:00
|
|
|
log "$arrQueueIdCount items in queue, no items in queue to clean up"
|
2023-07-08 18:18:37 +02:00
|
|
|
else
|
|
|
|
for queueId in $(echo $arrQueuedIds); do
|
|
|
|
arrQueueItemData="$(echo "$arrQueueData" | jq -r "select(.id==$queueId)")"
|
|
|
|
arrQueueItemTitle="$(echo "$arrQueueItemData" | jq -r .title)"
|
2024-06-07 12:57:33 +02:00
|
|
|
if [ "$arrApp" == "sonarr" ]; then
|
2023-07-08 18:18:37 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-15 01:08:32 +02:00
|
|
|
for (( ; ; )); do
|
|
|
|
let i++
|
|
|
|
logfileSetup
|
|
|
|
verifyConfig
|
|
|
|
log "Starting..."
|
|
|
|
QueueCleanerProcess
|
2023-07-15 16:40:18 +02:00
|
|
|
log "Sleeping $queueCleanerScriptInterval..."
|
|
|
|
sleep $queueCleanerScriptInterval
|
2023-07-15 01:08:32 +02:00
|
|
|
done
|
2023-07-08 18:18:37 +02:00
|
|
|
|
|
|
|
exit
|