-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSonarr-Invalid-Series-Auto-Cleaner.bash
More file actions
112 lines (91 loc) · 3.5 KB
/
Sonarr-Invalid-Series-Auto-Cleaner.bash
File metadata and controls
112 lines (91 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
scriptVersion="1.1"
scriptName="Sonarr-Invalid-Series-Auto-Cleaner"
dockerLogPath="/config/logs"
settings () {
log "Import Script $1 Settings..."
source "$1"
arrUrl="$sonarrUrl"
arrApiKey="$sonarrApiKey"
}
logfileSetup () {
logFileName="$scriptName-$(date +"%Y_%m_%d_%I_%M_%p").txt"
if [ ! -d "$dockerLogPath" ]; then
mkdir -p "$dockerLogPath"
chown ${PUID:-1000}:${PGID:-1000} "$dockerLogPath"
chmod 777 "$dockerLogPath"
fi
if find "$dockerLogPath" -type f -iname "$scriptName-*.txt" | read; then
# Keep only the last 5 log files for 6 active log files at any given time...
rm -f $(ls -1t $dockerLogPath/$scriptName-* | tail -n +5)
# delete log files older than 5 days
find "$dockerLogPath" -type f -iname "$scriptName-*.txt" -mtime +5 -delete
fi
if [ ! -f "$dockerLogPath/$logFileName" ]; then
echo "" > "$dockerLogPath/$logFileName"
chown ${PUID:-1000}:${PGID:-1000} "$dockerLogPath/$logFileName"
chmod 666 "$dockerLogPath/$logFileName"
fi
}
log () {
m_time=`date "+%F %T"`
echo $m_time" :: $scriptName (v$scriptVersion) :: "$1
echo $m_time" :: $scriptName (v$scriptVersion) :: "$1 >> "$dockerLogPath/$logFileName"
}
verifyConfig () {
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
}
InvalidAutoCleanerProcess () {
# 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")
done
}
for (( ; ; )); do
let i++
logfileSetup
log "Starting..."
confFiles=$(find /config -mindepth 1 -type f -name "*.conf")
confFileCount=$(echo "$confFiles" | wc -l)
if [ -z "$confFiles" ]; then
log "ERROR :: No config files found, exiting..."
exit
fi
for f in $confFiles; do
count=$(($count+1))
log "Processing \"$f\" config file"
settings "$f"
verifyConfig
if [ ! -z "$arrUrl" ]; then
if [ ! -z "$arrApiKey" ]; then
InvalidAutoCleanerProcess
else
log "ERROR :: Skipping Sonarr, missing API Key..."
fi
else
log "ERROR :: Skipping Sonarr, missing URL..."
fi
done
log "Script sleeping for $invalidSeriesAutoCleanerScriptInterval..."
sleep $invalidSeriesAutoCleanerScriptInterval
done
exit