-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
196 lines (148 loc) · 5.24 KB
/
database.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from botFunctions import decrypt
import os
from oauth2client.service_account import ServiceAccountCredentials
import gspread
import datetime
import re
# print(vars(botFunctions))
class Dummy:
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
scope = ["https://spreadsheets.google.com/feeds",
'https://www.googleapis.com/auth/spreadsheets',
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"]
with open("thebotdbCredentials-encrypted.nottxt", "rb") as f:
with open("thebotdb-creds-decrypted.bsecret", "w") as f2:
try:
encryptionKey = int(os.environ.get("encrypt", 0))
fr = f.read()
f2.write(decrypt(fr.decode("utf-8"), encryptionKey))
except Exception as e:
print(e)
# with open("thebotdbCredentials-encrypted.nottxt", "wb") as f:
# with open("TheBotBD-credentials.bsecret", "r") as f2:
# #print(f2.read())
# fr = f2.read()
# encrypted = botFunctions.encrypt(fr,key)
# print(encrypted)
# f.write(bytes(botFunctions.encrypt(fr,key),"utf-8"))
try:
creds = ServiceAccountCredentials.from_json_keyfile_name(
"thebotdb-creds-decrypted.bsecret", scope)
except Exception:
creds = ServiceAccountCredentials.from_json_keyfile_name(
"TheBotBD-credentials.bsecret", scope)
sheetClient = gspread.authorize(creds)
# print(sheetClient.open("TheBotDB").worksheets())
commandLog = sheetClient.open("TheBotDB").worksheet("Command Log")
messageLog = sheetClient.open("TheBotDB").worksheet("Message Log")
dataLog = sheetClient.open("TheBotDB").worksheet("Database")
advantniKalendar = sheetClient.open("TheBotDB").worksheet("AK")
timers = sheetClient.open("TheBotDB").worksheet("Timers")
birthdays = sheetClient.open("TheBotDB").worksheet("BDays")
iedb = sheetClient.open("TheBotDB").worksheet("IEBot")
covidTipsSheet = sheetClient.open("TheBotDB").worksheet("CovidTips")
def getBDays():
values = birthdays.get_all_values()
captions = values[0]
values = values[1:]
bDicts = [dict(zip(captions, v)) for v in values]
for i in bDicts:
for k in i:
if k in ["GuildIDs", "ChannelIDs"]:
i[k] = list(map(int, i[k].split(",")))
if k == "Birthdate":
i[k] = datetime.date.fromisoformat(i[k])
return bDicts
def getTodayBDays(today=None):
if today is None:
today = datetime.date.today()
bDays = getBDays()
for i in bDays:
if (
i["Birthdate"].month == today.month
and i["Birthdate"].day == today.day
):
yield i
def getGuildBDays(guild):
for i in getBDays():
if guild in i["GuildIDs"]:
yield i
def lastCheckedBDay():
lastDateStr = dataLog.cell(2, 4).value
return datetime.date.fromisoformat(lastDateStr)
def updateLastCheckedBDay(today=None):
if today is None:
today = datetime.date.today()
dataLog.update_cell(2, 4, today.isoformat())
def getIEData():
values = iedb.get_all_values()
captions = values[0]
values = values[1:]
ieDicts = [dict(zip(captions, v)) for v in values]
for i in ieDicts:
for k in i:
if k == "time":
i[k] = datetime.datetime.fromisoformat(i[k])
if k == "eventIndex":
i[k] = int(i[k])
return ieDicts
def addIEData(time, text, index):
if time in getIEDataTimes():
return False
iedb.append_row([time.isoformat(), text, index])
return True
def getIEDataT(time):
for i in getIEData():
if i["time"] == time:
return i
def deleteIEData(time):
indexes = [i for i, t in enumerate(getIEDataTimes()) if t == time]
for i in indexes:
iedb.delete_row(i+1)
def getIEDataTimes():
return ["time"]+[
datetime.datetime.fromisoformat(i) for i in iedb.col_values(1)[1:]
]
def getLastCovidTime():
return dataLog.cell(2, 5).value
def setLastCovidTime(time):
return dataLog.update_cell(2, 5, time)
def getCovidTips():
values = covidTipsSheet.get_all_values()
captions = values[0]
values = values[1:]
covidTipsData = [dict(zip(captions, v)) for v in values]
for i in covidTipsData:
for k in i:
if k == "date":
i[k] = datetime.datetime.fromisoformat(i[k])
if k == "userID":
i[k] = int(i[k])
if k == "number":
i[k] = int(i[k])
return covidTipsData
def getCovidTipsDate(date):
covidTips = getCovidTips()
covidTips = [i for i in covidTips if i["date"].date() == date]
users = set()
for tip in covidTips:
if tip["userID"] not in users:
users.add(tip["userID"])
yield tip
def setCovidTip(date: datetime.date, tip, user, twitterUsername=""):
isTwitterUsername = re.match(r"(\b[A-Za-z0-9]+\b)", twitterUsername)
if isTwitterUsername and isTwitterUsername.groups()[0] == twitterUsername:
twitterUsername = "@" + twitterUsername[:15]
else:
twitterUsername = twitterUsername[:25]
covidTipsSheet.append_row([
date.isoformat(),
str(user)
if not twitterUsername
else twitterUsername,
str(user.id),
str(tip)
])