30 Days of Python πŸ‘¨β€πŸ’» - Day 22 - Scripting Extras

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
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
  1. import requests    
  2.     
  3. API_URL_ALL_DATA = 'https://api.covid19india.org/data.json'    
  4. API_URL_DISTRICT_WISE = 'https://api.covid19india.org/state_district_wise.json'    
  5.     
  6. response = requests.get(API_URL_ALL_DATA)    
  7.     
  8. def get_daily_stats(response):    
  9.     try:    
  10.         all_cases_data_list = response.json()['cases_time_series']    
  11.         latest_cases_data = all_cases_data_list[len(all_cases_data_list) - 1]    
  12.         formatted_data = f'''''''  
  13.             COVID INDIA DAILY STATS:  
  14.             AS of {latest_cases_data['date']}  
  15.             ******************************  
  16.             Total Confirmeed Cases : {latest_cases_data['totalconfirmed']}  
  17.             Total Recovered Cases : {latest_cases_data['totalrecovered']}  
  18.             Total Deaths Reported: {latest_cases_data['totaldeceased']}  
  19.             Confirmed Cases Yesterday: {latest_cases_data['dailyconfirmed']}  
  20.             Confirmed Recovered Cases Yesterday: {latest_cases_data['dailyrecovered']}  
  21.             Deaths Reported Yesterday: {latest_cases_data['dailydeceased']}  
  22.             ******************************  
  23.         '''    
  24.         print(formatted_data)    
  25.     except:    
  26.         print('An error occurred while processing data')    
  27.     
  28. if __name__ == '__main__':    
  29.     get_daily_stats(response)   
On running the script, it should print the data in this format
  1. COVID INDIA DAILY STATS:    
  2. AS of 11 July    
  3. ******************************    
  4. Total Confirmeed Cases : 850364    
  5. Total Recovered Cases : 536232    
  6. Total Deaths Reported: 22689    
  7. Confirmed Cases Yesterday: 27755    
  8. Confirmed Recovered Cases Yesterday: 19981    
  9. Deaths Reported Yesterday: 543    
  10. ******************************    
I experimented further with the API data and created another function that displays the top 5 states with the most active cases. Here is the updated script.
 
covid_tracker_india.py
  1. import requests    
  2.     
  3. API_URL_ALL_DATA = 'https://api.covid19india.org/data.json'    
  4. API_URL_DISTRICT_WISE = 'https://api.covid19india.org/state_district_wise.json'    
  5.     
  6. response = requests.get(API_URL_ALL_DATA)    
  7.     
  8. def get_daily_stats(response):    
  9.     try:    
  10.         all_cases_data_list = response.json()['cases_time_series']    
  11.         latest_cases_data = all_cases_data_list[len(all_cases_data_list) - 1]    
  12.         formatted_data = f'''''''  
  13.             COVID INDIA DAILY STATS:  
  14.             AS of {latest_cases_data['date']}  
  15.             ******************************  
  16.             Total Confirmeed Cases : {latest_cases_data['totalconfirmed']}  
  17.             Total Recovered Cases : {latest_cases_data['totalrecovered']}  
  18.             Total Deaths Reported: {latest_cases_data['totaldeceased']}  
  19.             Confirmed Cases Yesterday: {latest_cases_data['dailyconfirmed']}  
  20.             Confirmed Recovered Cases Yesterday: {latest_cases_data['dailyrecovered']}  
  21.             Deaths Reported Yesterday: {latest_cases_data['dailydeceased']}  
  22.             ******************************  
  23.         '''    
  24.         print(formatted_data)    
  25.     except:    
  26.         print('An error occurred while processing data')    
  27.     
  28. def get_top5_states_with_active_cases(response):    
  29.     try:    
  30.         all_states_data_list = response.json()['statewise']    
  31.         all_states_data_list.sort(    
  32.             key=lambda x: int(x['active']), reverse=True)    
  33.         top5_active_states = all_states_data_list[1:6]    
  34.         print('Top 5 states with most active cases in India:')    
  35.         for index, state in enumerate(top5_active_states):    
  36.             formatted_data = f'''''''  
  37.             ********{index + 1}*************  
  38.             State: {state['state']}  
  39.             Active: {state['active']}  
  40.             Total Confirmed : {state['confirmed']}   
  41.             ***************************  
  42.           
  43.             '''    
  44.             print(formatted_data)    
  45.     except Exception as error:    
  46.         print(f'An error occured while processing data, {error}')    
  47.     
  48. if __name__ == '__main__':    
  49.     get_daily_stats(response)    
  50.     get_top5_states_with_active_cases(response)    
From my JavaScript based mental model, I was often trying to access the JSON object data using the . method which does not work in Python. Here we have to access object values using the bracket notation []. This is what I had to remember and update my mental model.
 
The script can be hosted in a server and can be run each day if we want to fetch the daily stats automatically.
 
Apart from building the simple COVID-tracker, I also explored building a basic twitter bot using the very convenient https://www.tweepy.org/ library that provides a wrapper over the twitter API to automate twitter tasks. There are tons of useful articles available on it already so I will be sharing the links to the resources instead.
 
Here is a list of resources that leverages Python scripting to create useful tasks.
  • https://github.com/geekcomputers/Python - Awsome Python scripts (My favorite)
  • https://github.com/realpython/python-scripts - Github repo of some useful Python scripts
  • https://github.com/Logan1x/Python-Scripts/tree/master/bin - Cool Python Scripts
  • https://github.com/hastagAB/Awesome-Python-Scripts - Curated Python Scripting Projects
Here are some reference articles for scripting with Python.
  • https://realpython.com/python-send-email/
  • https://realpython.com/twitter-bot-python-tweepy/
  • https://realpython.com/openpyxl-excel-spreadsheets-python/
I shall be exploring more on scripting with Python while working on projects in the upcoming days and learn about the new possibilities that can be unlocked. Scripting assists in in automating a lot of the monotonous redundant tasks by delegating them to computers so that we can focus on other important aspects of programming πŸ™‚
 
Tomorrow I shall be diving into another interesting area - Web Scraping with Python and exploring the basics while searching for new possibilities.
 
Have a great one!