-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.py
75 lines (59 loc) · 2.06 KB
/
Storage.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
FILE_NAME = 'users.json'
def loadFile(): # Carrega arquivo
from os.path import isfile
if isfile(FILE_NAME):
arq = open(FILE_NAME, 'r')
file = json.load(arq)
arq.close()
return file
else:
return dict()
def updateFile(users): # Reescreve o arquivo
arq = open(FILE_NAME, 'w') # Re-cria o arquivo
json.dump(users, arq, indent=4)
arq.close()
def updateAlert(chatid, users, alert):
if chatid not in users:
users[chatid] = list()
users[chatid].append(alert)
updateFile(users)
def createAlert(message, day, hour): # Cria objeto no padrão para armazenamento
from datetime import datetime
today = datetime.today()
d, m, y, h, mn = formatDateHour(today, day, hour)
dt = datetime(y, m, d, h, mn)
return {
'message': message,
'day': dt.strftime('%d/%m/%Y'),
'hour': dt.strftime('%H:%M'),
# 'createdAt': {'day': today.strftime('%d/%m/%Y'), 'hour': today.strftime('%H:%M:%S')},
'enabled': True
}
def formatDateHour(today, day, hour):
d = day.split('/')
if len(d) == 1:
d.append(today.month)
if len(d) == 2:
d.append(today.year)
h = hour[:5].split(':')
if len(h) == 1:
h.append(0)
d, m, y = [int(i) for i in d]
h, mn = [int(i) for i in h]
return d, m, y, h, mn
def getNumEnabledAlerts(user): # Pega número de alerta ativados do usuário especificado
n = 0
for alert in user:
if alert['enabled']:
n += 1
return n # return sum([1 for alert in user if alert['enabled']])
def getAlertList(chatid, users): # Retorna lista dos alertas ativados
if chatid not in users:
return '*Não há nenhum lembrete ativo em sua agenda.*'
user = users[chatid]
lst = f'*Existe(m) {getNumEnabledAlerts(user)} lembrete(s) ativo(s) em sua agenda.*\n\n'
for alert in user:
if alert['enabled']:
lst += f':white_check_mark: :memo:*{alert["message"]:10}* {"-":^5} :date:{alert["day"]} _às_ :watch:{alert["hour"]}\n'
return lst