Get Holidays List from Google Calendar API

 import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
import requests
from datetime import datetime
from typing import List

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)

app = FastAPI()

GOOGLE_CALENDAR_API_KEY = os.environ.get('GOOGLE_CALENDAR_API_KEY')
if not GOOGLE_CALENDAR_API_KEY:
    raise Exception("The GOOGLE_CALENDAR_API_KEY environment variable is not set.")

def fetch_holidays(country_code: str, year: str):
    url = f"https://www.googleapis.com/calendar/v3/calendars/{country_code}%23holiday%40group.v.calendar.google.com/events"
    params = {
        'key': GOOGLE_CALENDAR_API_KEY,
        'timeMin': f'{year}-01-01T00:00:00Z',
        'timeMax': f'{year}-12-31T23:59:59Z',
        'singleEvents': True,

About this template

A holiday fetching app that uses the Google Calendar API to display holidays based on country and year. Google calendar api allows to fetch holidays for current, previous and next year only. Requires GOOGLE_API_KEY to run.

Introduction to the Get Holidays List from Google Calendar API Template

Welcome to the Get Holidays List from Google Calendar API template! This template is designed to help you build an application that fetches holiday information for a specific country and year using the Google Calendar API. It's a great tool for developers who want to integrate holiday data into their applications without having to worry about the complexities of API integration and server setup.

Clicking Start with this Template

To begin using this template, simply click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial Setup: Adding Environment Secrets

Before you can use this template, you'll need to set up an environment secret for the Google Calendar API key. Here's how to do it:

  • Visit the Google Developers Console and create a new project or select an existing one.
  • Enable the Google Calendar API for your project.
  • Go to the "Credentials" section and create an API key.
  • Copy the API key you just created.
  • In the Lazy Builder interface, navigate to the Environment Secrets tab.
  • Create a new secret with the key `GOOGLE_CALENDAR_API_KEY` and paste the API key you copied as the value.

Test: Pressing the Test Button

Once you have set up your environment secret, you can test the application by clicking the "Test" button. This will deploy your app and launch the Lazy CLI.

Using the App

After pressing the "Test" button, Lazy will provide you with a dedicated server link to use the API. Additionally, since this template uses FastAPI, you will also be provided with a link to the API documentation.

To fetch holidays for a specific country and year, you will use the provided server link followed by the endpoint pattern `/holidays/{country_code}/{year}`. Replace `{country_code}` with the desired country code and `{year}` with the year you want to fetch holidays for.

Here's an example of how to make a request:


GET http://your-server-link/holidays/en.usa/2023

You should expect a response similar to this:


[
  {
    "kind": "calendar#event",
    "etag": "\"etag\"",
    "id": "event_id",
    "status": "confirmed",
    "htmlLink": "https://www.google.com/calendar/event?eid=event_id",
    "created": "datetime",
    "updated": "datetime",
    "summary": "New Year's Day",
    "description": "New Year's Day is a public holiday in the USA.",
    ...
  },
  ...
]

Integrating the App

If you want to integrate this holiday information into another service or frontend, you can use the server link provided by Lazy to make API requests from your application. Ensure that you handle the API responses correctly and display the holiday data as needed in your application.

For example, if you're building a calendar application, you can use the fetched holiday data to mark holidays on the calendar for users based on their country and year selection.

Remember to respect the Google Calendar API usage limits and terms of service when integrating and using the API in your application.

By following these steps, you should now have a functional application that can fetch and display holiday information from the Google Calendar API. Happy building!

Category
Technology
Last published
July 26, 2024

More templates like this

SecureUserAuthenticator

Develop a secure User Authentication system for users to register, log in, and manage their profiles, laying the foundation for user-specific data management and permissions in the CMS.

Laravel
Python
Flask
Javascript

Simple Multiplayer Telegram game

This app is a simple frontend for a game where users can upvote and downvote the most popular word in their country, learn about the flags of other countries, and view what other people voted for on a leaderboard.

Telegram
Python
Javascript

MP3ify: Youtube to MP3 Converter

A web application that allows users to download YouTube videos from URLs and provides the option to convert them to MP3 format.

Python
Flask
Home
/
Get Holidays List from Google Calendar API