Plex-Auto-Collections/app/radarr_tools.py

72 lines
2.5 KiB
Python
Raw Normal View History

import re, requests, os, yaml
import config_tools
2019-08-26 03:17:01 +00:00
from tmdbv3api import TMDb
from tmdbv3api import Movie
2019-12-28 10:55:36 +00:00
def add_to_radarr(config_path, missing):
config_tmdb = config_tools.TMDB(config_path)
config_radarr = config_tools.Radarr(config_path)
2019-08-26 03:17:01 +00:00
tmdb = TMDb()
tmdb.api_key = config_tmdb.apikey
tmdb.language = config_tmdb.language
2019-08-26 03:17:01 +00:00
movie = Movie()
for tmdb_id in missing:
try:
tmovie = movie.details(tmdb_id)
tmdb_title = tmovie.title
except AttributeError:
2020-12-26 10:52:08 +00:00
print("| --- Unable to fetch necessary TMDb information for TMDb ID {}, skipping".format(tmdb_id))
continue
# Validate TMDb year (several TMDb entries don't yet have release dates)
try:
# This might be overly punitive
tmdb_year = tmovie.release_date.split("-")[0]
except AttributeError:
print("| --- {} does not have a release date on TMDb yet, skipping".format(tmdb_title))
continue
if tmdb_year.isdigit() == False:
print("| --- {} does not have a release date on TMDb yet, skipping".format(tmdb_title))
continue
2019-08-26 03:17:01 +00:00
tmdb_poster = "https://image.tmdb.org/t/p/original{}".format(tmovie.poster_path)
2019-08-26 03:17:01 +00:00
titleslug = "{} {}".format(tmdb_title, tmdb_year)
titleslug = re.sub(r'([^\s\w]|_)+', '', titleslug)
titleslug = titleslug.replace(" ", "-")
titleslug = titleslug.lower()
payload = {
"title": tmdb_title,
"qualityProfileId": config_radarr.quality_profile_id,
2019-08-26 03:17:01 +00:00
"year": int(tmdb_year),
"tmdbid": str(tmdb_id),
"titleslug": titleslug,
"monitored": "true",
"rootFolderPath": config_radarr.root_folder_path,
2019-08-26 03:17:01 +00:00
"images": [{
"covertype": "poster",
"url": tmdb_poster
2019-08-26 04:32:22 +00:00
}],
"addOptions": {
"searchForMovie": config_radarr.search_movie
2019-08-26 04:32:22 +00:00
}
2019-08-26 03:17:01 +00:00
}
2020-07-25 12:17:09 +00:00
if config_radarr.version == "v3":
slug = "/api/v3/movie"
else:
slug = "/api/movie"
url = config_radarr.url + slug
querystring = {"apikey": "{}".format(config_radarr.token)}
response = requests.post(url, json=payload, params=querystring)
2019-08-26 03:17:01 +00:00
2020-07-25 12:17:09 +00:00
if response.status_code < 400:
print("| +++ {}: Added to Radarr".format(tmdb_title))
2020-07-25 12:17:09 +00:00
else:
print("| --- {}: {} {}".format(tmdb_title, response.json()[0]['errorMessage'], response.status_code))