-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsirfy_impfung.py
149 lines (116 loc) · 4.89 KB
/
sirfy_impfung.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
import os
import json
import urllib
import time
import mechanize
import ssl
from twilio.rest import Client
from datetime import datetime
from bs4 import BeautifulSoup
ssl._create_default_https_context = ssl._create_unverified_context
#flag to skip WhatsApp message - Set to False to use Twilio
skip_whatapp = False
run_test_whatsapp = False#True
run_test_appt2dict = False
load_twilio_config = True
#Use thjis list to filter out aapointments from the log and messaging service
incl_kwrds = ['zweite', 'comirnaty']
if load_twilio_config:
os.environ['TWILIO_SID'] = 'ACd94c1924c79aead0dcf7df3fa4b74c67'
# load authentication token and destination number from json
with open("twilio.auth", "r") as file:
dic = json.load(file)
os.environ['TWILIO_AUTH_TOKEN'] = dic['auth_token']
os.environ['TWILIO_NUMBER'] = dic['to']
cooldown_time = 5 # seconds
def test_twilio_message():
client = init_twilio_client()
print(os.environ['TWILIO_NUMBER'])
temp_dict = {}
temp_dict['des'] = 'Paracetamoxifrusibendroneomycin vaccine'
temp_dict['Date'] = '01/01/2021'
message = send_twilio(temp_dict['Date'], temp_dict['des'], client)
print(message)
def send_twilio(date, desc, client):
twilio_number = os.environ['TWILIO_NUMBER']
message = client.messages.create(
from_='whatsapp:+14155238886',
body='Your {} appointment is coming up on {}'.format(desc,
date),
to=twilio_number
)
print("Message send to {} successful".format(twilio_number))
return message
def init_twilio_client():
twilio_sid = os.environ['TWILIO_SID']
twilio_auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(twilio_sid, twilio_auth_token)
return client
def appt2dict(termine):
temp_dict = {}
temp_dict['Date'] = termine[0]
temp_dict['time'] = termine[1]
temp_dict['id'] = termine[2]
temp_dict['doc'] = termine[3]
temp_dict['desc'] = termine[4]
if any([x.lower() in temp_dict['desc'].lower() for x in incl_kwrds]):
print("{}: Appointment for {} on {} at {} with {}".format(datetime.now().strftime("%d/%m/%y %H:%M:%S"),
temp_dict['desc'],
temp_dict['Date'],
temp_dict['time'],
temp_dict['doc']))
return temp_dict
def test_appt2dict():
termine = ['01/01/2021', '14:00', '0001', 'Dr. Sirfy', 'Paracetamoxifrusibendroneomycin vaccine']
print(appt2dict(termine))
def main():
if skip_whatapp is not True:
twilio_number = os.environ['TWILIO_NUMBER']
# set up twilio using https://www.twilio.com/blog/send-whatsapp-message-30-seconds-python
client = init_twilio_client()
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
params = {"versichert": "", "terminsuche": "", "uniqueident": "607feb7a343fb"}
data = urllib.parse.urlencode(params)
#notified_id = []
global notified_id
global notify_wait
#notify_wait = 0
resp_dict = {'termine':None}
try:
resp = br.open("https://onlinetermine.zollsoft.de/includes/searchTermine_app_feature.php", data)
resp_dict = json.loads(BeautifulSoup(resp, 'html.parser').prettify())
if len(resp_dict['termine']) > 0:
global appt_dict
#appt_dict = []
for termine in resp_dict['termine']:
appt_dict.append(appt2dict(termine))
# send message if one hasn't been sent about this appointment and in the last 30 secs
for termine in appt_dict:
if notify_wait > 6 and termine['id'] not in notified_id and not skip_whatapp and any([x.lower() in termine['desc'].lower() for x in incl_kwrds]):
message = send_twilio(termine['Date'], termine['desc'], client)
notified_id.append(termine['id'])
notify_wait = 0
notify_wait +=1
except Exception as exception:
print(resp_dict['termine'])
print(exception)
if __name__ == '__main__':
if run_test_whatsapp is True:
print('test whatsapp message')
test_twilio_message()
print('whatsapp message test complete')
if run_test_appt2dict is True:
print('test appointment printout')
test_appt2dict()
print('appointment printout test complete')
global appt_dict
global notify_wait
global notified_id
appt_dict = []
notify_wait=0
notified_id = []
while True:
main()
time.sleep(cooldown_time)