lidarr-dl/QueueCleaner.bash

134 lines
5.1 KiB
Bash
Raw Normal View History

2023-03-18 17:39:31 +01:00
#!/usr/bin/with-contenv bash
scriptVersion="1.0.3"
2023-03-18 15:29:19 +01:00
2023-03-18 16:19:28 +01:00
######## Settings
scriptInterval="15m"
2023-03-18 15:52:06 +01:00
######## Package dependencies installation
PackageInstallation () {
apk add -U --update --no-cache curl jq python3-dev py3-pip &>/dev/null
pip install --upgrade --no-cache-dir -U yq &>/dev/null
}
2023-03-18 15:35:20 +01:00
2023-03-18 15:52:06 +01:00
# Logging output function
2023-03-18 15:29:19 +01:00
log () {
m_time=`date "+%F %T"`
echo $m_time" :: QueueCleaner :: $scriptVersion :: "$1
}
# auto-clean up log file to reduce space usage
if [ -f "/config/logs/QueueCleaner.txt" ]; then
find /config/logs -type f -name "QueueCleaner.txt" -size +1024k -delete
fi
2023-03-26 16:11:29 +02:00
if [ ! -f "/config/logs/QueueCleaner.txt" ]; then
touch "/config/logs/QueueCleaner.txt"
2023-04-03 15:53:30 +02:00
chmod 666 "/config/logs/QueueCleaner.txt"
2023-03-26 16:11:29 +02:00
fi
exec &> >(tee -a "/config/logs/QueueCleaner.txt")
2023-03-18 15:29:19 +01:00
QueueCleanerProcess () {
2023-03-18 15:52:06 +01:00
# Get Arr App information
2023-03-18 15:29:19 +01:00
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
2023-03-18 15:29:19 +01:00
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
2023-03-18 15:35:20 +01:00
arrQueueData="$(curl -s "$arrUrl/api/v3/queue?page=1&pagesize=200&sortDirection=descending&sortKey=progress&includeUnknownMovieItems=true&apikey=${arrApiKey}" | jq -r .records[])"
2023-03-18 15:29:19 +01:00
fi
# Lidarr
if [ "$arrPort" == "8686" ]; then
2023-03-18 15:29:19 +01:00
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
2023-03-18 15:29:19 +01:00
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
2023-03-18 22:30:44 +01:00
log "No items in queue to clean up"
2023-03-18 15:29:19 +01:00
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
2023-03-18 15:52:06 +01:00
arrEpisodeId="$(echo "$arrQueueItemData" | jq -r .episodeId)"
2023-03-18 15:58:48 +01:00
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}")
2023-03-18 15:52:06 +01:00
continue
fi
fi
2023-03-18 15:58:48 +01:00
log "$queueId ($arrQueueItemTitle) :: Removing Failed Queue Item from $arrName..."
deleteItem=$(curl -sX DELETE "$arrUrl/api/$arrApiVersion/queue/$queueId?removeFromClient=true&blocklist=true&apikey=${arrApiKey}")
2023-03-18 15:29:19 +01:00
done
fi
}
verifyApiAccess () {
until false
do
2023-03-18 17:14:05 +01:00
arrApiTest=""
arrApiVersion=""
if [ "$arrName" == "Sonarr" ] || [ "$arrName" == "Radarr" ]; then
arrApiVersion="v3"
elif [ "$arrName" == "Lidarr" ] || [ "$arrName" == "Readarr" ]; then
2023-03-18 17:14:05 +01:00
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
}
# Install packages
PackageInstallation
2023-03-18 16:19:28 +01:00
2023-03-18 15:52:06 +01:00
arrName="$(cat /config/config.xml | xq | jq -r .Config.InstanceName)"
if [ "$arrName" == "Sonarr" ] || [ "$arrName" == "Radarr" ] || [ "$arrName" == "Lidarr" ] || [ "$arrName" == "Readarr" ]; then
2023-03-18 15:52:06 +01:00
for (( ; ; )); do
let i++
log "Starting..."
2023-03-18 15:52:06 +01:00
QueueCleanerProcess
2023-03-18 22:30:44 +01:00
log "Sleeping $scriptInterval..."
sleep $scriptInterval
2023-03-18 15:52:06 +01:00
done
else
log "ERROR :: Arr app not detected, exiting..."
fi
2023-03-18 15:29:19 +01:00
exit