1+ import requests
2+ import json
3+ import datetime
4+ import openpyxl
5+
6+ __author__ = 'RVSB'
7+
8+ STATE = 'Karnataka'
9+ DISTRICT = 'Tumkur'
10+ AGE_LIMIT = 18
11+
12+
13+
14+
15+ base_url = 'https://cdn-api.co-vin.in/api/v2'
16+
17+ endpoints = {
18+
19+ 'meta_states' : '/admin/location/states' ,
20+ 'meta_districts' : '/admin/location/districts/{id}' , # State_ID
21+ 'availability_district' : '/appointment/sessions/public/calendarByDistrict/?district_id={district_id}&date={date}' # date = dd-mm-yyyy
22+
23+ }
24+
25+
26+
27+ headers = {
28+ "Connection" : "keep-alive" ,
29+ "Pragma" : "no-cache" ,
30+ "Cache-Control" : "no-cache" ,
31+ "sec-ch-ua-mobile" : "?0" ,
32+ "DNT" : "1" ,
33+ "Upgrade-Insecure-Requests" : "1" ,
34+ "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.51" ,
35+ "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ,
36+ "Sec-Fetch-Site" : "none" ,
37+ "Sec-Fetch-Mode" : "navigate" ,
38+ "Sec-Fetch-User" : "?1" ,
39+ "Sec-Fetch-Dest" : "document" ,
40+ "Accept-Language" : "en-GB,en;q=0.9,en-US;q=0.8"
41+
42+ }
43+
44+ def get_jsons ( endpoint , data = None ) :
45+ if data :
46+ print ( endpoints [endpoint ].format (** data ))
47+ request = requests .get (base_url + endpoints [endpoint ].format (** data ), headers = headers )
48+ else :
49+ request = requests .get (base_url + endpoints [endpoint ], headers = headers )
50+ if request .status_code == 200 :
51+ return json .loads (request .content )
52+
53+ else :
54+ print ("error occured at" , base_url + endpoints [endpoint ])
55+
56+
57+
58+
59+ states = { state ['state_name' ]:state ['state_id' ] for state in get_jsons ('meta_states' )['states' ] }
60+
61+ print (states )
62+
63+ districts = { district ['district_name' ]:district ['district_id' ] for district in get_jsons ('meta_districts' , { 'id' : states [STATE ]})['districts' ] }
64+
65+
66+ #whilte True:
67+ present = datetime .datetime .now ()
68+ availability_district = get_jsons ('availability_district' , { 'district_id' : districts [DISTRICT ], 'date' : present .strftime ('%d-%m-%Y' )} )
69+
70+
71+ from collections import OrderedDict
72+ avails = OrderedDict ()
73+
74+ for centre in availability_district ['centers' ]:
75+ for session in centre ['sessions' ]:
76+ if session ['available_capacity' ] > 0 and session ['min_age_limit' ] <= AGE_LIMIT :
77+ data = {}
78+ data ['hospital' ] = centre ['name' ]
79+ data ['address' ] = centre ['address' ]
80+ data ['fee_type' ] = centre ['fee_type' ]
81+ data ['capacity' ] = session ['available_capacity' ]
82+ data ['age' ] = session ['min_age_limit' ]
83+ if session ['date' ] in avails :
84+ avails [session ['date' ]].append (data )
85+ else :
86+ avails [session ['date' ]] = [data ]
87+
88+
89+
90+ workbook = openpyxl .Workbook ()
91+ sheet = workbook .active
92+ sheet .append (['Hospital Name' , 'Address' , 'Date' , 'Capacity' , 'Minimum Age' , 'Fee Type' ])
93+
94+
95+ data = []
96+ for date in avails :
97+ for slot in avails [date ]:
98+ data .append ([slot ['hospital' ], slot ['address' ], date , slot ['capacity' ], slot ['age' ], slot ['fee_type' ]])
99+
100+ data .sort (key = lambda x : (x [2 ], - 1 * x [3 ]))
101+
102+ for x in data :
103+ sheet .append (x )
104+
105+ workbook .save ('slots.xlsx' )
106+
0 commit comments