Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Emby notification script for Sonarr and Radarr #306

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions radarr/AutoConfig.service
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ if [ "$configureMetadataProviderSettings" == "true" ] || [ -z "$configureMetadat
log "Complete"
fi

if [ "$configureCustomScripts" == "true" ] || [ -z "$configureCustomScripts" ]; then
configure_script() {
scriptName=$1
scriptPath=$2

log "Configuring Radarr Custom Scripts"
if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "PlexNotify.bash" | read; then
log "PlexNotify.bash already added to Radarr custom scripts"
if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "$scriptName" | read; then
log "$scriptName already added to Radarr custom scripts"
else
log "Adding PlexNotify.bash to Radarr custom scripts"
updateArr=$(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2FPlexNotify.bash&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
updateArr=$(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onMovieAdded":false,"onMovieDelete":false,"onMovieFileDelete":true,"onMovieFileDeleteForUpgrade":true,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnMovieAdded":true,"supportsOnMovieDelete":true,"supportsOnMovieFileDelete":true,"supportsOnMovieFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"PlexNotify.bash","fields":[{"name":"path","value":"/config/extended/PlexNotify.bash"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/radarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
log "Complete"
log "Adding $scriptName to Radarr custom scripts"
updateArr=$(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2F$scriptPath&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
updateArr=$(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onMovieAdded":false,"onMovieDelete":false,"onMovieFileDelete":true,"onMovieFileDeleteForUpgrade":true,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnMovieAdded":true,"supportsOnMovieDelete":true,"supportsOnMovieFileDelete":true,"supportsOnMovieFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"'$scriptName'","fields":[{"name":"path","value":"/config/extended/'$scriptPath'"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/radarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
log "Complete"
fi
}

if [ "$configureCustomScripts" == "true" ] || [ -z "$configureCustomScripts" ]; then
configure_script "PlexNotify.bash" "PlexNotify.bash"
configure_script "EmbyNotify.bash" "EmbyNotify.bash"
fi

if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "Extras.bash" | read; then
log "Extras.bash already added to Radarr custom scripts"
Expand Down
79 changes: 79 additions & 0 deletions radarr/EmbyNotify.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash
scriptVersion="1.0"
scriptName="EmbyNotify"

#### Import Settings
source /config/extended.conf # This will import embyUrl and embyApiKey

log () {
m_time=$(date "+%F %T")
echo "$m_time :: $scriptName :: $scriptVersion :: $1"
}

notifiedBy="Radarr"
arrRootFolderPath="$(dirname "$radarr_movie_path")"
arrFolderPath="$radarr_movie_path"
arrEventType="$radarr_eventtype"
movieExtrasPath="$1"

# auto-clean up log file to reduce space usage
if [ -f "/config/logs/EmbyNotify.txt" ]; then
find /config/logs -type f -name "EmbyNotify.txt" -size +1024k -delete
fi

if [ ! -f "/config/logs/EmbyNotify.txt" ]; then
touch "/config/logs/EmbyNotify.txt"
chmod 777 "/config/logs/EmbyNotify.txt"
fi
exec &> >(tee -a "/config/logs/EmbyNotify.txt")

if [ "$arrEventType" == "Test" ]; then
log "$notifiedBy :: Tested Successfully"
exit 0
fi

EmbyConnectionError () {
log "ERROR :: Cannot communicate with Emby"
log "ERROR :: Please check your embyUrl and embyApiKey"
log "ERROR :: Configured embyUrl \"$embyUrl\""
log "ERROR :: Configured embyApiKey \"$embyApiKey\""
log "ERROR :: Exiting..."
exit
}

# Validate connection
if curl -s "$embyUrl/emby/System/Info?api_key=$embyApiKey" | jq . &>/dev/null; then
embyVersion=$(curl -s "$embyUrl/emby/System/Info?api_key=$embyApiKey" | jq -r '.Version')
if [ -z "$embyVersion" ]; then
# Error out if version is null, indicates bad token
EmbyConnectionError
else
log "Emby Connection Established, version: $embyVersion"
fi
else
# Error out if error in curl | jq . command output
EmbyConnectionError
fi

# Get Emby libraries
embyLibraries=$(curl -s "$embyUrl/emby/Library/VirtualFolders?api_key=$embyApiKey")
if [[ -z "$embyLibraries" ]]; then
log "$notifiedBy :: ERROR: Failed to retrieve libraries from Emby"
exit 1
fi

# Find matching Emby library for the given path
embyLibraryId=$(echo "$embyLibraries" | jq -r ".[] | select(.Locations[] | contains(\"$arrRootFolderPath\")) | .CollectionType")

if [[ -z "$embyLibraryId" ]]; then
log "$notifiedBy :: ERROR: No Emby Library found containing path \"$arrRootFolderPath\""
log "$notifiedBy :: ERROR: Add \"$arrRootFolderPath\" as a folder to an Emby Library"
exit 1
else
# Refresh only the relevant folder using its path
log "$notifiedBy :: Emby Library found for path \"$arrRootFolderPath\", refreshing..."
curl -X POST "$embyUrl/emby/Items/$embyLibraryId/Refresh?api_key=$embyApiKey"
log "$notifiedBy :: Emby Scan notification sent for folder: $arrFolderPath"
fi

exit
21 changes: 21 additions & 0 deletions radarr/Extras.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ arrItemId=$radarr_movie_id
tmdbApiKey="3b7751e3179f796565d88fdb2fcdf426"
autoScan="false"
updatePlex="false"
updateEmby="false"
ytdlpExtraOpts="--user-agent facebookexternalhit/1.1"
scriptName="Extras"

Expand Down Expand Up @@ -239,6 +240,7 @@ do
fi

updatePlex="true"
updateEmby="true"

if [ "$extrasSingle" == "true" ]; then
log "$itemTitle :: $i of $tmdbVideosListDataIdsCount :: $tmdbExtraType :: Finished processing single trailer download"
Expand Down Expand Up @@ -268,5 +270,24 @@ if [ ! -z "$plexToken" ]; then
exit
fi
fi
# Process item with EmbyNotify.bash if embyApiKey is configured
if [ ! -z "$embyApiKey" ]; then
# Always update Emby if extra is downloaded
if [ "$updateEmby" == "true" ]; then
log "Using EmbyNotify.bash to update Emby...."
bash /config/extended/EmbyNotify.bash "$itemPath"
exit
fi

# Do not notify emby if this script was triggered by the AutoExtras.bash and no Extras were downloaded
if [ "$autoScan" == "true" ]; then
log "Skipping Emby notification, not needed...."
exit
else
log "Using EmbyNotify.bash to update Emby...."
bash /config/extended/EmbyNotify.bash "$itemPath"
exit
fi
fi

exit
4 changes: 4 additions & 0 deletions radarr/extended.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ recyclarrConfig="/config/extended/recyclarr.yaml" # Change to a custom yaml file
##### PLEX NOTIFY SCRIPT
plexUrl="" # ONLY used if PlexNotify.bash is used, example: http://x.x.x.x:32400
plexToken="" # ONLY used if PlexNotify.bash is used

#### EMBY NOTIFY SCRIPT
embyUrl="" # ONLY used if EmbyNotify.bash is used, example: http://x.x.x.x:8096
embyApiKey="" # ONLY used if EmbyNotify.bash is used
4 changes: 4 additions & 0 deletions radarr/setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ echo "Download PlexNotify script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/radarr/PlexNotify.bash -o /config/extended/PlexNotify.bash
echo "Done"

echo "Download EmbyNotify script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/radarr/EmbyNotify.bash -o /config/extended/EmbyNotify.bash
echo "Done"

echo "Download Extras script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/radarr/Extras.bash -o /config/extended/Extras.bash
echo "Done"
Expand Down
33 changes: 22 additions & 11 deletions sonarr/AutoConfig.service
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,31 @@ if [ "$configureMetadataProviderSettings" == "true" ] || [ -z "$configureMetadat
updateArr=$(curl -s "$arrUrl/api/v3/metadata/4?" -X PUT -H "Content-Type: application/json" -H "X-Api-Key: $arrApiKey" --data-raw '{"enable":true,"name":"Plex","fields":[{"name":"seriesPlexMatchFile","value":true}],"implementationName":"Plex","implementation":"PlexMetadata","configContract":"PlexMetadataSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#plexmetadata","tags":[],"id":4}')
fi

if [ "$configureCustomScripts" == "true" ] || [ -z "$configureCustomScripts" ]; then
configure_script() {
scriptName=$1
scriptPath=$2

log "Configuring Sonarr Custom Scripts"
if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "PlexNotify.bash" | read; then
log "PlexNotify.bash already added to Sonarr custom scripts"
if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "$scriptName" | read; then
log "$scriptName already added to Sonarr custom scripts"
else
log "Adding PlexNotify.bash to Sonarr custom scripts"
# Send a command to check file path, to prevent error with adding...
updateArr=$(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2FPlexNotify.bash&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
# Add PlexNotify.bash
updateArr=$(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onSeriesDelete":true,"onEpisodeFileDelete":true,"onEpisodeFileDeleteForUpgrade":true,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnSeriesDelete":true,"supportsOnEpisodeFileDelete":true,"supportsOnEpisodeFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"PlexNotify.bash","fields":[{"name":"path","value":"/config/extended/PlexNotify.bash"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
log "Complete"
log "Adding $scriptName to Sonarr custom scripts"
# Check file path
updateArr=$(curl -s "$arrUrl/api/v3/filesystem?path=%2Fconfig%2Fextended%2Fscripts%2F$scriptPath&allowFoldersWithoutTrailingSlashes=true&includeFiles=true" -H "X-Api-Key: ${arrApiKey}")
# Add script
updateArr=$(curl -s "$arrUrl/api/v3/notification?" -X POST -H "Content-Type: application/json" -H "X-Api-Key: ${arrApiKey}" --data-raw '{"onGrab":false,"onDownload":true,"onUpgrade":true,"onRename":true,"onSeriesDelete":true,"onEpisodeFileDelete":true,"onEpisodeFileDeleteForUpgrade":true,"onHealthIssue":false,"onApplicationUpdate":false,"supportsOnGrab":true,"supportsOnDownload":true,"supportsOnUpgrade":true,"supportsOnRename":true,"supportsOnSeriesDelete":true,"supportsOnEpisodeFileDelete":true,"supportsOnEpisodeFileDeleteForUpgrade":true,"supportsOnHealthIssue":true,"supportsOnApplicationUpdate":true,"includeHealthWarnings":false,"name":"'$scriptName'","fields":[{"name":"path","value":"/config/extended/'$scriptPath'"},{"name":"arguments"}],"implementationName":"Custom Script","implementation":"CustomScript","configContract":"CustomScriptSettings","infoLink":"https://wiki.servarr.com/sonarr/supported#customscript","message":{"message":"Testing will execute the script with the EventType set to Test, ensure your script handles this correctly","type":"warning"},"tags":[]}')
log "Complete"
fi

}

if [ "$configureCustomScripts" == "true" ] || [ -z "$configureCustomScripts" ]; then
# For Sonarr
configure_script "PlexNotify.bash" "PlexNotify.bash"
configure_script "EmbyNotify.bash" "EmbyNotify.bash"
fi


if curl -s "$arrUrl/api/v3/notification" -H "X-Api-Key: ${arrApiKey}" | jq -r .[].name | grep "DailySeriesEpisodeTrimmer.bash" | read; then
log "DailySeriesEpisodeTrimmer.bash already added to Sonarr custom scripts"
else
Expand Down
95 changes: 95 additions & 0 deletions sonarr/EmbyNotify.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env bash
scriptVersion="1.0"
notifiedBy="Sonarr"
arrRootFolderPath="$(dirname "$sonarr_series_path")"
arrFolderPath="$sonarr_series_path"
arrEventType="$sonarr_eventtype"
extrasPath="$1"
scriptName="EmbyNotify"

#### Import Settings
source /config/extended.conf # Assuming you have embyUrl and embyApiKey in this config file

log () {
m_time=`date "+%F %T"`
echo "$m_time :: $scriptName :: $scriptVersion :: $1"
}

# Auto-clean up log file to reduce space usage
if [ -f "/config/logs/EmbyNotify.txt" ]; then
find /config/logs -type f -name "EmbyNotify.txt" -size +1024k -delete
fi

if [ ! -f "/config/logs/EmbyNotify.txt" ]; then
touch "/config/logs/EmbyNotify.txt"
chmod 666 "/config/logs/EmbyNotify.txt"
fi
exec &> >(tee -a "/config/logs/EmbyNotify.txt")

# If extras are enabled, update paths
if [ "$enableExtras" == "true" ]; then
if [ -z "$extrasPath" ]; then
log "Extras script is enabled, skipping..."
exit
fi
fi

if [ ! -z "$extrasPath" ]; then
arrFolderPath="$extrasPath"
if [ "$2" == "true" ]; then
arrRootFolderPath="$extrasPath"
else
arrRootFolderPath="$(dirname "$extrasPath")"
fi
fi

if [ "$arrEventType" == "Test" ]; then
log "$notifiedBy :: Tested Successfully"
exit 0
fi

embyConnectionError () {
log "ERROR :: Cannot communicate with Emby"
log "ERROR :: Please check your embyUrl and embyApiKey"
log "ERROR :: Configured embyUrl \"$embyUrl\""
log "ERROR :: Configured embyApiKey \"$embyApiKey\""
log "ERROR :: Exiting..."
exit
}

# Validate connection
if curl -s "$embyUrl/emby/System/Info?api_key=$embyApiKey" | jq . &>/dev/null; then
embyVersion=$(curl -s "$embyUrl/emby/System/Info?api_key=$embyApiKey" | jq -r '.Version')
if [ -z "$embyVersion" ]; then
# Error out if version is null, indicates bad token
embyConnectionError
else
log "Emby Connection Established, version: $embyVersion"
fi
else
# Error out if error in curl | jq . command output
embyConnectionError
fi

# Get Emby libraries
embyLibraries=$(curl -s "$embyUrl/emby/Library/VirtualFolders?api_key=$embyApiKey")
if [[ -z "$embyLibraries" ]]; then
log "$notifiedBy :: ERROR: Failed to retrieve libraries from Emby"
exit 1
fi

# Find matching Emby library for the given path
embyLibraryId=$(echo "$embyLibraries" | jq -r ".[] | select(.Locations[] | contains(\"$arrRootFolderPath\")) | .CollectionType")

if [[ -z "$embyLibraryId" ]]; then
log "$notifiedBy :: ERROR: No Emby Library found containing path \"$arrRootFolderPath\""
log "$notifiedBy :: ERROR: Add \"$arrRootFolderPath\" as a folder to an Emby Library"
exit 1
else
# Refresh only the relevant folder using its path
log "$notifiedBy :: Emby Library found for path \"$arrRootFolderPath\", refreshing..."
curl -X POST "$embyUrl/emby/Items/$embyLibraryId/Refresh?api_key=$embyApiKey"
log "$notifiedBy :: Emby Scan notification sent for folder: $arrFolderPath"
fi

exit
26 changes: 26 additions & 0 deletions sonarr/Extras.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ arrItemId=$sonarr_series_id
tmdbApiKey="3b7751e3179f796565d88fdb2fcdf426"
autoScan="false"
updatePlex="false"
updateEmby="false"
ytdlpExtraOpts="--user-agent facebookexternalhit/1.1"
scriptName="Extras"

Expand Down Expand Up @@ -178,6 +179,7 @@ DownloadExtras () {
fi

updatePlex="true"
updateEmby="true"
done
done

Expand Down Expand Up @@ -214,6 +216,28 @@ NotifyPlex () {
fi
}

NotifyEmby () {
# Process item with EmbyNotify.bash if embyApiKey is configured
if [ ! -z "$embyApiKey" ]; then
# Always update Emby if extra is downloaded
if [ "$updateEmby" == "true" ]; then
log "$itemTitle :: Using EmbyNotify.bash to update Emby...."
bash /config/extended/EmbyNotify.bash "$itemPath"
exit
fi

# Do not notify Emby if this script was triggered by the AutoExtras.bash and no Extras were downloaded
if [ "$autoScan" == "true" ]; then
log "$itemTitle :: Skipping Emby notification, not needed...."
exit
else
log "$itemTitle :: Using EmbyNotify.bash to update Emby...."
bash /config/extended/EmbyNotify.bash "$itemPath"
exit
fi
fi
}

# Check if series has been previously processed
if [ -f "/config/extended/logs/extras/$tmdbId" ]; then
# Delete log file older than 7 days, to allow re-processing
Expand All @@ -223,10 +247,12 @@ fi
if [ -f "/config/extended/logs/extras/$tmdbId" ]; then
log "$itemTitle :: Already processed Extras, waiting 7 days to re-check..."
NotifyPlex
NotifyEmby
exit
else
DownloadExtras
NotifyPlex
NotifyEmby
fi

exit
4 changes: 4 additions & 0 deletions sonarr/extended.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ recyclarrConfig="/config/extended/recyclarr.yaml" # Change to a custom yaml file
##### PLEX NOTIFY SCRIPT
plexUrl="" # ONLY used if PlexNotify.bash is used, example: http://x.x.x.x:32400
plexToken="" # ONLY used if PlexNotify.bash is used

#### EMBY NOTIFY SCRIPT
embyUrl="" # ONLY used if EmbyNotify.bash is used, example: http://x.x.x.x:8096
embyApiKey="" # ONLY used if EmbyNotify.bash is used
4 changes: 4 additions & 0 deletions sonarr/setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ echo "Download PlexNotify script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/sonarr/PlexNotify.bash -o /config/extended/PlexNotify.bash
echo "Done"

echo "Download EmbyNotify script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/sonarr/EmbyNotify.bash -o /config/extended/EmbyNotify.bash
echo "Done"

echo "Download DailySeriesEpisodeTrimmer script..."
curl https://raw.githubusercontent.com/RandomNinjaAtk/arr-scripts/main/sonarr/DailySeriesEpisodeTrimmer.bash -o /config/extended/DailySeriesEpisodeTrimmer.bash
echo "Done"
Expand Down