Python 3 Script to Get Business Holidays of any Country From Given Date Using calendarific API Full Project For Beginners

 

 

Holidays

A fun Python script that uses an api to get you the holidays of any place you want on a given day

Description

  • It uses an api call to fetch the data
  • Then it transforms it into a json and parses it to get useful information
  • Finally it serves that information to the user

How to use

  • Generate an api key from calendarific.com
  • Create a file api_key.txt, put your api key in and keep it beside the python script
  • Run the script from your console
  • I also used Task Scheduler on Windows to run this script everyday

 

 

app.py

 

 

'''
This script uses the https://calendarific.com/ api
'''
import requests
from datetime import date

with open('api_key.txt', 'r') as key: # <-- use your own api key here
	api_key = key.read().strip()

country = 'IN' # <-- you can also change the country... just look up the iso code from the docs

now = date.today()
day = str(now.day)
month = str(now.month)
year = str(now.year)

url = f'https://calendarific.com/api/v2/holidays?&api_key={api_key}&country={country}&day={day}&month={month}&year={year}'

try:
	r = requests.get(url)
	data = r.json()

	holidays = data['response']['holidays']

except Exception:
	holidays = None

if holidays is None:
	print("Could not fetch data. Terminating")

elif len(holidays) == 0:
	print("No holidays today...")

else:
	print("\nToday's holidays :")
	print("*************************")

	for holiday in holidays:
		print("\n")
		print(holiday['name'])
		print(holiday['description'])

input()

Leave a Reply