Last active
April 28, 2023 01:50
-
-
Save danielgross/de9d68e4a7e50a044b01b2f797bae21f to your computer and use it in GitHub Desktop.
View all day events in upcoming year week by week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Export all calendars as zip file from Gcal to a folder (e.g. ~/Downloads). Run this file there. | |
| # Requires icalendar and isoweek. | |
| import csv | |
| from icalendar import Calendar, Event | |
| import datetime | |
| import glob | |
| import isoweek | |
| import subprocess | |
| import os | |
| import collections | |
| years = [2021, 2022, 2023] | |
| # Find the zip file in this folder that ends with .ical.zip | |
| working_folder = os.path.dirname(os.path.realpath(__file__)) | |
| calendar_folder = os.path.join(working_folder, 'calendar') | |
| # if there is a calendar folder in the working folder, use that | |
| if not os.path.exists(calendar_folder): | |
| print('Finding zip file...') | |
| zip_file = glob.glob('*.ical.zip')[0] | |
| print('Found zip file: %s' % zip_file) | |
| # Unzip into the calendar folder | |
| subprocess.call(['unzip', zip_file, '-d', calendar_folder]) | |
| # Set the input and output file paths | |
| ics_files = glob.glob('%s/*.ics' % calendar_folder) | |
| csv_file_path = os.path.join(calendar_folder, 'calendar.csv') | |
| # skip birthday calendars | |
| ics_files = [x for x in ics_files if 'Birthday' not in x] | |
| print("Found %s ics files" % len(ics_files)) | |
| weeks_dict = collections.defaultdict(list) | |
| # add every week in the years to the weeks_dict | |
| for year in years: | |
| # iterate through all the weeks in the year | |
| for week in isoweek.Week.weeks_of_year(year): | |
| weeks_dict[week] = [] | |
| # Loop through all the ics files | |
| for ics_file in ics_files: | |
| # loop through all events | |
| calendar_name = ics_file.split('/')[-1].split('_')[0].replace('.ical', '') | |
| for event in Calendar.from_ical(open(ics_file, 'rb').read()).walk('vevent'): | |
| # Get the start date of the event | |
| event_start = event.get('dtstart').dt | |
| if event_start.year not in years: | |
| continue | |
| all_day = getattr(event_start, 'hour', None) is None | |
| # if not all day event, skip | |
| if not all_day: | |
| # if 'Flight' not in event.get('summary'): | |
| # continue | |
| continue | |
| # get the week number of the event | |
| week = event_start.isocalendar()[1] | |
| # get the year of the event | |
| year = event_start.isocalendar()[0] | |
| # append to the weeks_dict | |
| weeks_dict[isoweek.Week(year, week)].append([calendar_name, event]) | |
| weeks_dict = collections.OrderedDict(sorted(weeks_dict.items())) | |
| writer = csv.writer(open(csv_file_path, 'w')) | |
| writer.writerow(['Week', 'Year', 'Event1', 'Event2', 'Event3', | |
| 'Event4', 'Event5', 'Event6', 'Event7']) | |
| for week, events in weeks_dict.items(): | |
| # format week as something like Jan 1 - Jan 7 | |
| week_str = f'{week.monday().strftime("%b %d")} - {week.sunday().strftime("%b %d")}' | |
| # format year as something like 2020 | |
| year_str = f'{week.year}' | |
| row = [week_str, year_str] | |
| for event in events: | |
| # format event text as Summary (Calendar Name) | |
| event_text = '%s (%s)' % (event[1].get('summary'), event[0]) | |
| row.append(event_text) | |
| if len(row) == 8: | |
| break | |
| writer.writerow(row) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment