Skip to content

Instantly share code, notes, and snippets.

@metaMMA
Last active February 26, 2020 04:11
Show Gist options
  • Save metaMMA/4ecd6579476fce7e93b6c8a5271c12ae to your computer and use it in GitHub Desktop.
Save metaMMA/4ecd6579476fce7e93b6c8a5271c12ae to your computer and use it in GitHub Desktop.
Notify Sonarr that a plexDVR recording has been added to the library, so that it can update it's database and rename the file.
#!/bin/bash
#The main purpose of this script is to notify Sonarr that a plexDVR recording has been added to the library, so that it can update it's database and rename the file.
#This script will find the Sonarr 'series ID' of a TV show (taken as an argument when running the script). It will take that 'series ID' and use it to rescan the directory of the show and rename any eligible files (if the show has the specified tag in Sonarr).
#For each tv series that will contain plex DVR files, go into the "edit series" page of that series in Sonarr and add a "tag" that will be associated with these show from the DVR.
#This script can be used in many ways, but one way is to add this script as a 'notification agent' in Tautulli settings. From the "Configuration" tab, input the folder in which the script is saved, then select the script file. Under the "Triggers" tab, select 'Recently Added'. Under the "Conditions" tab, add " 'Media Type' 'is' 'episode' ". The name of the TV Show will be passed as the argument.
#TWO REQUIREMENTS:
#1) cURL - https://curl.haxx.se/download.html
#2) jq - https://stedolan.github.io/jq/
#Leaving the single/double quotation marks where they are, below you must:
# replace "192.168.1.2" with the IP address of the server/computer running Sonarr,
# replace "8989" with the port that Sonarr is running on,
# replace "REDACTED" with the Sonarr API key (found in Sonarr settings -> General -> Security),
# replace "dvr" with the tag you input on the "series edit" page in Sonarr.
# replace "/usr/bin/jq" with the directory location of the jq application. To find location: execute the command "which jq" from linux/mac terminal, or execute the command "where jq" from windows command prompt.
### EDIT SETTINGS ###
ip='192.168.1.2'
port='8989'
key='REDACTED'
tag='dvr'
jq_path="/usr/bin/jq"
### CODE BELOW ###
echo "The script to 'notify Sonarr that a Plex DVR recording has been added' has been triggered, and is running."
#The following line will get the "ID" associated with the tag that was input above.
echo "Script is attempting to look up the ID number associated with the user-defined tag used by Sonarr."
comm=" -rs --arg taglabel \"$tag\" '.[]| .[] | select(.label == \$taglabel ) | .id'"
tagID=$(curl -s http://$ip:$port/api/tag -H "X-Api-Key: $key" --compressed | eval eval $jq_path \$comm )
echo "Script has retrieved Tag ID: $tagID"
#The following line will get the 'series ID' from that tv show name that is passed as an argument
echo "Script is attempting to look up the ID number associated with the TV series in Sonarr."
comm=" -rs --arg seriestitle \"$1\" '.[]| .[] | select(.title == \$seriestitle ) | .id'"
seriesID=$(curl -s http://$ip:$port/api/series -H "X-Api-Key: $key" --compressed | eval eval $jq_path \$comm)
echo "Script has retrieved Series ID: $seriesID"
#The following line will get the list of all tags associated with the tv show that is passed as an argument and save it as a string.
echo "Script is attempting to look up the tags associated with the TV series that was passed into the script."
comm=" -rs --arg seriestitle \"$1\" '.[]| .[] | select(.title == \$seriestitle ) | .tags'"
seriesTagsString=$(curl -s http://$ip:$port/api/series -H "X-Api-Key: $key" --compressed | eval eval $jq_path \$comm)
echo "Script has retrieved all Series IDs"
#The following will convert the list of tags into a Bash array.
echo "Script is attempting to convert all tags from series into a list"
seriesTagsString=${seriesTagsString#*[}
seriesTagsString=${seriesTagsString%]*}
seriesTagsArr=(`echo ${seriesTagsString}`);
IFS=","
read -ra arr <<< ${seriesTagsArr[@]}
echo "Script has converted all tags from series into a list"
proceed="no"
#The following checks to see if the tv show has a tag that matches the tag that was input above.
echo "Script is checking to see if the added media is eligible for rescan/rename."
for item in ${seriesTagsArr[@]}; do
[[ "$tagID" == "$item" ]] && proceed="yes"
done
#The following will only rescan and rename if the tv show tag atches the tag that was input above.
if [[ $proceed == "no" ]]; then
echo "Script has determined that the added media was not from DVR, script will exit."
else
echo "Script has determined that the added media has matching tag, attempting to rescan, rename, then rescan."
#The following line will tell sonarr to rescan the directory of the newly added show to look for any new files
echo "Script is attempting to rescan the directory associated with TV series: $1."
curl -s http://$ip:$port/api/command -H "X-Api-Key: $key" --data-binary '{ "name": "rescanSeries", "seriesId" : "'$seriesID'" }'
echo "Script has started the rescan process."
#The following line will wait 10 seconds for the rescan to complete
echo "Script is waiting for rescan to finish."
sleep 10
#The following line will tell sonarr to rename the newly added file(s)
echo "Script is attempting to rename the added files found in directory associated with TV series: $1"
curl -s http://$ip:$port/api/command -H "X-Api-Key: $key" --data-binary '{ "name": "renameSeries", "seriesIds" : '["$seriesID"]' }'
echo "Script has started the rename process."
#The following line will wait 10 seconds for the rescan to complete
echo "Script is waiting for rename to finish."
sleep 10
#The following line will tell sonarr to rescan the directory of the newly added show to look for any new files
echo "Script is attempting to rescan the directory associated with TV series: $1."
curl -s http://$ip:$port/api/command -H "X-Api-Key: $key" --data-binary '{ "name": "rescanSeries", "seriesId" : "'$seriesID'" }'
echo "Script has started rescan of the directory associated with TV series: $1. End of script."
fi
@sgottscho
Copy link

sgottscho commented Jan 8, 2019

I got the script to work I run in manually, but when Tautulli calls it I get the following error.
2019-01-08 13:50:57 - ERROR :: Thread-21 : Tautulli Notifiers :: Failed to run script: [Error 193] %1 is not a valid Win32 application
I'm using python 2.7 on windows 10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment