Skip to content

Instantly share code, notes, and snippets.

@rustymyers
Last active November 2, 2018 12:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustymyers/cfa215bfb7a3caefccc9c6b3ffd18210 to your computer and use it in GitHub Desktop.
Save rustymyers/cfa215bfb7a3caefccc9c6b3ffd18210 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#--------------------------------------------------------------------------------------------------
#-- Series Tag Updater for Sonarr
#--------------------------------------------------------------------------------------------------
# Program : SonarrTagarr
# To Complie : n/a
#
# Purpose : Add tags to Sonarr series. Tags are retreived from Sonarr.
# Say Y to add a tag. N to skip a tag. N for all tags will remove all tags.
#
# Called By :
# Calls :
#
# Author : Rusty Myers <rustymyers@gmail.com>
# Based Upon :
#
# Note :
#
# Revisions :
# 2017-05-03 <rusty> Initial Version
# 2018-02-12 <rusty> Updating to combine new tags with existing tags,
# Check each tag on series to determine if it needs an update,
# Adding ability to update profiles.
#
# Version : 1.1
#--------------------------------------------------------------------------------------------------
import urllib2
import json
import requests
# Function to return json of all Seriess
def getData(SonarrHost):
response = requests.get(SonarrHost)
json = response.json()
return json
# function to put updated data
def putData(SonarrHost, seriesData):
r = requests.put(SonarrHost, json=seriesData)
return r
# Variables
# Sonarr API Key, get it in Settings->General->Security of Sonarr pages
apiKey = "APIKEY"
# Sonarr Host Name: Add reverse proxy or direct IP:port, no trailing slash
hostName = "https://hostname:8989"
# Get All Tags
allTagsJSON = getData(hostName + "/api/tag?apikey=" + apiKey)
# Get all Profiles
allProfilesJSON = getData(hostName + "/api/profile?apikey=" + apiKey)
# Set a profile
profileint = ""
profileNames = {}
# Print Profiles
for profile in allProfilesJSON:
print "Which profile should we add to the series?"
print "ID: {1} Name: {0}".format(profile["name"], profile["id"])
# Add tag to lookup by it's id
profileNames[profile["id"]] = profile["name"]
# Ask if we should add it to all movies?
addProfile = raw_input('Which profile to all series? [1-{0}] : '.format(len(profileNames)))
# Add the profiles to our list of tag numbers
profileint = int(addProfile)
addProfile = raw_input('Are you sure you want to set all movies to profile \"{0}\"? [y/n] : '.format(profileNames[profileint]))
# If the answer not starts with any case Y
if not addProfile.lower().startswith("y"):
print "Exiting"
else:
print "Adding profile {0}...".format(profileNames[profileint])
# Make list of tags to add
tags = []
# Make a tag lookup for later
tagNames = {}
# For each tag
for tag in allTagsJSON:
# Add tag to lookup by it's id
tagNames[tag['id']] = tag['label']
# Ask if we should add it to all series?
addTag = raw_input('Add Tag \"{0}\" to all series? [y/n] : '.format(tag['label']))
# If the answer starts with any case Y
if addTag.lower().startswith("y"):
# Add the tag to our list of tag numbers
tags.append(int(tag['id']))
# Ask if we should contiunue?
addTag = raw_input("Add Tag(s) '{0}' to all series? [y/n] : ".format(", ".join(tagNames[int(p)] for p in tags)))
# If the answer starts with any case Y
if addTag.lower().startswith("y"):
# Get All series
SonarrHost = hostName + "/api/series?apikey=" + apiKey
allSeriesJSON = getData(SonarrHost)
# for each series
for series in allSeriesJSON:
# get movie ID for updating data with putData
seriesID = series['id']
# Set tagMissing to false and check each tag
dataMissing = False
# For each tag we want to add
for tag in tags:
# If that tag does not exist currenlty
if tag not in series['tags']:
# Set boolean to update tags
dataMissing = True
if series['qualityProfileId'] != profileint or series['profileId'] != profileint:
dataMissing = True
if not dataMissing:
# Print series that have the correct tags
print "Skipping {0} with existing tags: {1} ".format(series['title'].encode('utf-8'), ", ".join(tagNames[int(p)] for p in series['tags']))
else:
# Print name to statisfy user something is happening
print "Adding '{0}' to Series: {1}".format(", ".join(tagNames[int(p)] for p in tags), series['title'].encode('utf-8'))
# Add tag
series['tags'] = tags
series['qualityProfileId'] = profileint
series['profileId'] = profileint
# Update SonarrHost to include series ID
SonarrHost = hostName + "/api/series/" + str(seriesID) + "?apikey=" + apiKey
# Put series back to Sonarr
updateSeries = putData(SonarrHost, series)
# If the update worked, just tell 'em that
if updateSeries.status_code == 202:
print "Sucessfully updated \"{0}\"".format(series['title'].encode('utf-8'))
else:
# If the update didn't work, let 'em know
print "Failed: Series \"{0}\" returned response code: {1}".format(series['title'].encode('utf-8'), updateSeries.status_code)
print updateSeries.text
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment