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