How I Made a Ten Line Ruby Script to Get My 1st Jab

Written by rishipithadiya | Published 2021/06/02
Tech Story Tags: ruby-on-rails | web-scraping | ruby | coding | programming | app | web | developer

TLDR 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. Cowin already has Public APIs available, which anyone can use to check slot availability. 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- The API returns a response that contains all required information like center details, session details, and available slot details.via the TL;DR App

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 -
  1. min_age_limit
     attribute comparison with 
    18
     (
    min_age_limit == 18
    )
  2. available_capacity
     is greater than 
    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 -
  1. Write a fancy output, maybe create mail/SMS to send notification.
  2. 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

Written by rishipithadiya | Senior Technical Lead(Ruby on Rails) at Sell.do ⚡️
Published by HackerNoon on 2021/06/02