-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCivUrlHandler.py
More file actions
61 lines (51 loc) · 1.47 KB
/
CivUrlHandler.py
File metadata and controls
61 lines (51 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import base64
import logging
import json
from json import JSONDecodeError
import binascii
import traceback
class CivUrlHandler:
def getPathForNotification(self, bottype, userid):
"""
Generates a Path for the Notification defined by bottype and userid
Parameters
----------
bottype : str
The type of bot for this Notification (e.g. "Telegram", "Discord")
userid : string
The id for the chat (or similar) for the bot to use to send the message to the right chat.
Returns
-------
Path (string)
The Path for this Notification containing a base64 encoded json
"""
notification = [{"type": bottype, "userid" : userid}]
jNotification = json.dumps(notification)
return base64.urlsafe_b64encode(jNotification.encode('utf-8')).decode("utf-8")
def getNotificationsForPath(self, path):
"""
Gets the Notifications for the given path
Parameters
----------
path : str
The Path to get Notifications for.
Returns
-------
List
List of Notifications for given Path
OR
None
If given Path does not exist in Database.
"""
try:
decoded = base64.urlsafe_b64decode(path)
return json.loads(decoded.decode("utf-8"))
except JSONDecodeError as e:
traceback.print_exc()
logging.error(e.msg)
return None
except binascii.Error as e:
traceback.print_exc()
logging.error(str(e))
return None