- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
Have you thought about how many company holidays are available? Do you often check when the next holiday is? Would it be nice to have a calendar full of the holidays for the year? Today we will create a holiday calendar using Python using the Nylas APIs and visualize with Obsidian (A powerful knowledge base).
To install Python (if you don’t have it installed already), install the Nylas Python SDK, set up the .env file and get your tokens for the dashboard. Also, read the blog post How to Send Emails with the Nylas Python SDK for further details on setup.
To install Obsidian (as we will generate a Markdown file), follow their instructions.
Our .env file will contain the following information:
CLIENT_ID = "<CLIENT_ID>" CLIENT_SECRET = "<CLIENT_SECRET>" ACCESS_TOKEN = "<ACCESS_TOKEN>" CALENDAR_ID = “<COMPANY_HOLIDAYS_CALENDAR>”
Create a file and call it Holidays_Generator.py with the following code:
# Import your dependencies import os import sys import re import calendar from datetime import date import datetime from nylas import APIClient from dotenv import load_dotenv # Load your env variables load_dotenv() # Initialize your Nylas API client nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ACCESS_TOKEN"), ) # Array to hold all events yearly_events = [] # Months of the year months = range(1, 13) # Year that we want to print out year = str(sys.argv[1]) # Path of file file_name = ( "<YOUR_OBSIDIAN_PATH>/Documents/Blag/Holidays Calendar " + year + ".md" ) START_TIME = int(datetime.datetime(int(year), 1, 1, 0, 0, 0).strftime('%s')) END_TIME = int(datetime.datetime(int(year), 12, 31, 23, 0, 0).strftime('%s')) # Use RegEx to filter out by year regex = re.compile(year) # Get all events from our Holidays Calendar events = nylas.events.where(calendar_id=os.environ.get("HOLIDAYS_CALENDAR_ID"), starts_after = START_TIME, ends_before = END_TIME) # Store all events in an array for event in events: yearly_events.append(event.when["date"] + " " + event.title) # Use our RegEx to get only events happening on the requested year #filtered_events = [event for event in yearly_events if regex.match(event)] filtered_events = yearly_events # Open the file to write to it with open(file_name, "w") as f: f.write(f"**Holidays Calendar {year}**\n\n") # Loop months to get events for each of them for month in months: if month < 10: s_month = "0" + str(month) else: s_month = str(month) # Get only the current month using RegEx regex = re.compile(".*-" + s_month + "-.*") filtered_month = [event for event in filtered_events if regex.match(event)] # Print name of month f.write(f"|{calendar.month_name[month]}| |") f.write("\n|-|-|") # If we have events for that month, print them out if len(filtered_month) > 0: for filtered in filtered_month: # We want to get the day the event happens day = re.findall("(..\s)", filtered) # And we want the event title event = re.findall("(\s.*)", filtered) f.write(f"\n|{day[0]}|{event[0]}|") f.write("\n\n") else: f.write("\n\n") # Close the file f.close()
The code is doing the following items:
To create a holiday calendar, we need to run our script passing the year that we want to process:
$ python3 Holiday_Calendar.py 2023
This will create the Holiday Calendar 2023.md file on our Obsidian installation.
If we open Obsidian, we will find the newly created note displaying the holidays for the specified year.
To learn more about Calendars and Events, check our official documentation on Calendars and Events.
Why don’t you sign up Nylas for free and start building!
Don’t miss the action, watch our LiveStream Coding with Nylas:
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.