From eaf0c2fe5d7e6a5aead1e51a89fa5e291c419166 Mon Sep 17 00:00:00 2001 From: meisnate12 Date: Wed, 21 Sep 2022 11:34:57 -0400 Subject: [PATCH] [39] reduce anidb calls --- VERSION | 2 +- modules/anidb.py | 10 +++++++++- modules/cache.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 615a4ba3..6a9268ad 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.17.3-develop38 +1.17.3-develop39 diff --git a/modules/anidb.py b/modules/anidb.py index d0d0095b..acfa4d9f 100644 --- a/modules/anidb.py +++ b/modules/anidb.py @@ -83,13 +83,21 @@ class AniDB: def authorize(self, client, version, expiration): self.client = client self.version = version - logger.secret(self.client) self.expiration = expiration + logger.secret(self.client) + if self.config.Cache: + value1, value2, success = self.config.Cache.query_testing("anidb_login") + if str(value1) == str(client) and str(value2) == str(version) and success: + return try: self.get_anime(69, ignore_cache=True) + if self.config.Cache: + self.config.Cache.update_testing("anidb_login", self.client, self.version, "True") except Failed: self.client = None self.version = None + if self.config.Cache: + self.config.Cache.update_testing("anidb_login", self.client, self.version, "False") raise @property diff --git a/modules/cache.py b/modules/cache.py index eed5bde5..d4214652 100644 --- a/modules/cache.py +++ b/modules/cache.py @@ -268,6 +268,14 @@ class Cache: type TEXT, text TEXT)""" ) + cursor.execute( + """CREATE TABLE IF NOT EXISTS testing ( + key INTEGER PRIMARY KEY, + name TEXT, + value1 TEXT, + value2 TEXT, + success 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") @@ -935,3 +943,26 @@ class Cache: with closing(connection.cursor()) as cursor: cursor.execute("INSERT OR IGNORE INTO overlay_special_text(rating_key, type) VALUES(?, ?)", (rating_key, data_type)) cursor.execute("UPDATE overlay_special_text SET text = ? WHERE rating_key = ? AND type = ?", (text, rating_key, data_type)) + + def query_testing(self, name): + value1 = None + value2 = None + success = None + with sqlite3.connect(self.cache_path) as connection: + connection.row_factory = sqlite3.Row + with closing(connection.cursor()) as cursor: + cursor.execute(f"SELECT * FROM testing WHERE name = ?", (name,)) + row = cursor.fetchone() + if row: + value1 = row["value1"] + value2 = row["value2"] + success = True if row["success"] == "True" else False + return value1, value2, success + + def update_testing(self, name, value1, value2, success): + 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 testing(name) VALUES(?)", (name,)) + sql = f"UPDATE testing SET value1 = ?, value2 = ?, success = ? WHERE name = ?" + cursor.execute(sql, (value1, value2, success, name))