This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwacovidmailer.py
987 lines (721 loc) · 29.2 KB
/
wacovidmailer.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
#!/usr/bin/env python3
from datetime import date, datetime
from html.parser import HTMLParser
from pprint import pprint
import codecs
import csv
import json
import re
import lxml.html
import os
import pytz
import requests
import shutil
import smtplib, ssl
import sqlite3
import subprocess
import time
import traceback
waGovUrl = "https://www.healthywa.wa.gov.au/COVID19locations"
current_datetime = datetime.now(pytz.timezone("Australia/Perth"))
date_time = current_datetime.strftime("%d/%m/%Y %H:%M:%S")
unix_timestamp = int(current_datetime.timestamp())
### CONFIGURATION ITEMS ###
# Debug mode disables sending of alerts
debug = True
# Database location
db_file = "/path/to/exposures.db" # will be created on first use
# Email details
emailAlerts = False
smtpServ = ""
smtpPort = ""
fromAddr = ""
replyAddr = ""
subjLine = f"Alert: Updated WA covid-19 exposure sites ({date_time})"
destAddr = [
]
# Slack Alerts
slackAlerts = False
webhook_urls = [
"https://hooks.slack.com/services/XXXXXXX/XXXXXXX/XXXXXXX",
"https://hooks.slack.com/services/XXXXXXX/XXXXXXX/XXXXXXX"
]
# Discord Alerts
discordAlerts = False
discord_webhook_urls = [
"https://discordapp.com/api/webhooks/XXXXXXX/XXXXXXX",
"https://discordapp.com/api/webhooks/XXXXXXX/XXXXXXX"
]
# Dreamhost Announce
dreamhostAnounces = False
apiKey = ""
listDomain = ""
listName = ""
subjLine = f"Alert: Updated WA covid-19 exposure sites ({date_time})"
# Error Alert Email
adminAlerts = False
adminSmtpServ = ""
adminSmtpPort = ""
adminFromAddr = ""
adminSmtpUser = ""
adminSmtpPass = ""
AdminReplyAddr = ""
AdminSubjLine = f"Alert: WA Covid Mailer Error ({date_time})"
AdminDestAddr = [
]
### END OF CONFIGURATION ITEMS
if debug:
db_file = "exposures-debug.db"
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file, isolation_level=None)
except Error as e:
print(f"something went wrong: {e}")
# create tables if needed
query = (
"SELECT name FROM sqlite_master WHERE type = 'table';"
)
result = conn.execute(query)
tables = result.fetchall()
required_tables = [
'wahealth_exposures',
'sheet_exposures',
'ecu_exposures',
'uwa_exposures',
'murdoch_exposures',
'curtin_exposures'
]
for table in tables:
while table[0] in required_tables:
required_tables.remove(table[0])
for exposures_table in required_tables:
table_create = ""
if exposures_table == 'wahealth_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS wahealth_exposures (
id integer PRIMARY KEY,
datentime text,
suburb text,
location text,
updated text,
advice text,
first_seen integer,
last_seen integer
);
"""
elif exposures_table == 'sheet_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS sheet_exposures (
id integer PRIMARY KEY,
datentime text,
location text,
suburb text,
first_seen integer,
last_seen integer
);
"""
elif exposures_table == 'ecu_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS ecu_exposures (
id integer PRIMARY KEY,
campus text,
building text,
date text,
room text,
time text,
first_seen integer,
last_seen integer
);
"""
elif exposures_table == 'uwa_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS uwa_exposures (
id integer PRIMARY KEY,
date text,
location text,
time text,
first_seen integer,
last_seen integer
);
"""
elif exposures_table == 'murdoch_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS murdoch_exposures (
id integer PRIMARY KEY,
campus text,
date text,
location text,
time text,
first_seen integer,
last_seen integer
);
"""
elif exposures_table == 'curtin_exposures':
table_create = """
CREATE TABLE IF NOT EXISTS curtin_exposures (
id integer PRIMARY KEY,
campus text,
contact_type text,
date text,
location text,
time text,
first_seen integer,
last_seen integer
);
"""
conn.execute(table_create)
conn.commit()
return conn
def sendEmails(body):
for destEmail in destAddr:
message = f"""To: {destEmail}
From: {fromAddr}
Reply-To: {replyAddr}
Subject: {subjLine}
{body}.""".encode("ascii", "replace")
try:
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtpServ, smtpPort, context=context) as server:
server.sendmail(fromAddr, destEmail, message)
print(f"Email sent to {destEmail}")
except smtplib.SMTPException as e:
print("SMTP error occurred: " + str(e))
def sendAdminAlert(errorMsg):
if(adminAlerts):
for adminDestEmail in AdminDestAddr:
message = f"""To: {AdminDestAddr}
From: {adminFromAddr}
Reply-To: {AdminReplyAddr}
Subject: {AdminSubjLine}
{errorMsg}.""".encode("ascii", "replace")
try:
with smtplib.SMTP(adminSmtpServ, adminSmtpPort) as server:
server.starttls()
server.ehlo()
server.login(adminSmtpUser, adminSmtpPass)
server.sendmail(adminFromAddr, adminDestEmail, message)
print(f"Email sent to {adminDestEmail}")
except smtplib.SMTPException as e:
print("SMTP error occurred: " + str(e))
else:
print("Admin alerts disabled")
print(errorMsg)
def post_message_to_slack(text, blocks=None):
for webhook_url in webhook_urls:
slack_data = {"text": text}
response = requests.post(
webhook_url,
data=json.dumps(slack_data),
headers={"Content-Type": "application/json"},
)
if response.status_code != 200:
raise ValueError(
"Request to slack returned an error %s, the response is:\n%s"
% (response.status_code, response.text)
)
print("Slack sent")
def chunky_alerts(text, delimeter="\n\n", max_length=1990):
i = 0
while i < len(text):
# Note: if the last chunk is less than max_length, it will be included
if i + max_length > len(text):
yield text[i:]
break
nearest_delim = text[i:i+max_length][::-1].index(delimeter) # Calculate the nearest delimiter index, by reversing the string and finding the first occurence of the delimeter
yield text[i:i+max_length-nearest_delim -len(delimeter)] # we don't need the additional delim chars here
i += max_length - nearest_delim # we need them here, so we don't end up including a bunch of line breaks
def post_message_to_discord(text, blocks=None):
for discord_webhook_url in discord_webhook_urls:
# Discord doesn't let us post more than 2000 characters at a time
# so we need to split and make individual posts every 2 seconds to avoid rate limits.
# This may spam notifications depending on your server settings
alert_total = len(list(chunky_alerts(text)))
for alert_number, alert in enumerate(chunky_alerts(text)):
discord_data = {"content": alert}
response = requests.post(
discord_webhook_url,
data=json.dumps(discord_data),
headers={"Content-Type": "application/json"},
)
if response.status_code != 200|204: #Discord returns 204 no data on success
raise ValueError(
"Request to discord returned an error %s, the response is:\n%s"
% (response.status_code, response.text)
)
print("Discord sent %s of %s" % (alert_number, alert_total))
time.sleep(2)
def sendDhAnnounce(comms):
url = "https://api.dreamhost.com/"
bodyParams = {
"key": "somevalue",
}
data = {
"key": apiKey,
"cmd": "announcement_list-post_announcement",
"listname": listName,
"domain": listDomain,
"subject": subjLine,
"message": comms,
"charset": "utf-8",
"type": "text",
"duplicate_ok": "1",
}
x = requests.post(url, data=data)
print(x.text)
return x.status_code
def html_cleanString(s):
try:
s = str(lxml.html.fromstring(s).text_content())
except:
pass
s = s.strip().replace('\r','').replace('\n','').rstrip(',')
return s
def wahealth_GetLocations():
req = requests.get(waGovUrl)
if req.status_code != 200:
print(f"Failed to fetch page: {req.reason}")
raise Exception("reqest_not_ok")
doc = lxml.html.fromstring(req.content)
sites_table = doc.xpath('//table[@id="locationTable"]')[0][1]
rows = sites_table.xpath(".//tr")
# check for proper header
header = doc.xpath('//table[@id="locationTable"]')[0][0]
headerRows = header.xpath(".//th")
if (headerRows[0].text_content() == 'Exposure date & time' and
headerRows[1].text_content() == 'Suburb' and
headerRows[2].text_content() == 'Location' and
headerRows[3].text_content() == 'Date updated' and
headerRows[4].text_content() == 'Health advice'):
pass
else:
raise Exception("WAHealth Failed - Parsing page failure")
if len(rows) < 1:
print(f"found no data")
raise Exception("table_parse_fail")
return rows
def wahealth_cleanString(location):
newLoc = ""
location = location.replace("\xa0", "")
for line in location.split("\n"):
newLoc = newLoc + line.lstrip().rstrip() + ", "
return newLoc.rstrip(", ").lstrip(", ").replace(", , ", "; ").replace(" , ", " ").rstrip("\r\n")
def wahealth_buildDetails(exposure):
exposure_details = f"""Date and Time: {exposure['datentime']}
Suburb: {exposure['suburb']}
Location: {exposure['location']}
Updated: {exposure['updated']}
Advice: {exposure['advice']}\n\n"""
return exposure_details
def wahealth_filterExposures(exposures):
# examine each exposure
# if it is in the DB already, get the id and update last seen
# if it is not in the DB: create the first seen date and make id 'None'
alerts = []
for exposure in exposures:
record = {}
record['datentime'] = wahealth_cleanString(exposure[1].text_content())
record['suburb'] = wahealth_cleanString(exposure[2].text_content())
record['location'] = wahealth_cleanString(exposure[3].text_content())
record['updated'] = wahealth_cleanString(exposure[4].text_content())
record['advice'] = wahealth_cleanString(exposure[5].text_content())
record['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM wahealth_exposures WHERE
datentime = ?
AND suburb = ?
AND location = ?
AND updated = ?
AND advice = ?;"""
args = (record['datentime'], record['suburb'], record['location'], record['updated'], record['advice'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
record['id'] = id[1]
else:
record['id'] = None
record['first_seen'] = unix_timestamp
alerts.append(record)
return alerts
def sheet_GetLocations():
# Consumer: https://docs.google.com/spreadsheets/d/1-U8Ea9o9bnST5pzckC8lzwNNK_jO6kIVUAi5Uu_-Ltc/edit?fbclid=IwAR3EaVvU0di14R6zqqfFP7sDLCwPOYax_SjMcDlmV2D2leqKGRAROCInpj4#gid=1427159313
# Detailed/Admin: https://docs.google.com/spreadsheets/d/12fN17qFR8ruSk2yf29CR1S6xZMs_nve2ww_6FJk7__8/edit#gid=0
sheet_id = "1-U8Ea9o9bnST5pzckC8lzwNNK_jO6kIVUAi5Uu_-Ltc"
sheet_name = "All%20Locations"
url = f"https://docs.google.com/spreadsheets/d/{sheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}"
res = requests.get(url)
contents = codecs.decode(res.content, 'UTF-8')
contents = contents.replace('"",','')
split = contents.splitlines()
reader = csv.reader(split)
sheetExposures = []
for record in reader:
exposure = {}
if record[4] == "Business":
exposure['datentime'] = html_cleanString(record[2])
exposure['suburb'] = html_cleanString(record[1])
exposure['location'] = html_cleanString(record[0]) + " " + html_cleanString(record[3])
exposure['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM sheet_exposures WHERE
datentime = ?
AND suburb = ?
AND location = ?;"""
args = (exposure['datentime'], exposure['suburb'], exposure['location'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
exposure['id'] = id[1]
else:
exposure['id'] = None
exposure['first_seen'] = unix_timestamp
sheetExposures.append(exposure)
if(len(sheetExposures) < 1):
raise Exception("Sheets Failed - Zero records retrieved")
return sheetExposures
def sheet_buildDetails(exposure):
exposure_details = f"""Date and Time: {exposure['datentime']}
Suburb: {exposure['suburb']}
Location: {exposure['location']}\n\n"""
return exposure_details
def ecu_GetLocations():
ecu_url = 'https://www.ecu.edu.au/covid-19/advice-for-staff'
req = requests.get(ecu_url)
if req.status_code != 200:
print(f"Failed to fetch page: {req.reason}")
raise Exception("reqest_not_ok")
doc = lxml.html.fromstring(req.content)
container = doc.xpath('//div[@id="accordion-01e803ff84807e270adaddf7ade2fa91035b560d"]')[0]
tables = container.xpath(".//table")
outRows = []
for table in tables:
campus = html_cleanString(table.getparent().getparent().getparent().getparent()[0].text_content().strip())
rows = table.xpath('./tr')
for row in rows:
record = {}
record['campus'] = campus
record['date'] = html_cleanString(row[0].text_content().strip())
record['time'] = html_cleanString(row[1].text_content().strip())
record['building'] = html_cleanString(row[2].text_content().strip())
record['room'] = html_cleanString(row[3].text_content().strip())
record['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM ecu_exposures WHERE
campus = ?
AND date = ?
AND time = ?
AND building = ?
AND room = ?;"""
args = (record['campus'], record['date'], record['time'], record['building'], record['room'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
record['id'] = id[1]
else:
record['id'] = None
record['first_seen'] = unix_timestamp
outRows.append(record)
for table in tables:
header = table.xpath('.//thead')[0][0]
if (header[0].text_content().strip() == 'Date' and
header[1].text_content().strip() == 'Time' and
header[2].text_content().strip() == 'Building' and
header[3].text_content().strip() == 'Room'):
pass
else:
raise Exception("ECU Failed - Parsing page failure")
return outRows
def ecu_buildDetails(exposure):
exposure_details = f"""Date: {exposure['date']}
Time: {exposure['time']}
Campus: {exposure['campus']}
Building: {exposure['building']}
Room: {exposure['room']}\n\n"""
return exposure_details
def uwa_GetLocations():
uwa_url = 'https://www.uwa.edu.au/covid-19-faq/Home'
req = requests.get(uwa_url)
if req.status_code != 200:
print(f"Failed to fetch page: {req.reason}")
raise Exception("reqest_not_ok")
doc = lxml.html.fromstring(req.content)
rows = doc.xpath('//div/table/tbody/tr')
header = rows.pop(0)
outRows = []
for row in rows:
record = {}
# kludge as there are empty rows with a single cell sometimes :(
if(len(row) < 3):
return ""
record['date'] = html_cleanString(row[0].text_content().strip())
record['location'] = html_cleanString(row[1].text_content().strip())
record['time'] = html_cleanString(row[2].text_content().strip())
record['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM uwa_exposures WHERE
date = ?
AND time = ?
AND location = ?;"""
args = (record['date'], record['time'], record['location'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
record['id'] = id[1]
else:
record['id'] = None
record['first_seen'] = unix_timestamp
outRows.append(record)
if (header[0].text_content().strip() == 'Date' and
header[1].text_content().strip() == 'Location' and
header[2].text_content().strip() == 'Time'):
pass
else:
raise Exception("UWA Failed - Parsing page failure")
return outRows
def uwa_buildDetails(exposure):
exposure_details = f"""Date: {exposure['date']}
Time: {exposure['time']}
Location: {exposure['location']}\n\n"""
return exposure_details
def murdoch_GetLocations():
murdoch_url = 'https://www.murdoch.edu.au/notices/covid-19-advice'
req = requests.get(murdoch_url)
if req.status_code != 200:
print(f"Failed to fetch page: {req.reason}")
raise Exception("reqest_not_ok")
doc = lxml.html.fromstring(req.content)
rows = doc.xpath('//tr')
header = rows.pop(0)
outRows = []
for row in rows:
record = {}
record['date'] = html_cleanString(row[0].text_content().strip())
record['time'] = html_cleanString(row[1].text_content().strip())
record['campus'] = html_cleanString(row[2].text_content().strip())
record['location'] = html_cleanString(row[3].text_content().strip())
record['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM murdoch_exposures WHERE
date = ?
AND time = ?
AND campus = ?
AND location = ?;"""
args = (record['date'], record['time'], record['campus'], record['location'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
record['id'] = id[1]
else:
record['id'] = None
record['first_seen'] = unix_timestamp
outRows.append(record)
if (header[0].text_content().strip() == 'Date' and
header[1].text_content().strip() == 'Time' and
header[2].text_content().strip() == 'Campus' and
header[3].text_content().strip() == 'Location'):
pass
else:
raise Exception("Murdoch Failed - Parsing page failure")
return outRows
def murdoch_buildDetails(exposure):
exposure_details = f"""Date: {exposure['date']}
Time: {exposure['time']}
Campus: {exposure['campus']}
Location: {exposure['location']}\n\n"""
return exposure_details
def curtin_GetLocations():
curtin_url = 'https://www.curtin.edu.au/novel-coronavirus/recent-exposure-sites-on-campus/'
req = requests.get(curtin_url)
if req.status_code != 200:
print(f"Failed to fetch page: {req.reason}")
raise Exception("reqest_not_ok")
doc = lxml.html.fromstring(req.content)
table = doc.xpath('//table[@id="table_1"]')[0]
rows = table.xpath('.//tr')
header = rows.pop(0)
outRows = []
for row in rows:
record = {}
record['date'] = html_cleanString(row[0].text_content().strip())
record['time'] = html_cleanString(row[1].text_content().strip())
record['campus'] = html_cleanString(row[2].text_content().strip())
record['location'] = html_cleanString(row[3].text_content().strip())
record['contact_type'] = html_cleanString(row[4].text_content().strip())
record['last_seen'] = unix_timestamp
query = """SELECT count(id), coalesce(id, 0) FROM curtin_exposures WHERE
date = ?
AND time = ?
AND campus = ?
AND location = ?
AND contact_type = ?;"""
args = (record['date'], record['time'], record['campus'], record['location'], record['contact_type'])
result = dbconn.execute(query, args)
id = result.fetchone()
if id[0] > 0:
record['id'] = id[1]
else:
record['id'] = None
record['first_seen'] = unix_timestamp
outRows.append(record)
if (header[0].text_content().strip() == 'Date' and
header[1].text_content().strip() == 'Time' and
header[2].text_content().strip() == 'Campus' and
header[3].text_content().strip() == 'Location' and
header[4].text_content().strip() == 'Contact type'):
pass
else:
raise Exception("Curtin Failed - Parsing page failure")
return outRows
def curtin_buildDetails(exposure):
exposure_details = f"""Date: {exposure['date']}
Time: {exposure['time']}
Campus: {exposure['campus']}
Location: {exposure['location']}
Contact Type: {exposure['contact_type']}\n\n"""
return exposure_details
# load sqlite3
dbconn = create_connection(db_file)
# backup db incase things go bad
shutil.copy(db_file, f"{db_file}.bak")
# get exposures
try:
wahealth_exposures = wahealth_GetLocations()
#sheet_exposures = sheet_GetLocations()
sheet_exposures = []
ecu_exposures = ecu_GetLocations()
uwa_exposures = uwa_GetLocations()
curtin_exposures = curtin_GetLocations()
murdoch_exposures = murdoch_GetLocations()
except Exception as e:
print(e)
traceback.print_stack()
sendAdminAlert("Unable to fetch data, please investigate")
exit()
# clean exposures list and check if they've already been seen
wahealth_alerts = wahealth_filterExposures(wahealth_exposures)
# for each new exposure add it to the DB and add it to a string for comms
wahealth_comms = ""
sheet_comms = ""
ecu_comms = ""
uwa_comms = ""
murdoch_comms = ""
curtin_comms = ""
for exposure in wahealth_alerts:
if exposure['id'] is None:
wahealth_comms = wahealth_comms + wahealth_buildDetails(exposure)
query = f"""INSERT INTO wahealth_exposures (datentime, suburb, location, updated, advice, first_seen, last_seen)
VALUES (?,?,?,?,?,?,?) """
args = (exposure['datentime'], exposure['suburb'], exposure['location'], exposure['updated'], exposure['advice'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE wahealth_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(wahealth_comms) > 0:
# print(wahealth_comms)
for exposure in sheet_exposures:
if exposure['id'] is None:
sheet_comms = sheet_comms + sheet_buildDetails(exposure)
query = f"""INSERT INTO sheet_exposures (datentime, suburb, location, first_seen, last_seen)
VALUES (?,?,?,?,?) """
args = (exposure['datentime'], exposure['suburb'], exposure['location'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE sheet_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(sheet_comms) > 0:
# print(sheet_comms)
for exposure in ecu_exposures:
if exposure['id'] is None:
ecu_comms = ecu_comms + ecu_buildDetails(exposure)
query = f"""INSERT INTO ecu_exposures (date, time, campus, building, room, first_seen, last_seen)
VALUES (?,?,?,?,?,?,?) """
args = (exposure['date'], exposure['time'], exposure['campus'], exposure['building'], exposure['room'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE ecu_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(ecu_comms) > 0:
# print(ecu_comms)
for exposure in uwa_exposures:
if exposure['id'] is None:
uwa_comms = uwa_comms + uwa_buildDetails(exposure)
query = f"""INSERT INTO uwa_exposures (date, time, location, first_seen, last_seen)
VALUES (?,?,?,?,?) """
args = (exposure['date'], exposure['time'], exposure['location'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE uwa_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(uwa_comms) > 0:
# print(uwa_comms)
for exposure in murdoch_exposures:
if exposure['id'] is None:
murdoch_comms = murdoch_comms + murdoch_buildDetails(exposure)
query = f"""INSERT INTO murdoch_exposures (date, time, campus, location, first_seen, last_seen)
VALUES (?,?,?,?,?,?) """
args = (exposure['date'], exposure['time'], exposure['campus'], exposure['location'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE murdoch_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(murdoch_comms) > 0:
# print(murdoch_comms)
for exposure in curtin_exposures:
if exposure['id'] is None:
curtin_comms = curtin_comms + curtin_buildDetails(exposure)
query = f"""INSERT INTO curtin_exposures (date, time, campus, location, contact_type, first_seen, last_seen)
VALUES (?,?,?,?,?,?,?) """
args = (exposure['date'], exposure['time'], exposure['campus'], exposure['location'], exposure['contact_type'], exposure['first_seen'], exposure['last_seen'])
result = dbconn.execute(query, args)
else:
query = f"""UPDATE curtin_exposures SET last_seen = ?
WHERE id = ? """
args = (exposure['last_seen'], exposure['id'])
result = dbconn.execute(query, args)
#if debug and len(curtin_comms) > 0:
# print(curtin_comms)
# Build report
comms = ""
if(len(wahealth_comms) > 0):
comms = comms + "*WA Health Exposure Sites*\n\n" + wahealth_comms + "\n\n"
if(len(sheet_comms) > 0):
comms = comms + "*Unofficial Civilian Compiled Exposure Sites*\n\n" + sheet_comms + "\n\n"
if(len(ecu_comms) > 0):
comms = comms + "*Edith Cowan University Exposure Sites*\n\n" + ecu_comms + "\n\n"
if(len(uwa_comms) > 0):
comms = comms + "*University of Western Australia Exposure Sites*\n\n" + uwa_comms + "\n\n"
if(len(murdoch_comms) > 0):
comms = comms + "*Murdoch University Exposure Sites*\n\n" + murdoch_comms + "\n\n"
if(len(curtin_comms) > 0):
comms = comms + "*Curtin University Exposure Sites*\n\n" + curtin_comms + "\n\n"
if debug and len(comms) > 0:
print(comms)
# kludge ugh
mailPostSuccess = 200
if not debug:
if len(comms) > 0 and dreamhostAnounces:
mailPostSuccess = sendDhAnnounce(comms)
if len(comms) > 0 and emailAlerts:
sendEmails(comms)
if len(comms) > 0 and slackAlerts:
post_message_to_slack(comms)
if len(comms) > 0 and discordAlerts:
post_message_to_discord(comms)
dbconn.commit()
# we don't close as we're using autocommit, this results in greater
# compatability with different versions of sqlite3
if len(comms) > 0 and dreamhostAnounces and mailPostSuccess != 200 and not debug:
print(result)
os.replace(f"{db_file}.bak", db_file)
sendAdminAlert("Unable to send mail, please investigate")
else:
os.remove(f"{db_file}.bak")