From cc2ce8851b08850e8daa427e3458c4ed3ee5887f Mon Sep 17 00:00:00 2001 From: GaMeNu <98153342+GaMeNu@users.noreply.github.com> Date: Sun, 15 Oct 2023 11:47:45 +0300 Subject: [PATCH] v2.1.3 Attempt to fix: JSONDecodeError, InternalError: Unread result found as per issue #004 --- cog_notificator.py | 25 +++++++++++++++++++------ db_access.py | 4 ++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cog_notificator.py b/cog_notificator.py index 1ed7bd0..b1e8760 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -33,14 +33,17 @@ class AlertReqs: decoded = req.content.decode('utf-8-sig') if decoded is None or len(decoded) < 3: # Why does it get a '\r\n' wtf - ret_dict = None + ret_dict = {} else: - ret_dict = json.loads(decoded) + try: + ret_dict = json.loads(decoded) + except (json.decoder.JSONDecodeError, json.JSONDecodeError): + ret_dict = None return ret_dict @staticmethod - def request_history_json() -> dict: + def request_history_json() -> dict | None: """ Request a json of the alert history from last day :return: JSON object as Python dict @@ -50,7 +53,11 @@ class AlertReqs: content = req.text - return json.loads(content) + try: + ret_dict = json.loads(content) + except (json.JSONDecodeError, json.decoder.JSONDecodeError): + ret_dict = None + return ret_dict class Alert: @@ -63,7 +70,10 @@ class Alert: @staticmethod def from_dict(data: dict): - return Alert(int(data.get('id', '0')), int(data.get('cat', '0')), data.get('title'), data.get('data'), + return Alert(int(data.get('id', '0')), + int(data.get('cat', '0')), + data.get('title'), + data.get('data'), data.get('desc')) @@ -108,7 +118,9 @@ class Notificator(commands.Cog): return self.log.debug(f'Alert response: {current_alert}') - if current_alert is None: + if current_alert is None or len(current_alert) == 0: + if current_alert is None: + self.log.warning('Error while current alert data.') if len(self.active_districts) == 0: return @@ -152,6 +164,7 @@ class Notificator(commands.Cog): @staticmethod def generate_alert_embed(alert_object: Alert, district: str, arrival_time: int | None, time: str, lang: str) -> discord.Embed: + # TODO: Using 1 generate alert function is probably bad, should probably split into a utility class e = discord.Embed(color=discord.Color.from_str('#FF0000')) e.title = f'התראה ב{district}' e.add_field(name=district, value=alert_object.title, inline=False) diff --git a/db_access.py b/db_access.py index 3d749a4..ed8538f 100644 --- a/db_access.py +++ b/db_access.py @@ -93,6 +93,7 @@ class DBAccess: with self.connection.cursor() as crsr: crsr.execute('SELECT * FROM areas WHERE area_id=%s', (id,)) res = crsr.fetchone() + crsr.fetchall() if res is not None: return Area(res[0], res[1]) @@ -103,6 +104,7 @@ class DBAccess: with self.connection.cursor() as crsr: crsr.execute('SELECT * FROM districts WHERE district_id=%s', (id,)) res = crsr.fetchone() + crsr.fetchall() if res is not None: return District(res[0], res[1], res[2], res[3]) @@ -116,6 +118,7 @@ class DBAccess: with self.connection.cursor() as crsr: crsr.execute('SELECT * FROM servers WHERE server_id=%s', (id,)) res = crsr.fetchone() + crsr.fetchall() if res is not None: return Server(res[0], res[1]) @@ -126,6 +129,7 @@ class DBAccess: with self.connection.cursor() as crsr: crsr.execute('SELECT * FROM channels WHERE channel_id=%s', (id,)) res = crsr.fetchone() + crsr.fetchall() if res is not None: return Channel(res[0], res[1], res[2])