give omdb and mdblist their own cache lengths

pull/707/head
meisnate12 3 years ago
parent b4f1086dbc
commit 16049b7a45

@ -1 +1 @@
1.15.1-develop60 1.15.1-develop61

@ -270,7 +270,7 @@ class Cache:
sql = f"UPDATE {map_name} SET {val2_name} = ?, expiration_date = ?{'' if media_type is None else ', media_type = ?'} WHERE {val1_name} = ?" sql = f"UPDATE {map_name} SET {val2_name} = ?, expiration_date = ?{'' if media_type is None else ', media_type = ?'} WHERE {val1_name} = ?"
cursor.execute(sql, (val2, expiration_date.strftime("%Y-%m-%d"), media_type, val1)) cursor.execute(sql, (val2, expiration_date.strftime("%Y-%m-%d"), media_type, val1))
def query_omdb(self, imdb_id): def query_omdb(self, imdb_id, expiration):
omdb_dict = {} omdb_dict = {}
expired = None expired = None
with sqlite3.connect(self.cache_path) as connection: with sqlite3.connect(self.cache_path) as connection:
@ -294,11 +294,11 @@ class Cache:
omdb_dict["Response"] = "True" omdb_dict["Response"] = "True"
datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d") datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d")
time_between_insertion = datetime.now() - datetime_object time_between_insertion = datetime.now() - datetime_object
expired = time_between_insertion.days > self.expiration expired = time_between_insertion.days > expiration
return omdb_dict, expired return omdb_dict, expired
def update_omdb(self, expired, omdb): def update_omdb(self, expired, omdb, expiration):
expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, self.expiration))) expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, expiration)))
with sqlite3.connect(self.cache_path) as connection: with sqlite3.connect(self.cache_path) as connection:
connection.row_factory = sqlite3.Row connection.row_factory = sqlite3.Row
with closing(connection.cursor()) as cursor: with closing(connection.cursor()) as cursor:
@ -311,7 +311,7 @@ class Cache:
omdb.series_id, omdb.season_num, omdb.episode_num, omdb.series_id, omdb.season_num, omdb.episode_num,
expiration_date.strftime("%Y-%m-%d"), omdb.imdb_id)) expiration_date.strftime("%Y-%m-%d"), omdb.imdb_id))
def query_mdb(self, key_id): def query_mdb(self, key_id, expiration):
mdb_dict = {} mdb_dict = {}
expired = None expired = None
with sqlite3.connect(self.cache_path) as connection: with sqlite3.connect(self.cache_path) as connection:
@ -341,11 +341,11 @@ class Cache:
] ]
datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d") datetime_object = datetime.strptime(row["expiration_date"], "%Y-%m-%d")
time_between_insertion = datetime.now() - datetime_object time_between_insertion = datetime.now() - datetime_object
expired = time_between_insertion.days > self.expiration expired = time_between_insertion.days > expiration
return mdb_dict, expired return mdb_dict, expired
def update_mdb(self, expired, key_id, mdb): def update_mdb(self, expired, key_id, mdb, expiration):
expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, self.expiration))) expiration_date = datetime.now() if expired is True else (datetime.now() - timedelta(days=random.randint(1, expiration)))
with sqlite3.connect(self.cache_path) as connection: with sqlite3.connect(self.cache_path) as connection:
connection.row_factory = sqlite3.Row connection.row_factory = sqlite3.Row
with closing(connection.cursor()) as cursor: with closing(connection.cursor()) as cursor:

@ -377,7 +377,10 @@ class ConfigFile:
if "omdb" in self.data: if "omdb" in self.data:
logger.info("Connecting to OMDb...") logger.info("Connecting to OMDb...")
try: try:
self.OMDb = OMDb(self, {"apikey": check_for_attribute(self.data, "apikey", parent="omdb", throw=True)}) self.OMDb = OMDb(self, {
"apikey": check_for_attribute(self.data, "apikey", parent="omdb", throw=True),
"cache_expiration": check_for_attribute(self.data, "cache_expiration", parent="settings", var_type="int", default=60)
})
except Failed as e: except Failed as e:
self.errors.append(e) self.errors.append(e)
logger.error(e) logger.error(e)
@ -391,7 +394,10 @@ class ConfigFile:
if "mdblist" in self.data: if "mdblist" in self.data:
logger.info("Connecting to Mdblist...") logger.info("Connecting to Mdblist...")
try: try:
self.Mdblist.add_key(check_for_attribute(self.data, "apikey", parent="mdblist", throw=True)) self.Mdblist.add_key(
check_for_attribute(self.data, "apikey", parent="mdblist", throw=True),
check_for_attribute(self.data, "cache_expiration", parent="settings", var_type="int", default=60)
)
logger.info("Mdblist Connection Successful") logger.info("Mdblist Connection Successful")
except Failed as e: except Failed as e:
self.errors.append(e) self.errors.append(e)

@ -55,10 +55,12 @@ class Mdblist:
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.apikey = None self.apikey = None
self.expiration = 60
self.limit = False self.limit = False
def add_key(self, apikey): def add_key(self, apikey, expiration):
self.apikey = apikey self.apikey = apikey
self.expiration = expiration
try: try:
self._request(imdb_id="tt0080684", ignore_cache=True) self._request(imdb_id="tt0080684", ignore_cache=True)
except Failed: except Failed:
@ -82,7 +84,7 @@ class Mdblist:
raise Failed("MdbList Error: Either IMDb ID or TMDb ID and TMDb Type Required") raise Failed("MdbList Error: Either IMDb ID or TMDb ID and TMDb Type Required")
expired = None expired = None
if self.config.Cache and not ignore_cache: if self.config.Cache and not ignore_cache:
mdb_dict, expired = self.config.Cache.query_mdb(key) mdb_dict, expired = self.config.Cache.query_mdb(key, self.expiration)
if mdb_dict and expired is False: if mdb_dict and expired is False:
return MDbObj(mdb_dict) return MDbObj(mdb_dict)
if self.config.trace_mode: if self.config.trace_mode:
@ -95,7 +97,7 @@ class Mdblist:
else: else:
mdb = MDbObj(response) mdb = MDbObj(response)
if self.config.Cache and not ignore_cache: if self.config.Cache and not ignore_cache:
self.config.Cache.update_mdb(expired, key, mdb) self.config.Cache.update_mdb(expired, key, mdb, self.expiration)
return mdb return mdb
def get_imdb(self, imdb_id): def get_imdb(self, imdb_id):

@ -52,13 +52,14 @@ class OMDb:
def __init__(self, config, params): def __init__(self, config, params):
self.config = config self.config = config
self.apikey = params["apikey"] self.apikey = params["apikey"]
self.expiration = params["expiration"]
self.limit = False self.limit = False
self.get_omdb("tt0080684", ignore_cache=True) self.get_omdb("tt0080684", ignore_cache=True)
def get_omdb(self, imdb_id, ignore_cache=False): def get_omdb(self, imdb_id, ignore_cache=False):
expired = None expired = None
if self.config.Cache and not ignore_cache: if self.config.Cache and not ignore_cache:
omdb_dict, expired = self.config.Cache.query_omdb(imdb_id) omdb_dict, expired = self.config.Cache.query_omdb(imdb_id, self.expiration)
if omdb_dict and expired is False: if omdb_dict and expired is False:
return OMDbObj(imdb_id, omdb_dict) return OMDbObj(imdb_id, omdb_dict)
if self.config.trace_mode: if self.config.trace_mode:
@ -67,7 +68,7 @@ class OMDb:
if response.status_code < 400: if response.status_code < 400:
omdb = OMDbObj(imdb_id, response.json()) omdb = OMDbObj(imdb_id, response.json())
if self.config.Cache and not ignore_cache: if self.config.Cache and not ignore_cache:
self.config.Cache.update_omdb(expired, omdb) self.config.Cache.update_omdb(expired, omdb, self.expiration)
return omdb return omdb
else: else:
error = response.json()['Error'] error = response.json()['Error']

Loading…
Cancel
Save