mirror of
https://github.com/GaMeNu/HFCNotificator.git
synced 2024-11-16 15:24:51 +02:00
v2.1.3
Attempt to fix: JSONDecodeError, InternalError: Unread result found as per issue #004
This commit is contained in:
parent
2e289131e8
commit
cc2ce8851b
@ -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:
|
||||
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 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)
|
||||
|
@ -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])
|
||||
|
Loading…
Reference in New Issue
Block a user