Skip to content

Instantly share code, notes, and snippets.

@peaceman
Last active June 7, 2022 09:20
Show Gist options
  • Save peaceman/47e745180d91aea6900b1ce468b568c0 to your computer and use it in GitHub Desktop.
Save peaceman/47e745180d91aea6900b1ce468b568c0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
def requestMovie(baseURL, headers):
url = baseURL + "/api/v3/movie"
print("Queueing rescan command to Radarr via %s." % url)
r = requests.get(url, headers=headers)
command = r.json()
return command
def movie_reached_desired_resolution(movie, min_resolution):
quality = (
movie.get("movieFile", {})
.get("quality", {})
.get("quality", {})
)
return quality.get("resolution", 0) >= min_resolution
def movie_reached_desired_source(movie, min_source):
quality = (
movie.get("movieFile", {})
.get("quality", {})
.get("quality", {})
)
return quality.get("source", 0) == min_source
def movie_filename_does_not_contain(movie, name_parts):
path = movie.get("movieFile", {}).get("originalFilePath", "")
return not any([x.lower() in path.lower() for x in name_parts])
def unmonitor_movie(baseURL, headers, movie):
if (movie["monitored"] == False):
return
print("Unmonitor movie: %s at resolution %s is currently %s" % (
movie["title"],
movie["movieFile"]["quality"]["quality"]["resolution"],
"monitored" if movie["monitored"] else "unmonitored",
))
url = baseURL + "/api/v3/movie/" + str(movie["id"])
data = dict(movie.items(), **{ "monitored": False })
r = requests.put(url, headers=headers, json=data)
r.raise_for_status()
def monitor_movie(baseURL, headers, movie):
if (movie["monitored"]):
return
print("Monitor movie: %s at resolution %s is currently %s" % (
movie["title"],
movie.get("movieFile", {}).get("quality", {}).get("quality", {}).get("resolution"),
"monitored" if movie["monitored"] else "unmonitored",
))
url = baseURL + "/api/v3/movie/" + str(movie["id"])
data = dict(movie.items(), **{ "monitored": True })
r = requests.put(url, headers=headers, json=data)
r.raise_for_status()
try:
host = ''
port = '7878'
webroot = ''
apikey = ''
ssl = ''
protocol = "https://" if ssl else "http://"
baseURL = protocol + host + ":" + str(port) + webroot
min_resolution = 1080
min_source = 'bluray'
if apikey != '':
headers = {
'X-Api-Key': apikey,
}
for x in requestMovie(baseURL, headers):
if (movie_reached_desired_resolution(x, min_resolution)
and movie_reached_desired_source(x, min_source)
and movie_filename_does_not_contain(x, [".ld.", ".md.", ".mic."])):
unmonitor_movie(baseURL, headers, x)
else:
monitor_movie(baseURL, headers, x)
except RuntimeError as e:
print("Error or smth")
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment