-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowin.py
204 lines (153 loc) · 6.86 KB
/
cowin.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
import requests
import logging
from datetime import datetime, timedelta
import pytz
from hashlib import sha256
import time
class Session:
def __init__(self, id, name, address, district, pay, date, numSlots):
self.id = id
self.name = name
self.address = address
self.district = district
self.pay = pay
self.date = date
self.numSlots = numSlots
def __str__(self):
return "NAME : " + self.name + \
", ADDRESS : " + self.address + \
", DISTRICT : " + self.district + \
", PAY : " + self.pay + \
", DATE : " + self.date + \
", SLOTS : " + str(self.numSlots)
class Telegram:
chatId = "" # chatId of the telegram group
token = "" # token of the bot
def sendMessage(self,msg):
sendText = 'https://api.telegram.org/bot' + self.token + '/sendMessage?chat_id=' + self.chatId + '&parse_mode=Markdown&text=' + msg
response = requests.get(sendText)
if response.status_code != 200:
print("Sending to telegram failed!")
class CowinApp:
# Server end points
PROD_SERVER = "https://cdn-api.co-vin.in/api"
TEST_SERVER = "https://api.demo.co-vin.in/api"
STANDARD_HEADERS = {"Accept": "application/json",
"Connection": "keep-alive",
"User-Agent": "CowinApp/0.0.1",
}
# Utility end points
GET_STATES = "/v2/admin/location/states"
GET_DISTRICT = "/v2/admin/location/districts/" # {state_id}
GET_CALENDAR = "/v2/appointment/sessions/public/calendarByDistrict"
POST_GENOTP = "/v2/auth/public/generateOTP"
POST_CONFIRMOTP = "/v2/auth/public/confirmOTP"
def __init__(self):
now = datetime.now(tz=pytz.timezone('Asia/Kolkata'))
fname = now.strftime("%H_%M_%S") + ".log"
logging.basicConfig(filename=fname, filemode='w',
format='%(asctime)s - %(message)s',
level=logging.INFO)
self.txn_id = ""
self.token = ""
def getStatesOfInterest(self, locationOfInterest):
statesOfInterest = locationOfInterest.keys()
stateIds = {}
urlToHit = self.PROD_SERVER + self.GET_STATES
logging.info("Querying for all available states at " + urlToHit)
r = requests.get(url=urlToHit, headers=self.STANDARD_HEADERS)
if r.status_code == 200:
stateData = r.json()
for state in stateData["states"]:
if state["state_name"] in statesOfInterest:
logging.info("State id for " + state["state_name"] + " is " + str(state["state_id"]))
stateIds[state["state_name"]] = state["state_id"]
else:
logging.fatal(r.content)
logging.fatal("Couldn't retrieve states")
return stateIds
def getDistrictsOfInterest(self, locationOfInterest):
stateIds = self.getStatesOfInterest(locationOfInterest)
districtIds = {}
if len(stateIds) == 0:
logging.error("No states to search")
else:
for state in locationOfInterest:
if state not in stateIds:
logging.error("Couldn't find state id for " + state)
continue
urlToHit = self.PROD_SERVER + self.GET_DISTRICT + str(stateIds[state])
logging.info("Getting districts for state " + state)
r = requests.get(url=urlToHit, headers=self.STANDARD_HEADERS)
if r.status_code == 200:
districtData = r.json()
for districts in districtData["districts"]:
if districts["district_name"] in locationOfInterest[state]:
districtIds[districts["district_name"]] = districts["district_id"]
else:
logging.fatal(r.content)
logging.fatal("Couldn't retrieve districts for " + state)
return districtIds
def getAvailableSessions(self, districtIds, sessionChecker):
sessions = []
for district in districtIds:
tomorrow = datetime.today() #+ timedelta(days=1)
dateStr = tomorrow.strftime("%d-%m-%Y")
logging.info("Fetching 7 day calendar for " + district + " starting " + dateStr)
urlToHit = self.PROD_SERVER + self.GET_CALENDAR
params = {
"district_id": districtIds[district],
"date": dateStr,
}
time.sleep(4)
r = requests.get(url=urlToHit, params=params, headers=self.STANDARD_HEADERS)
if r.status_code == 200:
calendarData = r.json()
for center in calendarData["centers"]:
for session in center["sessions"]:
if sessionChecker(session) and session["available_capacity"] > 0:
s = Session(session["session_id"], center["name"], center["address"],
center["district_name"], center["fee_type"], session["date"],
session["available_capacity"])
sessions.append(s)
logging.info("FOUND a session " + str(session))
else:
self.prevSessionsSent = []
logging.fatal("Unable to get calendar for " + district)
logging.fatal(r.content)
return sessions
def authenticate(self):
urlToHit = self.PROD_SERVER + self.POST_GENOTP
print("Enter mobile number : ")
phoneNumber = input().strip()
params = {
"mobile": str(phoneNumber)
}
r = requests.post(url=urlToHit, json=params, headers=self.STANDARD_HEADERS)
if r.status_code == 200:
genOTPData = r.json()
self.txn_id = genOTPData["txnId"]
logging.info("Received the transaction id " + self.txn_id)
urlToHit = self.PROD_SERVER + self.POST_CONFIRMOTP
print("Enter the OTP received : ")
otp = input().strip()
hashOTP = sha256(otp.encode('utf-8')).hexdigest()
params = {
"otp": hashOTP,
"txnId": self.txn_id
}
r = requests.post(url=urlToHit, json=params, headers=self.STANDARD_HEADERS)
if r.status_code == 200:
tokenData = r.json()
self.token = tokenData["token"]
logging.info("Received the token " + self.token)
print("Authentication successful!")
return True
else:
print("Something went bad" + str(r.content))
else:
print("Something went wrong!" + str(r.content))
return False
def book(self, sessions):
# This is not open to all :(
pass