2021-01-20 21:37:59 +00:00
|
|
|
from modules import util
|
|
|
|
from modules.util import Failed
|
2021-06-14 01:51:30 +00:00
|
|
|
from arrapi import RadarrAPI
|
2021-12-30 00:37:31 +00:00
|
|
|
from arrapi.exceptions import ArrException
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2022-02-13 16:33:57 +00:00
|
|
|
logger = util.logger
|
2021-01-20 21:37:59 +00:00
|
|
|
|
2021-07-23 19:44:21 +00:00
|
|
|
availability_translation = {"announced": "announced", "cinemas": "inCinemas", "released": "released", "db": "preDB"}
|
|
|
|
apply_tags_translation = {"": "add", "sync": "replace", "remove": "remove"}
|
|
|
|
availability_descriptions = {"announced": "For Announced", "cinemas": "For In Cinemas", "released": "For Released", "db": "For PreDB"}
|
2021-04-05 02:25:41 +00:00
|
|
|
|
2021-06-14 01:51:30 +00:00
|
|
|
class Radarr:
|
2021-11-29 15:24:36 +00:00
|
|
|
def __init__(self, config, library, params):
|
2021-07-14 14:47:20 +00:00
|
|
|
self.config = config
|
2021-11-29 15:24:36 +00:00
|
|
|
self.library = library
|
2021-04-05 02:25:41 +00:00
|
|
|
self.url = params["url"]
|
2021-04-02 20:15:59 +00:00
|
|
|
self.token = params["token"]
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.secret(self.url)
|
|
|
|
logger.secret(self.token)
|
2021-01-20 21:37:59 +00:00
|
|
|
try:
|
2021-07-15 22:08:47 +00:00
|
|
|
self.api = RadarrAPI(self.url, self.token, session=self.config.session)
|
2021-11-12 07:23:03 +00:00
|
|
|
self.api.respect_list_exclusions_when_adding()
|
2021-12-07 02:10:36 +00:00
|
|
|
self.api._validate_add_options(params["root_folder_path"], params["quality_profile"])
|
2021-06-14 01:51:30 +00:00
|
|
|
except ArrException as e:
|
|
|
|
raise Failed(e)
|
2021-12-30 17:13:01 +00:00
|
|
|
self.add_missing = params["add_missing"]
|
2021-08-16 05:41:04 +00:00
|
|
|
self.add_existing = params["add_existing"]
|
2021-04-05 02:25:41 +00:00
|
|
|
self.root_folder_path = params["root_folder_path"]
|
|
|
|
self.monitor = params["monitor"]
|
|
|
|
self.availability = params["availability"]
|
2021-06-14 01:51:30 +00:00
|
|
|
self.quality_profile = params["quality_profile"]
|
2021-04-05 02:25:41 +00:00
|
|
|
self.tag = params["tag"]
|
|
|
|
self.search = params["search"]
|
2021-11-24 16:47:21 +00:00
|
|
|
self.radarr_path = params["radarr_path"] if params["radarr_path"] and params["plex_path"] else ""
|
|
|
|
self.plex_path = params["plex_path"] if params["radarr_path"] and params["plex_path"] else ""
|
2021-04-05 02:25:41 +00:00
|
|
|
|
|
|
|
def add_tmdb(self, tmdb_ids, **options):
|
2021-11-29 19:04:43 +00:00
|
|
|
_ids = []
|
|
|
|
_paths = []
|
2021-11-24 13:22:17 +00:00
|
|
|
for tmdb_id in tmdb_ids:
|
2021-11-29 19:04:43 +00:00
|
|
|
if isinstance(tmdb_id, tuple):
|
|
|
|
_paths.append(tmdb_id)
|
|
|
|
else:
|
|
|
|
_ids.append(tmdb_id)
|
2021-12-30 16:32:18 +00:00
|
|
|
logger.info("")
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.separator(f"Adding {'Missing' if _ids else 'Existing'} to Radarr", space=False, border=False)
|
2021-12-30 16:32:18 +00:00
|
|
|
logger.debug("")
|
2021-11-29 19:04:43 +00:00
|
|
|
logger.debug(f"Radarr Adds: {_ids if _ids else ''}")
|
|
|
|
for tmdb_id in _paths:
|
2021-11-24 13:22:17 +00:00
|
|
|
logger.debug(tmdb_id)
|
2021-04-05 02:25:41 +00:00
|
|
|
folder = options["folder"] if "folder" in options else self.root_folder_path
|
|
|
|
monitor = options["monitor"] if "monitor" in options else self.monitor
|
2021-06-14 01:51:30 +00:00
|
|
|
availability = availability_translation[options["availability"] if "availability" in options else self.availability]
|
|
|
|
quality_profile = options["quality"] if "quality" in options else self.quality_profile
|
2021-04-05 02:25:41 +00:00
|
|
|
tags = options["tag"] if "tag" in options else self.tag
|
|
|
|
search = options["search"] if "search" in options else self.search
|
2021-11-24 16:47:21 +00:00
|
|
|
|
2021-12-06 09:16:43 +00:00
|
|
|
arr_paths = {}
|
|
|
|
arr_ids = {}
|
|
|
|
for movie in self.api.all_movies():
|
|
|
|
if movie.path:
|
2021-12-28 22:41:04 +00:00
|
|
|
arr_paths[movie.path[:-1].lower() if movie.path.endswith(("/", "\\")) else movie.path.lower()] = movie.tmdbId
|
2021-12-06 09:16:43 +00:00
|
|
|
arr_ids[movie.tmdbId] = movie
|
2021-12-13 14:28:46 +00:00
|
|
|
if self.config.trace_mode:
|
|
|
|
logger.debug(arr_paths)
|
|
|
|
logger.debug(arr_ids)
|
2021-12-06 09:16:43 +00:00
|
|
|
|
2021-11-24 16:47:21 +00:00
|
|
|
added = []
|
|
|
|
exists = []
|
2021-12-01 13:17:28 +00:00
|
|
|
skipped = []
|
2021-11-24 16:47:21 +00:00
|
|
|
invalid = []
|
2021-12-30 16:32:18 +00:00
|
|
|
invalid_root = []
|
2021-11-24 16:47:21 +00:00
|
|
|
movies = []
|
2021-12-06 09:16:43 +00:00
|
|
|
path_lookup = {}
|
|
|
|
mismatched = {}
|
2021-12-07 02:10:36 +00:00
|
|
|
path_in_use = {}
|
2021-12-30 16:32:18 +00:00
|
|
|
|
|
|
|
def mass_add():
|
|
|
|
try:
|
|
|
|
_a, _e, _i = self.api.add_multiple_movies(movies, folder, quality_profile, monitor, search,
|
|
|
|
availability, tags, per_request=100)
|
|
|
|
added.extend(_a)
|
|
|
|
exists.extend(_e)
|
|
|
|
invalid.extend(_i)
|
|
|
|
except ArrException as e:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.stacktrace()
|
2021-12-30 16:32:18 +00:00
|
|
|
raise Failed(f"Radarr Error: {e}")
|
|
|
|
|
2021-11-24 16:47:21 +00:00
|
|
|
for i, item in enumerate(tmdb_ids, 1):
|
|
|
|
path = item[1] if isinstance(item, tuple) else None
|
|
|
|
tmdb_id = item[0] if isinstance(item, tuple) else item
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.ghost(f"Loading TMDb ID {i}/{len(tmdb_ids)} ({tmdb_id})")
|
2021-11-29 15:24:36 +00:00
|
|
|
if self.config.Cache:
|
|
|
|
_id = self.config.Cache.query_radarr_adds(tmdb_id, self.library.original_mapping_name)
|
|
|
|
if _id:
|
2021-12-01 13:17:28 +00:00
|
|
|
skipped.append(item)
|
2021-11-29 15:24:36 +00:00
|
|
|
continue
|
2021-11-24 16:47:21 +00:00
|
|
|
try:
|
2021-12-06 09:16:43 +00:00
|
|
|
if tmdb_id in arr_ids:
|
|
|
|
exists.append(arr_ids[tmdb_id])
|
|
|
|
continue
|
2021-12-29 03:44:55 +00:00
|
|
|
if path and path.lower() in arr_paths:
|
2021-12-06 09:16:43 +00:00
|
|
|
mismatched[path] = tmdb_id
|
|
|
|
continue
|
2021-12-30 16:32:18 +00:00
|
|
|
if path and not path.startswith(folder):
|
|
|
|
invalid_root.append(item)
|
|
|
|
continue
|
2021-11-24 16:47:21 +00:00
|
|
|
movie = self.api.get_movie(tmdb_id=tmdb_id)
|
2021-12-30 00:37:31 +00:00
|
|
|
if self.config.trace_mode:
|
|
|
|
logger.debug(f"Folder to Check: {folder}/{movie.folder}")
|
2021-12-28 22:41:04 +00:00
|
|
|
if f"{folder}/{movie.folder}".lower() in arr_paths:
|
2021-12-07 02:10:36 +00:00
|
|
|
path_in_use[f"{folder}/{movie.folder}"] = tmdb_id
|
|
|
|
continue
|
2021-12-06 09:16:43 +00:00
|
|
|
if path:
|
|
|
|
movies.append((movie, path))
|
|
|
|
path_lookup[path] = tmdb_id
|
|
|
|
else:
|
|
|
|
movies.append(movie)
|
2021-11-29 00:27:15 +00:00
|
|
|
except ArrException:
|
2021-11-24 16:47:21 +00:00
|
|
|
invalid.append(item)
|
|
|
|
if len(movies) == 100 or len(tmdb_ids) == i:
|
2021-12-30 16:32:18 +00:00
|
|
|
mass_add()
|
|
|
|
movies = []
|
|
|
|
if movies:
|
|
|
|
mass_add()
|
|
|
|
movies = []
|
2021-06-14 01:51:30 +00:00
|
|
|
|
|
|
|
if len(added) > 0:
|
2021-07-03 01:47:09 +00:00
|
|
|
logger.info("")
|
2021-06-14 01:51:30 +00:00
|
|
|
for movie in added:
|
2021-12-30 16:32:18 +00:00
|
|
|
logger.info(f"Added to Radarr | {movie.tmdbId:<7} | {movie.title}")
|
2021-11-29 15:24:36 +00:00
|
|
|
if self.config.Cache:
|
|
|
|
self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name)
|
2021-06-14 01:51:30 +00:00
|
|
|
logger.info(f"{len(added)} Movie{'s' if len(added) > 1 else ''} added to Radarr")
|
|
|
|
|
2021-12-06 09:16:43 +00:00
|
|
|
if len(exists) > 0 or len(skipped) > 0:
|
2021-07-03 01:47:09 +00:00
|
|
|
logger.info("")
|
2021-12-06 09:16:43 +00:00
|
|
|
if len(exists) > 0:
|
|
|
|
for movie in exists:
|
2021-12-30 16:32:18 +00:00
|
|
|
logger.info(f"Already in Radarr | {movie.tmdbId:<7} | {movie.title}")
|
2021-12-06 09:16:43 +00:00
|
|
|
if self.config.Cache:
|
|
|
|
self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name)
|
|
|
|
if len(skipped) > 0:
|
|
|
|
for movie in skipped:
|
|
|
|
logger.info(f"Skipped: In Cache | {movie}")
|
|
|
|
logger.info(f"{len(exists) + len(skipped)} Movie{'s' if len(skipped) > 1 else ''} already exist in Radarr")
|
2021-06-14 01:51:30 +00:00
|
|
|
|
2021-12-06 09:16:43 +00:00
|
|
|
if len(mismatched) > 0:
|
2021-12-01 13:17:28 +00:00
|
|
|
logger.info("")
|
2021-12-07 02:10:36 +00:00
|
|
|
logger.info("Items in Plex that have already been added to Radarr but under a different TMDb ID then in Plex")
|
2021-12-06 09:16:43 +00:00
|
|
|
for path, tmdb_id in mismatched.items():
|
2021-12-28 22:41:04 +00:00
|
|
|
logger.info(f"Plex TMDb ID: {tmdb_id:<7} | Radarr TMDb ID: {arr_paths[path.lower()]:<7} | Path: {path}")
|
2021-12-06 09:16:43 +00:00
|
|
|
logger.info(f"{len(mismatched)} Movie{'s' if len(mismatched) > 1 else ''} with mismatched TMDb IDs")
|
2021-12-01 13:17:28 +00:00
|
|
|
|
2021-12-07 02:10:36 +00:00
|
|
|
if len(path_in_use) > 0:
|
|
|
|
logger.info("")
|
|
|
|
logger.info("TMDb IDs that cannot be added to Radarr because the path they will use is already in use by a different TMDb ID")
|
|
|
|
for path, tmdb_id in path_in_use.items():
|
2021-12-28 22:41:04 +00:00
|
|
|
logger.info(f"TMDb ID: {tmdb_id:<7} | Radarr TMDb ID: {arr_paths[path.lower()]:<7} | Path: {path}")
|
2021-12-07 02:10:36 +00:00
|
|
|
logger.info(f"{len(path_in_use)} Movie{'s' if len(path_in_use) > 1 else ''} with paths already in use by other TMDb IDs")
|
|
|
|
|
2021-07-03 04:18:09 +00:00
|
|
|
if len(invalid) > 0:
|
2021-07-03 01:47:09 +00:00
|
|
|
logger.info("")
|
2021-07-03 04:18:09 +00:00
|
|
|
for tmdb_id in invalid:
|
|
|
|
logger.info(f"Invalid TMDb ID | {tmdb_id}")
|
2021-12-06 09:16:43 +00:00
|
|
|
logger.info(f"{len(invalid)} Movie{'s' if len(invalid) > 1 else ''} with Invalid IDs")
|
2021-07-03 04:18:09 +00:00
|
|
|
|
2021-12-30 16:32:18 +00:00
|
|
|
if len(invalid_root) > 0:
|
|
|
|
logger.info("")
|
|
|
|
for tmdb_id, path in invalid_root:
|
|
|
|
logger.info(f"Invalid Root Folder for TMDb ID | {tmdb_id:<7} | {path}")
|
|
|
|
logger.info(f"{len(invalid_root)} Movie{'s' if len(invalid_root) > 1 else ''} with Invalid Paths")
|
|
|
|
|
2022-01-25 06:30:07 +00:00
|
|
|
return added
|
2021-11-03 14:36:11 +00:00
|
|
|
|
2021-07-03 04:18:09 +00:00
|
|
|
def edit_tags(self, tmdb_ids, tags, apply_tags):
|
|
|
|
logger.info("")
|
|
|
|
logger.info(f"{apply_tags_translation[apply_tags].capitalize()} Radarr Tags: {tags}")
|
|
|
|
|
2021-08-26 14:35:29 +00:00
|
|
|
edited, not_exists = self.api.edit_multiple_movies(tmdb_ids, tags=tags, apply_tags=apply_tags_translation[apply_tags], per_request=100)
|
2021-07-03 04:18:09 +00:00
|
|
|
|
|
|
|
if len(edited) > 0:
|
|
|
|
logger.info("")
|
|
|
|
for movie in edited:
|
|
|
|
logger.info(f"Radarr Tags | {movie.title:<25} | {movie.tags}")
|
|
|
|
logger.info(f"{len(edited)} Movie{'s' if len(edited) > 1 else ''} edited in Radarr")
|
|
|
|
|
|
|
|
if len(not_exists) > 0:
|
|
|
|
logger.info("")
|
|
|
|
for tmdb_id in not_exists:
|
|
|
|
logger.info(f"TMDb ID Not in Radarr | {tmdb_id}")
|
2021-12-18 22:45:09 +00:00
|
|
|
|
|
|
|
def remove_all_with_tags(self, tags):
|
|
|
|
lower_tags = [_t.lower() for _t in tags]
|
|
|
|
remove_items = []
|
|
|
|
for movie in self.api.all_movies():
|
|
|
|
tag_strs = [_t.label.lower() for _t in movie.tags]
|
|
|
|
remove = True
|
|
|
|
for tag in lower_tags:
|
|
|
|
if tag not in tag_strs:
|
|
|
|
remove = False
|
|
|
|
break
|
|
|
|
if remove:
|
|
|
|
remove_items.append(movie)
|
|
|
|
if remove_items:
|
|
|
|
self.api.delete_multiple_movies(remove_items)
|