diff --git a/modules/cache.py b/modules/cache.py index 260f9d7b..366308a9 100644 --- a/modules/cache.py +++ b/modules/cache.py @@ -96,6 +96,18 @@ class Cache: key INTEGER PRIMARY KEY, library TEXT UNIQUE)""" ) + cursor.execute( + """CREATE TABLE IF NOT EXISTS radarr_adds ( + key INTEGER PRIMARY KEY, + tmdb_id TEXT, + library TEXT)""" + ) + cursor.execute( + """CREATE TABLE IF NOT EXISTS sonarr_adds ( + key INTEGER PRIMARY KEY, + tvdb_id TEXT, + library TEXT)""" + ) cursor.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='image_map'") if cursor.fetchone()[0] > 0: cursor.execute(f"SELECT DISTINCT library FROM image_map") @@ -338,3 +350,31 @@ class Cache: with closing(connection.cursor()) as cursor: cursor.execute(f"INSERT OR IGNORE INTO {table_name}(rating_key) VALUES(?)", (rating_key,)) cursor.execute(f"UPDATE {table_name} SET location = ?, compare = ?, overlay = ? WHERE rating_key = ?", (location, compare, overlay, rating_key)) + + def query_radarr_adds(self, tmdb_id, library): + return self.query_arr_adds(tmdb_id, library, "radarr", "tmdb_id") + + def query_sonarr_adds(self, tvdb_id, library): + return self.query_arr_adds(tvdb_id, library, "sonarr", "tvdb_id") + + def query_arr_adds(self, t_id, library, arr, id_type): + with sqlite3.connect(self.cache_path) as connection: + connection.row_factory = sqlite3.Row + with closing(connection.cursor()) as cursor: + cursor.execute(f"SELECT * FROM {arr}_adds WHERE {id_type} = ? AND library = ?", (t_id, library)) + row = cursor.fetchone() + if row and row[id_type]: + return int(row[id_type]) + return None + + def update_radarr_adds(self, tmdb_id, library): + return self.update_arr_adds(tmdb_id, library, "radarr", "tmdb_id") + + def update_sonarr_adds(self, tvdb_id, library): + return self.update_arr_adds(tvdb_id, library, "sonarr", "tvdb_id") + + def update_arr_adds(self, t_id, library, arr, id_type): + with sqlite3.connect(self.cache_path) as connection: + connection.row_factory = sqlite3.Row + with closing(connection.cursor()) as cursor: + cursor.execute(f"INSERT OR IGNORE INTO {arr}_adds({id_type}, library) VALUES(?)", (t_id, library)) diff --git a/modules/config.py b/modules/config.py index b04fffc5..987c2533 100644 --- a/modules/config.py +++ b/modules/config.py @@ -517,7 +517,7 @@ class Config: logger.info(f"Connecting to {display_name} library's Radarr...") logger.info("") try: - library.Radarr = Radarr(self, { + library.Radarr = Radarr(self, library, { "url": check_for_attribute(lib, "url", parent="radarr", var_type="url", default=self.general["radarr"]["url"], req_default=True, save=False), "token": check_for_attribute(lib, "token", parent="radarr", default=self.general["radarr"]["token"], req_default=True, save=False), "add": check_for_attribute(lib, "add", parent="radarr", var_type="bool", default=self.general["radarr"]["add"], save=False), @@ -545,7 +545,7 @@ class Config: logger.info(f"Connecting to {display_name} library's Sonarr...") logger.info("") try: - library.Sonarr = Sonarr(self, { + library.Sonarr = Sonarr(self, library, { "url": check_for_attribute(lib, "url", parent="sonarr", var_type="url", default=self.general["sonarr"]["url"], req_default=True, save=False), "token": check_for_attribute(lib, "token", parent="sonarr", default=self.general["sonarr"]["token"], req_default=True, save=False), "add": check_for_attribute(lib, "add", parent="sonarr", var_type="bool", default=self.general["sonarr"]["add"], save=False), @@ -576,7 +576,7 @@ class Config: logger.info(f"Connecting to {display_name} library's Tautulli...") logger.info("") try: - library.Tautulli = Tautulli(self, { + library.Tautulli = Tautulli(self, library, { "url": check_for_attribute(lib, "url", parent="tautulli", var_type="url", default=self.general["tautulli"]["url"], req_default=True, save=False), "apikey": check_for_attribute(lib, "apikey", parent="tautulli", default=self.general["tautulli"]["apikey"], req_default=True, save=False) }) diff --git a/modules/radarr.py b/modules/radarr.py index 79b986eb..84c71ced 100644 --- a/modules/radarr.py +++ b/modules/radarr.py @@ -11,8 +11,9 @@ apply_tags_translation = {"": "add", "sync": "replace", "remove": "remove"} availability_descriptions = {"announced": "For Announced", "cinemas": "For In Cinemas", "released": "For Released", "db": "For PreDB"} class Radarr: - def __init__(self, config, params): + def __init__(self, config, library, params): self.config = config + self.library = library self.url = params["url"] self.token = params["token"] try: @@ -52,6 +53,11 @@ class Radarr: path = item[1] if isinstance(item, tuple) else None tmdb_id = item[0] if isinstance(item, tuple) else item util.print_return(f"Loading TMDb ID {i}/{len(tmdb_ids)} ({tmdb_id})") + if self.config.Cache: + _id = self.config.Cache.query_radarr_adds(tmdb_id, self.library.original_mapping_name) + if _id: + exists.append(item) + continue try: movie = self.api.get_movie(tmdb_id=tmdb_id) movies.append((movie, path) if path else movie) @@ -72,12 +78,16 @@ class Radarr: logger.info("") for movie in added: logger.info(f"Added to Radarr | {movie.tmdbId:<6} | {movie.title}") + if self.config.Cache: + self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name) logger.info(f"{len(added)} Movie{'s' if len(added) > 1 else ''} added to Radarr") if len(exists) > 0: logger.info("") for movie in exists: logger.info(f"Already in Radarr | {movie.tmdbId:<6} | {movie.title}") + if self.config.Cache: + self.config.Cache.update_radarr_adds(movie.tmdbId, self.library.original_mapping_name) logger.info(f"{len(exists)} Movie{'s' if len(exists) > 1 else ''} already existing in Radarr") if len(invalid) > 0: diff --git a/modules/sonarr.py b/modules/sonarr.py index 487a17ba..f68ad76d 100644 --- a/modules/sonarr.py +++ b/modules/sonarr.py @@ -29,8 +29,9 @@ monitor_descriptions = { apply_tags_translation = {"": "add", "sync": "replace", "remove": "remove"} class Sonarr: - def __init__(self, config, params): + def __init__(self, config, library, params): self.config = config + self.library = library self.url = params["url"] self.token = params["token"] try: @@ -78,10 +79,15 @@ class Sonarr: path = item[1] if isinstance(item, tuple) else None tvdb_id = item[0] if isinstance(item, tuple) else item util.print_return(f"Loading TVDb ID {i}/{len(tvdb_ids)} ({tvdb_id})") + if self.config.Cache: + _id = self.config.Cache.query_sonarr_adds(tvdb_id, self.library.original_mapping_name) + if _id: + exists.append(item) + continue try: show = self.api.get_series(tvdb_id=tvdb_id) shows.append((show, path) if path else show) - except NotFound: + except ArrException: invalid.append(item) if len(shows) == 100 or len(tvdb_ids) == i: try: @@ -98,12 +104,16 @@ class Sonarr: logger.info("") for series in added: logger.info(f"Added to Sonarr | {series.tvdbId:<6} | {series.title}") + if self.config.Cache: + self.config.Cache.update_sonarr_adds(series.tvdbId, self.library.original_mapping_name) logger.info(f"{len(added)} Series added to Sonarr") if len(exists) > 0: logger.info("") for series in exists: logger.info(f"Already in Sonarr | {series.tvdbId:<6} | {series.title}") + if self.config.Cache: + self.config.Cache.update_sonarr_adds(series.tvdbId, self.library.original_mapping_name) logger.info(f"{len(exists)} Series already existing in Sonarr") if len(invalid) > 0: diff --git a/modules/tautulli.py b/modules/tautulli.py index 3238f161..a2c7cdf2 100644 --- a/modules/tautulli.py +++ b/modules/tautulli.py @@ -11,8 +11,9 @@ logger = logging.getLogger("Plex Meta Manager") builders = ["tautulli_popular", "tautulli_watched"] class Tautulli: - def __init__(self, config, params): + def __init__(self, config, library, params): self.config = config + self.library = library self.url = params["url"] self.apikey = params["apikey"] try: