add pushover automation

This commit is contained in:
Joseph Hanson 2024-12-02 20:45:20 -06:00
parent 93018e5b70
commit a4a45e5dc6
Signed by: jahanson
SSH key fingerprint: SHA256:vy6dKBECV522aPAwklFM3ReKAVB086rT3oWwiuiFG7o
4 changed files with 126 additions and 0 deletions

View file

@ -117,3 +117,14 @@ spec:
path: /moria/media/ path: /moria/media/
globalMounts: globalMounts:
- path: /data/moria-media - path: /data/moria-media
scripts:
type: configMap
name: sonarr-configmap
defaultMode: 0775
globalMounts:
- path: /scripts/pushover-notify.sh
subPath: pushover-notify.sh
readOnly: true
- path: /scripts/refresh-series.sh
subPath: refresh-series.sh
readOnly: true

View file

@ -6,3 +6,12 @@ resources:
- ./externalsecret.yaml - ./externalsecret.yaml
- ./helmrelease.yaml - ./helmrelease.yaml
- ../../../../templates/volsync - ../../../../templates/volsync
configMapGenerator:
- name: sonarr-configmap
files:
- pushover-notify.sh=./resources/pushover-notify.sh
- refresh-series.sh=./resources/refresh-series.sh
generatorOptions:
disableNameSuffixHash: true
annotations:
kustomize.toolkit.fluxcd.io/substitute: disabled

View file

@ -0,0 +1,85 @@
#!/usr/bin/env bash
# shellcheck disable=SC2154
set -euo pipefail
# User defined variables for pushover
PUSHOVER_USER_KEY="${PUSHOVER_USER_KEY:-required}"
PUSHOVER_TOKEN="${PUSHOVER_TOKEN:-required}"
PUSHOVER_PRIORITY="${PUSHOVER_PRIORITY:-"-2"}"
if [[ "${sonarr_eventtype:-}" == "Test" ]]; then
PUSHOVER_PRIORITY="1"
printf -v PUSHOVER_TITLE \
"Test Notification"
printf -v PUSHOVER_MESSAGE \
"Howdy this is a test notification from %s" \
"${sonarr_instancename:-Sonarr}"
printf -v PUSHOVER_URL \
"%s" \
"${sonarr_applicationurl:-localhost}"
printf -v PUSHOVER_URL_TITLE \
"Open %s" \
"${sonarr_instancename:-Sonarr}"
fi
if [[ "${sonarr_eventtype:-}" == "Download" ]]; then
printf -v PUSHOVER_TITLE \
"Episode %s" \
"$( [[ "${sonarr_isupgrade}" == "True" ]] && echo "Upgraded" || echo "Downloaded" )"
printf -v PUSHOVER_MESSAGE \
"<b>%s (S%02dE%02d)</b><small>\n%s</small><small>\n\n<b>Quality:</b> %s</small><small>\n<b>Client:</b> %s</small>" \
"${sonarr_series_title}" \
"${sonarr_episodefile_seasonnumber}" \
"${sonarr_episodefile_episodenumbers}" \
"${sonarr_episodefile_episodetitles}" \
"${sonarr_episodefile_quality:-Unknown}" \
"${sonarr_download_client:-Unknown}"
printf -v PUSHOVER_URL \
"%s/series/%s" \
"${sonarr_applicationurl:-localhost}" \
"${sonarr_series_titleslug}"
printf -v PUSHOVER_URL_TITLE \
"View series in %s" \
"${sonarr_instancename:-Sonarr}"
fi
if [[ "${sonarr_eventtype:-}" == "ManualInteractionRequired" ]]; then
PUSHOVER_PRIORITY="1"
printf -v PUSHOVER_TITLE \
"Episode import requires intervention"
printf -v PUSHOVER_MESSAGE \
"<b>%s</b><small>\n<b>Client:</b> %s</small>" \
"${sonarr_series_title}" \
"${sonarr_download_client:-Unknown}"
printf -v PUSHOVER_URL \
"%s/activity/queue" \
"${sonarr_applicationurl:-localhost}"
printf -v PUSHOVER_URL_TITLE \
"View queue in %s" \
"${sonarr_instancename:-Sonarr}"
fi
json_data=$(jo \
token="${PUSHOVER_TOKEN}" \
user="${PUSHOVER_USER_KEY}" \
title="${PUSHOVER_TITLE}" \
message="${PUSHOVER_MESSAGE}" \
url="${PUSHOVER_URL}" \
url_title="${PUSHOVER_URL_TITLE}" \
priority="${PUSHOVER_PRIORITY}" \
html="1"
)
status_code=$(curl \
--silent \
--write-out "%{http_code}" \
--output /dev/null \
--request POST \
--header "Content-Type: application/json" \
--data-binary "${json_data}" \
"https://api.pushover.net/1/messages.json" \
)
printf "pushover notification returned with HTTP status code %s and payload: %s\n" \
"${status_code}" \
"$(echo "${json_data}" | jq --compact-output)" >&2

View file

@ -0,0 +1,21 @@
#!/usr/bin/env bash
# shellcheck disable=SC2154
set -euo pipefail
CURL_CMD=(curl -fsSL --header "X-Api-Key: ${SONARR__AUTH__APIKEY:-}")
SONARR_API_URL="http://localhost:${SONARR__SERVER__PORT:-}/api/v3"
if [[ "${sonarr_eventtype:-}" == "Grab" ]]; then
tba=$("${CURL_CMD[@]}" "${SONARR_API_URL}/episode?seriesId=${sonarr_series_id:-}" | jq --raw-output '
[.[] | select((.title == "TBA") or (.title == "TBD"))] | length
')
if (( tba > 0 )); then
echo "INFO: Refreshing series ${sonarr_series_id:-} due to TBA/TBD episodes found"
"${CURL_CMD[@]}" \
--request POST \
--header "Content-Type: application/json" \
--data-binary '{"name": "RefreshSeries", "seriesId": '"${sonarr_series_id:-}"'}' \
"${SONARR_API_URL}/command" &>/dev/null
fi
fi