-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_service.py
192 lines (148 loc) · 6.81 KB
/
api_service.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
import json
import os
import random
import time
from datetime import datetime, timedelta
from functools import lru_cache
import logging
import requests
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
# logging.basicConfig(level=logging.DEBUG)
# https://stackoverflow.com/questions/27652543/how-to-use-python-requests-to-fake-a-browser-visit-a-k-a-and-generate-user-agent
from tqdm import tqdm
os.environ['TZ'] = 'Asia/Kolkata'
time.tzset()
resp = requests.get("http://ip-api.com/json")
print(resp.json())
software_names = [SoftwareName.CHROME.value,
SoftwareName.FIREFOX.value,
SoftwareName.EDGE.value]
operating_systems = [OperatingSystem.WINDOWS.value]
user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100)
# TODO: fix this
# ROOT_URL = r"https://cdn-api.co-vin.in/api"
def get_all_state_codes():
headers = headers = {'Content-type': 'application/json',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'User-Agent': user_agent_rotator.get_random_user_agent(),
'referer': r'https://www.cowin.gov.in/',
'origin': r'https://www.cowin.gov.in'}
for i in range(10):
try:
response = requests.get("https://cdn-api.co-vin.in/api/v2/admin/location/states", headers=headers)
if response.status_code != 200:
print(response.text)
json_data = response.json()["states"]
return json_data
except Exception as e:
print(e)
print("unable to get states attempt {}".format(i))
time.sleep(10 + random.random() * 30 + random.random() * 30)
raise Exception("Unable to get list of states")
@lru_cache()
def get_all_dist_codes_api():
dist_codes = list()
states = get_all_state_codes()
for state in tqdm(states):
headers = {'Content-type': 'application/json',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'User-Agent': user_agent_rotator.get_random_user_agent(),
'referer': r'https://www.cowin.gov.in/',
'origin': r'https://www.cowin.gov.in'}
success = False
for i in range(10):
try:
response = requests.get(
"https://cdn-api.co-vin.in/api/v2/admin/location/districts/{}".format(state["state_id"]),
headers=headers)
print(state["state_name"])
json_data = json.loads(response.text)
for dist_info in json_data["districts"]:
dist_codes.append({"dist_id": dist_info["district_id"],
"dist_name": dist_info["district_name"],
"state_id": state["state_id"],
"state_name": state["state_name"]}
)
success = True
break
except Exception as e:
print(e)
print("unable to get dist info attempt {}".format(i))
time.sleep(3 * 60 + random.random() * 30)
if not success:
raise Exception("Unable to get dist info for state {}".format(state["state_name"]))
time.sleep(10 + random.random() * 10)
return dist_codes
@lru_cache()
def get_dist_id_from_name_api(dist_name):
dist_codes = get_all_dist_codes_api()
name_code_dict = dict((d["dist_name"], d["dist_id"]) for d in dist_codes)
return name_code_dict.get(dist_name)
def get_dist_vaccination_calendar_by_date(dist_id, date, user_agent=user_agent_rotator.get_random_user_agent()):
headers = {'Content-type': 'application/json',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8',
'User-Agent': user_agent,
'referer': r'https://www.cowin.gov.in/',
'origin': r'https://www.cowin.gov.in'}
url = r"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict"
params = {"district_id": str(dist_id),
"date": date}
response = requests.get(url, params=params, headers=headers)
if response.status_code != 200:
print("Non 200 data received for dist_id {} date {}".format(dist_id, date))
# print(response.text)
raise Exception("Couldn't get slots for {}".format(dist_id))
data = response.json()
slots = []
if "centers" not in data:
print("Invalid data received for dist_id {} date {}".format(dist_id, date))
print(data)
# print(data)
for center_info in data.get("centers", []):
slot_info = {
"center_id": center_info["center_id"],
"center_name": center_info["name"],
"state_name": center_info["state_name"],
"dist_name": center_info["district_name"],
"block_name": center_info["block_name"],
"pincode": center_info["pincode"],
"dist_id": dist_id,
}
for session in center_info["sessions"]:
datetimeobject = datetime.strptime(session["date"], '%d-%m-%Y')
new_format = datetimeobject.strftime('%d%b')
min_age_limit = session["min_age_limit"]
session_info = {
"date": new_format,
"capacity_18_above": session["available_capacity"] if min_age_limit == 18 else 0,
"capacity_45_above": session["available_capacity"] if min_age_limit == 45 else 0,
"vaccine": session["vaccine"]
}
combined_info = {**slot_info, **session_info}
slots.append(combined_info)
slots = list(filter(lambda x: x["capacity_18_above"] > 0, slots))
return slots
def get_dist_vaccination_calendar(dist_id):
date_today = datetime.today()
slots = []
date = date_today
for i in range(4):
date_string = datetime.strftime(date, '%d-%m-%Y')
# print(date_string, "\n")
week_slots = get_dist_vaccination_calendar_by_date(dist_id,
date_string,
user_agent=user_agent_rotator.get_random_user_agent())
slots = slots + week_slots
date = date + timedelta(days=8)
if len(slots) >= 5:
break
time.sleep(1 + random.random() * 2)
return slots
# print(get_dist_vaccination_calendar(363))