Skip to content

Instantly share code, notes, and snippets.

@metaMMA
Last active January 13, 2024 07:32
Show Gist options
  • Save metaMMA/f479bf2993be4db437c22ab2f746d812 to your computer and use it in GitHub Desktop.
Save metaMMA/f479bf2993be4db437c22ab2f746d812 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.
#!/usr/bin/python
#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) python 2.7 - https://www.python.org/downloads/
#2) requests - http://docs.python-requests.org/en/master/user/install/#install
#Leaving the 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 that you input on the "edit series" page.
### EDIT SETTINGS ###
url="192.168.1.2"
port="8989"
key="REDACTED"
tag="dvr"
### CODE BELOW ###
import sys
import requests
import json
import time
print("The script to 'notify Sonarr that a Plex DVR recording has been added' has been triggered, and is running.")
#The following retrieves the all of the information sonarr stores about each series.
print("Script is attempting to retrieve all of the information associated with all of the TV series in Sonarr.")
headers={"X-Api-Key": key}
rq = requests.get("http://"+url+":"+port+"/api/series", headers=headers)
js = json.loads(rq.text)
print("Script has retrieved all of the information associated with all of the TV series in Sonarr.")
#The following retrieves all of the information sonarr stores about each tag.
print("Script is attempting to retrieve all of the information associated with all of the user-defined tags in Sonarr.")
rqtag = requests.get("http://"+url+":"+port+"/api/tag", headers=headers)
jstag = json.loads(rqtag.text)
print("Script has retrieved all of the information associated with all of the user-defined tags in Sonarr.")
#The following finds the corresponding "ID" associated with the tag that was input above.
print("Script is attempting to translate the tag label that was input above into a tag ID number.")
tagID = "none"
for z in range(0,len(jstag)-1):
if jstag[z]['label'] == tag:
tagID = jstag[z]['id']
break
if tagID == "none": print("ERROR: You have not entered a valid tag label in the script settings.")
else: print("Script has translated the tag label that was input above into a tag ID number.")
#The following line will get the 'series ID' from that tv show name that is passed as an argument and save it only if it has the correct tag.
print("Script is attempting to translate the title of the media into a Sonarr series ID.")
seriesID = "none"
for x in range(0,len(js)-1):
if js[x]['title'] == sys.argv[1] :
for y in range(0,len(js[x]['tags'])):
if js[x]['tags'][y] == tagID:
seriesID = js[x]['id']
break
else: seriesID = "none"
break
print("Script has completed it's attempt to translate the title of the media into a Sonarr series ID.")
if seriesID != "none":
print("Script has determined that the added media has matching tag, attempting to rescan, rename, then rescan.")
#The following converts the seriesID into a seriesIDs list with the seriesID as the only element.
seriesIds=[]
seriesIds.append(str(seriesID))
#The following line will tell sonarr to rescan the directory of the newly added show to look for any new files
print("Script is attempting to rescan the directory associated with TV series: "+sys.argv[1])
payload = {"name":"rescanSeries","seriesId":seriesID}
requests.post("http://"+url+":"+port+"/api/command", headers=headers, data=json.dumps(payload))
print("Script has started the rescan process.")
#The following line will wait 10 seconds for the rescan to complete
print("Waiting for scan to finish.")
time.sleep(10)
#The following line will tell sonarr to rename the newly added file(s)
print("Script is attempting to rename the added files found in directory associated with TV series: "+sys.argv[1])
payload = {"name":"renameSeries","seriesIds":seriesIds}
requests.post("http://"+url+":"+port+"/api/command", headers=headers, data=json.dumps(payload))
print("Script has started the rename process.")
#The following line will wait 10 seconds for the rename to complete
print("Waiting for rename to finish.")
time.sleep(10)
#The following line will tell sonarr to rescan the directory of the newly renamed show to update file paths
print("Script is attempting to rescan the directory associated with TV series: "+sys.argv[1])
payload = {"name":"rescanSeries","seriesId":seriesID}
requests.post("http://"+url+":"+port+"/api/command", headers=headers, data=json.dumps(payload))
print("Script has started rescan of the directory associated with TV series: "+sys.argv[1]+" and will finish shortly. Script end.")
else:
print("Script has determined that the added media was not from DVR, script will exit.")
@ncc-hyperion
Copy link

I'm running both Tautulli and Sonarr in a docker environment, and I'm getting the following error, can anyone help?

`

2024-01-13 00:27:58 INFO Tautulli Notifiers :: Script notification sent.
2024-01-13 00:27:58 DEBUG Tautulli Notifiers :: Script returned:     The script to 'notify Sonarr that a Plex DVR recording has been added' has been triggered, and is running.    Script is attempting to retrieve all of the information associated with all of the TV series in Sonarr.
2024-01-13 00:27:58 ERROR Tautulli Notifiers :: Script error:     Traceback (most recent call last):        File "/config/scripts/plexDVR_notify_sonarr.py", line 43, in             js = json.loads(rq.text)                      ^^^^^^^^^^^^^^^^^^^        File "/usr/lib/python3.11/json/init.py", line 346, in loads            return _default_decoder.decode(s)                          ^^^^^^^^^^^^^^^^^^^^^^^^^^        File "/usr/lib/python3.11/json/decoder.py", line 337, in decode            obj, end = self.raw_decode(s, idx=_w(s, 0).end())                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        File "/usr/lib/python3.11/json/decoder.py", line 355, in raw_decode            raise JSONDecodeError("Expecting value", s, err.value) from None    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

`

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