This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
- 30 Days Of Python 👨💻 - Day 9 - OOP Pillars
- 30 Days Of Python 👨💻 - Day 10 - OOP Missing Pieces
- 30 Days Of Python 👨💻 - Day 11 - Functional Programming
- 30 Days Of Python 👨💻 - Day 12 - Lambda Expression And Comprehensions
- 30 Days Of Python 👨💻 - Day 13 - Decorators
- 30 Days Of Python 👨💻 - Day 14 - Handling Errors
- 30 Days Of Python 👨💻 - Day 15 - Generators
- 30 Days Of Python 👨💻 - Day 16 - Module Basics
- 30 Days Of Python 👨💻 - Day 17 - External Modules
- 30 Days of Python 👨💻 - Day 18 - File I/O
- 30 Days Of Python 👨💻 - Day 19 - Regular Expressions
- 30 Days Of Python 👨💻 - Day 20 - Debugging And Testing
- 30 Days Of Python 👨💻 - Day 21 - Scripting Basics
Today I continued exploring more of the possibilities of scripting with Python. I read some interesting articles which I will be sharing at the end of this post.
Doing HTTP Requests
One of the most common things that we often need to implement is communicating with some API to fetch data and process it. So I decided to find out ways in which we can talk to an API using Python and access the data. There are some libraries to do HTTP requests such as httplib, httplib2, urllib but the most commonly used library is: https://requests.readthedocs.io/en/master/
It helps to do HTTP requests with ease and has excellent documentation.
It can be installed using the command pip install requests
I decided to try doing something interesting while exploring how to use the requests library. I found an API to track the COVID-19 statistical data for India. This API provides the official data as per ICMR stats. I decided to use the API to create a script that can show us the daily stats for COVID-19 along with some other useful information, and a script to display daily statistics.
I created a new script file covid_tracker_india.py in the existing scripts project. The link to the GitHub repo is here https://github.com/arindamdawn/python-scripting.
covid_tracker_india.py
- import requests
- API_URL_ALL_DATA = 'https://api.covid19india.org/data.json'
- API_URL_DISTRICT_WISE = 'https://api.covid19india.org/state_district_wise.json'
- response = requests.get(API_URL_ALL_DATA)
- def get_daily_stats(response):
- try:
- all_cases_data_list = response.json()['cases_time_series']
- latest_cases_data = all_cases_data_list[len(all_cases_data_list) - 1]
- formatted_data = f'''''''
- COVID INDIA DAILY STATS:
- AS of {latest_cases_data['date']}
- ******************************
- Total Confirmeed Cases : {latest_cases_data['totalconfirmed']}
- Total Recovered Cases : {latest_cases_data['totalrecovered']}
- Total Deaths Reported: {latest_cases_data['totaldeceased']}
- Confirmed Cases Yesterday: {latest_cases_data['dailyconfirmed']}
- Confirmed Recovered Cases Yesterday: {latest_cases_data['dailyrecovered']}
- Deaths Reported Yesterday: {latest_cases_data['dailydeceased']}
- ******************************
- '''
- print(formatted_data)
- except:
- print('An error occurred while processing data')
- if __name__ == '__main__':
- get_daily_stats(response)

Join the conversation! Your thoughts help the community grow.