kubernetes/apps/media/sonarr/app/resources/refresh-series.sh
1
#!/usr/bin/env bash
2
set -euo pipefail
4
# Incoming environment variables
5
EVENT_TYPE="${sonarr_eventtype:-}"
6
SERIES_ID="${sonarr_series_id:-}"
8
# Only proceed for "Grab" events with valid series ID
9
[[ "${EVENT_TYPE}" == "Grab" && -n "${SERIES_ID}" ]] || exit 0
11
# Required environment variables
12
: "${SONARR__AUTH__APIKEY:?API key required}"
13
: "${SONARR__SERVER__PORT:?Server port required}"
15
# Setup base API URL
16
readonly SONARR_API_URL="http://localhost:${SONARR__SERVER__PORT}/api/v3"
18
# Wrapper for API calls
19
api_call() {
20
curl -fsSL --max-time 30 \
21
--header "Content-Type: application/json" \
22
--header "X-Api-Key: ${SONARR__AUTH__APIKEY}" \
23
"$@"
24
}
26
# Check for episodes with TBA/TBD titles
27
episodes=$(api_call "${SONARR_API_URL}/episode?seriesId=${SERIES_ID}" | \
28
jq '[.[] | select((.title // "" | ascii_downcase | test("(tba|tbd)")) and .airDateUtc < (now + 86400 | strftime("%Y-%m-%dT%H:%M:%SZ")))] | length')
30
# Refresh series if any TBA/TBD episodes found
31
if (( episodes > 0 )); then
32
echo "Refreshing series ${SERIES_ID} (${episodes} TBA/TBD episodes found)"
33
api_call -X POST --data-binary "{\"name\": \"RefreshSeries\", \"seriesId\": ${SERIES_ID}}" \
34
"${SONARR_API_URL}/command" >/dev/null
35
fi