Last active
June 7, 2022 08:18
-
-
Save rustymyers/671a536b3318e1a3540aa825c98dc16a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
"""#------------------------------------------------------------------------------------------------ | |
#-- Movie Tag Updater for Radarr | |
#-------------------------------------------------------------------------------------------------- | |
# Program : Radarr_tagarr | |
# To Complie : n/a | |
# | |
# Purpose : Add tags and profiles to radarr movies. Tags are retreived from Radarr. | |
# 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 <[email protected]> | |
# Based Upon : | |
# | |
# Note : | |
# | |
# Revisions : | |
# 2017-05-03 <rusty> Initial Version | |
# 2017-05-03 <rusty> Updated to dynamically retreive tags and bulk set the ones chosen | |
# 2018-02-13 <rusty> New method to check for matching tags without concern for order, | |
# Adding code to combine tags with existing movie tags instead of replace, | |
# Updating hostname fields to make it easier to support http or https | |
# 2019-07-26 <rusty> Updating to Python3 | |
# | |
# Version : 1.3 | |
#------------------------------------------------------------------------------------------------""" | |
from __future__ import print_function | |
import requests | |
# Function to return json of all movies | |
def get_data(radarrhost): | |
"""Get data from API""" | |
response = requests.get(radarrhost) | |
return response.json() | |
# function to put updated data | |
def put_data(radarrhost, moviedata): | |
"""Put data into API""" | |
result = requests.put(radarrhost, json=moviedata) | |
return result | |
# Variables | |
# Radarr API Key, get it in Settings->General->Security of Sonarr pages | |
APIKEY = "API" | |
# Radarr Host Name: Add reverse proxy or direct IP:port, no trailing slash | |
HOSTNAME = "https://hostname" | |
# Get All Tags | |
ALLTAGSJSON = get_data(HOSTNAME + "/api/tag?apikey=" + APIKEY) | |
# Get all Profiles | |
ALLPROFILESJSON = get_data(HOSTNAME + "/api/profile?apikey=" + APIKEY) | |
# Set a profile | |
PROFILEINT = "" | |
PROFILENAMES = {} | |
# Print Profiles | |
for profile in ALLPROFILESJSON: | |
print("Which profile should we add to the movie?") | |
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 = input('Which profile to all movies? [1-{0}] : '.format(len(PROFILENAMES))) | |
# Add the profiles to our list of tag numbers | |
PROFILEINT = int(ADDPROFILE) | |
ADDPROFILE = 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("Skipping profile...") | |
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 movies? | |
ADDTAG = input('Add Tag \"{0}\" to all movies? [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 = input("Add Tag(s) '{0}' to all movies? [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 Movies | |
ALLMOVIESJSON = get_data(HOSTNAME + "/api/movie?apikey=" + APIKEY) | |
# for each movie | |
for movie in ALLMOVIESJSON: | |
# get movie ID for updating data with put_data | |
movieID = movie['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 movie['tags']: | |
# Set boolean to update tags | |
dataMissing = True | |
if movie['profileId'] != PROFILEINT: | |
dataMissing = True | |
if not dataMissing: | |
# Print movies that have the correct tags | |
TAGLIST = ", ".join(TAGNAMES[int(p)] for p in movie['tags']) | |
MOVIETITLE = movie['title'].encode('utf-8') | |
print("Skipping {0} with existing tags: {1} ".format(MOVIETITLE, TAGLIST)) | |
else: | |
# Print name to statisfy user something is happening | |
TAGLIST = ", ".join(TAGNAMES[int(p)] for p in TAGS) | |
MOVIETITLE = movie['title'].encode('utf-8') | |
PNAMES = PROFILENAMES[PROFILEINT] | |
print("Adding '{0}' tags and {2} profile to Movie: {1}"\ | |
.format(TAGLIST, MOVIETITLE, PNAMES)) | |
# Combine movie tags and new tags | |
tag_set = list(set(movie['tags']+TAGS)) | |
# Add unique tags | |
movie['tags'] = tag_set | |
movie['profileId'] = PROFILEINT | |
# Update radarrHost to include movie ID | |
radarrHost = HOSTNAME + "/api/movie/" + str(movieID) + "?apikey=" + APIKEY | |
# Put movie back to radarr | |
updateMovie = put_data(radarrHost, movie) | |
# If the update worked, just tell 'em that | |
if updateMovie.status_code == 202: | |
print("Sucessfully updated \"{0}\"".format(movie['title'].encode('utf-8'))) | |
else: | |
# If the update didn't work, let 'em know | |
MOVIETITLE = movie['title'].encode('utf-8') | |
STATUSCODE = updateMovie.status_code | |
print("Failed: \"{0}\" returned response code: {1}".format(MOVIETITLE, STATUSCODE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment