-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
275 lines (201 loc) · 7.08 KB
/
script.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
import requests
import json
import os
import time
import datetime
from dotenv import load_dotenv
import re
load_dotenv()
API_TOKEN = os.getenv("API_TOKEN")
DATA_DIRECTORY = os.getenv("DATA_DIRECTORY")
VACCINE_TRACKER_BASE_URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin"
TELEGRAM_BOT_BASE_URL = "https://api.telegram.org/bot"
REQUEST_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
}
USER_DATA_FILENAME = DATA_DIRECTORY + "/user_data.json"
DATA_OFFSET_FILE = DATA_DIRECTORY + "/data_offset.json"
def read_offset_data():
offset_data = None
if os.path.exists(DATA_OFFSET_FILE):
infile = open(DATA_OFFSET_FILE, 'r')
offset_data = infile.read()
return offset_data
def save_offset_data(latest_offset):
outfile = open(DATA_OFFSET_FILE, 'w')
outfile.write(str(latest_offset))
def save_user_data(user_id_pincode_dict):
with open(USER_DATA_FILENAME, 'w') as outfile:
json.dump(user_id_pincode_dict, outfile, indent=2)
def read_user_data():
user_id_pincode_dict = {}
if os.path.exists(USER_DATA_FILENAME):
with open(USER_DATA_FILENAME, 'r') as infile:
user_id_pincode_dict = json.load(infile)
return user_id_pincode_dict
def is_valid_pincode(pincode):
pattern = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$"
match_compiler = re.compile(pattern)
is_valid = re.match(match_compiler, pincode);
if is_valid is None:
return False
else:
return True
def get_new_user_data(user_id_pincode_dict, offset_id=None):
bot_update_url = "https://api.telegram.org/bot{}/getUpdates".format(API_TOKEN)
if offset_id:
bot_update_url += "?offset={}".format(offset_id)
latest_offset_id = offset_id
response = requests.get(bot_update_url, headers=REQUEST_HEADERS)
json_data = response.json()
if len(json_data['result']) == 0:
print("NO USERS UNTIL NOW.")
return False
result_list = json_data['result']
for data in result_list:
if 'update_id' in data:
if latest_offset_id == data['update_id']:
return
latest_offset_id = data['update_id']
if 'message' in data and 'text' in data['message']:
chat_id = data['message']['chat']['id']
if 'unsub_pincode' in data['message']['text']:
text_list = data['message']['text'].split(" ")
if len(text_list) > 1:
pincode = text_list.pop()
if is_valid_pincode(pincode) is False:
continue
if pincode in user_id_pincode_dict and chat_id in user_id_pincode_dict[pincode]:
user_id_pincode_dict[pincode].remove(chat_id)
elif 'pincode' in data['message']['text']:
text_list = data['message']['text'].split(" ")
if len(text_list) > 1:
pincode = text_list.pop()
if is_valid_pincode(pincode) is False:
continue
if pincode in user_id_pincode_dict:
user_id_pincode_dict[pincode].append(chat_id)
else:
user_id_pincode_dict[pincode] = [chat_id]
user_data_without_duplicates = {}
for key in user_id_pincode_dict:
if len(user_id_pincode_dict[key]) == 0:
continue
user_data_without_duplicates[key] = list(set(user_id_pincode_dict[key]))
# print(user_id_pincode_dict)
save_user_data(user_data_without_duplicates)
save_offset_data(latest_offset_id)
return
def fetch_data(pincode):
try:
current_date = datetime.date.today()
formatted_date = current_date.strftime("%d-%m-%Y")
API_URL = VACCINE_TRACKER_BASE_URL + '?pincode={}&date={}'.format(pincode, formatted_date)
response = requests.get(API_URL, headers=REQUEST_HEADERS)
if response.status_code != 200:
return []
json_data = response.json()
centre_list = []
if 'centers' in json_data:
centre_list = json_data['centers']
return centre_list
except Exception as e:
print("ERROR:{}".format(e))
def identify_available_slots(centre_list):
free_slots = []
data_by_group = [
{
"group": 18,
"data_count": 0,
"has_data": False,
"grouped_msgs": {},
"msg": "Vaccine Slots for Age group: 18-44: \n",
},
{
"group": 45,
"data_count": 0,
"has_data": False,
"grouped_msgs": {},
"msg": "Vaccine Slots for Age group: 45+: \n"
}
]
for centre in centre_list:
free_centre = {}
if len(centre['sessions']) == 0:
continue
for session in centre['sessions']:
if session['available_capacity'] > 50:
free_centre = {
"name": centre['name'],
"address": centre['address'],
"district": centre['district_name'],
"block": centre['block_name'],
"pincode": centre['pincode'],
"vaccine": session['vaccine'],
"date": session['date'],
"type": centre['fee_type'],
"group": session['min_age_limit'],
"quantity": session['available_capacity']
}
group_idx = 0
if session['min_age_limit'] == 45:
group_idx = 1
data_by_group[group_idx]['data_count'] += 1
data_by_group[group_idx]['has_data'] = True
age_group = str(session['min_age_limit']) + "+"
formatted_msg = "\n{}".format(prepare_msg(free_centre))
if session['date'] in data_by_group[group_idx]['grouped_msgs']:
data_by_group[group_idx]['grouped_msgs'][session['date']]['msg'] += '\n' + formatted_msg
else:
data_by_group[group_idx] = {
**data_by_group[group_idx],
'grouped_msgs': {
**data_by_group[group_idx]['grouped_msgs'],
session['date']: {
'msg': "\n\nAge group: {}\nDate: {}\n{}".format(age_group,session['date'],formatted_msg)
}
}
}
free_slots.append(free_centre)
return data_by_group
def prepare_msg(data_dict):
msg = "{}, \nAddress: {}, {}, Pincode: {}, \nVaccine Name: {}, \nType: {} \nAvailable Quantity: {}".format(
data_dict['name'],
data_dict['address'],
data_dict['district'],
str(data_dict['pincode']),
data_dict['vaccine'],
data_dict['type'],
str(data_dict['quantity'])
)
return msg
def send_notification(msg_dict, recipient_ids):
for msg_group in msg_dict:
if msg_group['has_data']:
for recipient_id in recipient_ids:
for date_key in msg_group['grouped_msgs']:
msg_data = msg_group['grouped_msgs'][date_key]['msg']
print("-------------------------------------")
print('SENDING DATA: {}'.format(msg_data))
time.sleep(1)
try:
response = requests.get("https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}".format(API_TOKEN, recipient_id, msg_data), headers=REQUEST_HEADERS)
if response.status_code != 200:
print("Notification not sent for recipient id: {}, error: {}".format(str(recipient_id), response.text))
except Exception as e:
print("CANNOT SEND MSG:{}".format(e))
def main():
latest_offset = read_offset_data()
user_data = read_user_data()
get_new_user_data(user_data, latest_offset)
print("Available user_data: {}".format(user_data))
for pincode in user_data:
response_data = fetch_data(pincode)
msg_dict = identify_available_slots(response_data)
send_notification(msg_dict, user_data[pincode])
if __name__ == "__main__":
start_time = datetime.datetime.now()
print("Script started at : {}".format(str(start_time)))
main()
run_time = datetime.datetime.now() - start_time
print("Total Runtime : {}".format(str(run_time)))