Recently, the Indian govt announced vaccination for the 18-45 age group from 1st May āØ
Anyone who wants to vaccinate in this age group can visit theĀ official website or installĀ Arogya Setu AppĀ from Google Play to book a slot for vaccination.
The problem is vaccination is limited and slots are also limited, so whenever anyone wanted to book a slot, it always showsĀ bookedĀ on official websiteĀ š¢
After visiting theĀ websiteĀ multiple times in a day to book my slot, I was unable to find any available slots because it always showsĀ bookedĀ š„ŗ
Now, on the same website when you click on a search to find slots, it calls one API which is used to get the availability of slots for the selected district-
Cowin already hasĀ Public APIsĀ available, which anyone can use to check slot availability.
Out of all public APIs, I usedĀ
calendarByDistrict
Ā API returns planned vaccination sessions forĀ 7Ā days from a specific date for a given district.Which returns a response that contains all required information like center details, session details, and available slot details šš¼
{
"centers": [
{
"center_id": 1234,
"name": "District General Hostpital",
"state_name": "Maharashtra",
"district_name": "Pune",
"pincode": "411057",
"lat": 28.7,
"long": 77.1,
"from": "09:00:00",
"to": "18:00:00",
"fee_type": "Free",
"sessions": [
{
"session_id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"date": "10-05-2021",
"available_capacity": 50,
"min_age_limit": 18,
"vaccine": "COVISHIELD",
"slots": [
"FORENOON",
"AFTERNOON"
]
}
]
}
]
}
You may visit this link to get an idea about the detailed responseĀ here.
For sending web requests in ruby language,Ā Net::HTTPĀ class available. For the above request, you can execute the below code in your rails console for getting center list JSON -
# District - Pune, Date 10th May, 2021. You can modify it as per your requirement
uri = URI.parse("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id=363&date=#{Date.today.strftime('%d-%m-%Y')}")
results = Net::HTTP.get(uri)
results = JSON.parse(results).with_indifferent_access
It will give you a list of centers likeĀ thisĀ in your rails console.
Now we haveĀ
results
Ā hash variable available in which the root element isĀ centers
Ā like this -{
"centers": [
{
"center_id": 1234,
"name": "District General Hostpital"
"sessions": [
{
"date": "10-05-2021",
"available_capacity": 50,
"min_age_limit": 18
}
]
}
]
}
Basically, from above data, we need to compare the following attributes to get available slots -
Ā attribute comparison withĀmin_age_limit
Ā (18
)min_age_limit == 18
Ā is greater thanĀavailable_capacity
Ā (0
)available_capacity > 0
Alright, letās search for available slots then -
Thatās it š„³Ā
availability
Ā variable will have the following hash value if any availability found.āÆ availability
āÆ {413102=>["02-05-2021"], 411044=>["02-05-2021"]}
It contains dates on which slots available for particular Pincode š
Now, as we have all the required data with us, we can do the following thing with this script -
- Write a fancy output, maybe create mail/SMS to send notification.
- Create a cron-job to run it every 1 minute and notify if thereās availability.
Apart from this, you may also create a minimal Rails app to get user details from users and send notifications to help them find available slots.
Conclusion
Automation is good sometimes if used properly. In this case, if you wanted to run your cron-job in large intervals, this is possible. Perhaps once in 10 minutes, itās fine. Automation can be bad too so your script will get noticed if it misbehaves.
However, if you run it on a very small interval. Access cowin API too frequently, and there is a chance that your IP might be blocked. To access these APIs, you must be responsible. So use your coding powers wisely āš¼
Important
Sometimes youāll get the following error while API call -
We canāt connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
It might be there is a lot of traffic on the server on peak hours. Be patient, execute API after some time (perhaps 1 hour), so that the server is given time to clear its workload.
Reference
If you found this article insightful and helpful, then do let me know your views in the comments š
In case you want to connect with me, hereās my Twitter -Ā rishipi
Disclaimer: The author provides this code and software āAS ISā, without
warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement. In no event shall the author be liable for any claim, damages or other liability in connection with the software or code provided here
warranty of any kind, express or implied, including but not limited to fitness for a particular purpose and non-infringement. In no event shall the author be liable for any claim, damages or other liability in connection with the software or code provided here
Previously published at https://rishi.tips/p/find-vaccine-slots/