forked from Ya-s-h/db-lab-autmation-hax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_processing.py
More file actions
160 lines (136 loc) · 5.61 KB
/
Copy pathdata_processing.py
File metadata and controls
160 lines (136 loc) · 5.61 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
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
import datetime
import json
from sys import stderr
from typing import List
from classes.Database import Database, EventAlreadyLockedException
from classes.QuestionData import QuestionData
from classes.Webhook import Webhook
def process_data(data: List[dict], database: Database, webhook: Webhook):
new_questions: List[QuestionData] = []
updated_questions: List[QuestionData] = []
for question in data:
question["class_name"] = question["class"]
question["q_type"] = question["question_type"]
del question["class"], question["question_type"]
question = QuestionData(**question)
# check if current event exists in database
if question.primary_hash in database:
continue
# check if secondary hash exists in db
elif question.secondary_hash in database:
# if yes, then the event was updated
updated_questions.append(question)
else:
# otherwise, this is a new event
new_questions.append(question)
process_updated_questions(updated_questions, database, webhook)
process_new_questions(new_questions, database, webhook)
def process_updated_questions(events: List[QuestionData], database: Database, webhook: Webhook):
if not events:
return
print("Processing updated events...")
__processed = 0
for event in events:
old_q = database.get_via_secondary_hash(event.secondary_hash)
diff = old_q.diff(event)
# delete old message
webhook.delete_message(old_q)
# if this fails, skip and move to next
try:
# first lock this event
database.lock(event)
# Sanity check if another instance didn't do stuff
assert old_q.primary_hash in database, "old question has been purged probably by another instance."
event.message_id = webhook.send_message(event,
'`' + "—" * 30 + "`\nUPDATED: " + diff + "\n",
'\n`' + "—" * 30 + '`')
except ValueError:
print("no message id received, skipping over: ", event, file=stderr)
except AssertionError as e:
print(e, file=stderr)
except EventAlreadyLockedException:
print("Another instance already locked", event.question)
else:
# now delete old entry and add new entry
database.remove(old_q)
database.insert(event)
__processed += 1
finally:
database.unlock(event)
print("Processed", __processed, "updated events")
def process_new_questions(events: List[QuestionData], database: Database, webhook: Webhook):
if not events:
return
print("Processing new events...")
events.sort(key=lambda x: x.due_date)
__sent = 0
dashes = "" #"`" + "-" * 30 + "`"
for event in events:
# skip practise problems
if event.q_type == "P":
continue
try:
# Lock the current event first
database.lock(event)
# sanity check if another instance didn't send the message already
assert event.primary_hash not in database, "Another instance already sent this event's webhook"
event.message_id = webhook.send_message(event, dashes + "\n", "\n" + dashes)
except ValueError:
print("no message id recieved, skipping over: ", event, file=stderr)
except AssertionError as e:
print(e, file=stderr)
except EventAlreadyLockedException:
print("Another instance already locked", event.question)
else:
database.insert(event)
__sent += 1
finally:
database.unlock(event)
print("Sent", __sent, "new webhook messages")
def purge_old_questions(database: Database, webhook: Webhook):
old_secondary_hashes = []
purged = 0
with database as cursor:
query = "SELECT secondary_hash FROM question_data WHERE due_date < ?"
for output in cursor.execute(query, (datetime.datetime.now(),)):
old_secondary_hashes.append(output[0])
for hash_ in old_secondary_hashes:
try:
old_event = database.get_via_secondary_hash(hash_)
except ValueError:
print("another instance already deleted", hash_)
continue
else:
deleted = webhook.delete_message(old_event)
if deleted:
database.remove(old_event)
purged += 1
if purged > 0:
print("Purged", purged, "expired events")
if __name__ == '__main__':
import os, yaml
from dotenv import load_dotenv
load_dotenv()
def date_hook(json_dict):
for (key, value) in json_dict.items():
if json_dict[key] == "0001-01-01 00:00:00":
json_dict[key] = datetime.datetime.min
else:
try:
json_dict[key] = datetime.datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
except ValueError:
pass
return json_dict
with open("test_data.json") as file:
data = json.load(file, object_hook=date_hook)
# print(data)
db = Database(folder="db/")
webhook = Webhook(os.getenv("WEBHOOK_URL"),
avatar=os.getenv("AVATAR_URL"),
username="bone")
process_data(data, db, webhook)
purge_old_questions(db, webhook)
# input("> press to delete all webhooks")
# with db as cursor:
# for msg_id in cursor.execute("SELECT message_id FROM message_ids"):
# webhook.delete_message(str(msg_id))