Convert Schedule to .ics
This is the small project for convert schedule to .ics
files.
The relevant note are here:
Python Script
This Python script is designed to process textual input describing events, convert it into a structured schedule, and then generate .ics
(iCalendar) files for each event. These files are then bundled into a ZIP file for download. Let's break down the script into its key components using Markdown:
source code here
Importing Libraries
from icalendar import Calendar, Event
import pytz
from datetime import datetime, timedelta
import re
import io
import zipfile
icalendar
: Library for working with iCalendar files.pytz
: For timezone calculations.datetime
,timedelta
: For date and time manipulation.re
: Regular expression library (though it's imported but not used in the script).io
: Input/output operations, here used for creating in-memory files.zipfile
: For creating ZIP archives.
Function: create_ics_file
def create_ics_file(event_details, start_date, end_date):
...
- This function creates an individual
.ics
file for an event. - It initializes a calendar and an event, then sets various properties like summary, description, location, start and end times, and recurrence rules.
- Finally, it returns the calendar in iCalendar format.
Function: parse_input
def parse_input(input_text):
...
- Parses the input text to extract event details.
- It supports a specific format where each event's details are spread over multiple lines.
Function: parse_and_create_ics_files
def parse_and_create_ics_files(schedule, start_date, end_date):
...
- Converts the parsed schedule into
.ics
files. - It localizes the times to the 'America/Chicago' timezone.
- Finally, it bundles all
.ics
files into a ZIP file, returned as byte data.
Function: schedule_to_ics
def schedule_to_ics(request):
...
- This is the main function that interfaces with an external request.
- It processes input data from a request, either JSON or query parameters.
- After validating and parsing dates, it uses
parse_input
andparse_and_create_ics_files
to create the iCalendar data. - Returns the iCalendar data with appropriate HTTP headers for file download.
Summary
The script is a complete solution for converting text-based event descriptions into downloadable iCalendar files. It handles input parsing, date and time manipulation, file creation, and HTTP response generation. The use of timezone-aware datetime objects ensures accurate scheduling across different regions.
Local Version
Source code here
Most logic are same but the local version just need you save your code into .txt
file and inout start time and end time by hand.