Skip to content

Instantly share code, notes, and snippets.

@gaucheph
Created January 10, 2017 00:44
Show Gist options
  • Save gaucheph/4b6373b695bea0c23db67a5f00c8f85c to your computer and use it in GitHub Desktop.
Save gaucheph/4b6373b695bea0c23db67a5f00c8f85c to your computer and use it in GitHub Desktop.
Generates list of dates at specific intervals before a given date then creates an *.ics file with those dates as events
from datetime import datetime, timedelta
from icalendar import Calendar, Event
import tempfile, os
# lists of date itervals specific to each type of schedule item
milestones_symphony = [7, 14, 28, 112]
milestones_opera = [7, 14, 21, 28]
milestones_other = [7, 28, 364]
# pass in one of the lists
def calculate_milestones(input_date, date_intervals_list):
dates = []
for interval in date_intervals_list:
dates.append(input_date - timedelta(days=interval))
return dates
def create_calendar(input_dates):
# so we make a calendar to which we add all the events
cal = Calendar()
# adds a new event to the calendar for each date in the list
for each_date in input_dates:
event = Event()
event.add('dtstart', each_date)
cal.add_component(event)
# makes a temporary file to export the calendar to
directory = tempfile.mkdtemp()
f = open(os.path.join(directory, 'milestones.ics'), 'wb')
f.write(cal.to_ical())
f.close()
# prints the location of the ics file
print(directory)
my_milestones = calculate_milestones(datetime.now(), milestones_other)
for ms in my_milestones:
print(ms)
create_calendar(my_milestones)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment