Created
January 23, 2013 21:16
-
-
Save alghanmi/4613481 to your computer and use it in GitHub Desktop.
iCalWeekGenerator is a commandline based tool that could be used to generate recurring weekly events with an icon in iCal format. The goal is to have a small icon on every Monday (or other day) holding the week number. I use this to mark weeks in the academic calendar.
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
| import argparse | |
| from datetime import date | |
| from datetime import datetime | |
| from datetime import timedelta | |
| """ | |
| TODO: Add timezone support | |
| """ | |
| # Global Variables | |
| DAYS_PER_WEEK = 7 | |
| ICON_URL = 'X-GOOGLE-CALENDAR-CONTENT-ICON:http://www-scf.usc.edu/~alghanmi/images/week{}.png' | |
| ICON_ABOUT_URL = 'X-GOOGLE-CALENDAR-CONTENT-URL:http://www-scf.usc.edu/~alghanmi/calendar/about.html' | |
| def getDate(datestr): | |
| """ Given a date in %Y-%m-%d, return the date object """ | |
| return datetime.strptime(datestr, '%Y-%m-%d').date() | |
| def getvCalendarHeader(calName): | |
| """ vCalendar Header """ | |
| if calName == None: | |
| calName = '' | |
| return ['BEGIN:VCALENDAR', | |
| 'VERSION:2.0', | |
| 'X-WR-CALNAME:{}'.format(calName), | |
| 'X-WR-TIMEZONE:US/Pacific', | |
| 'CALSCALE:GREGORIAN'] | |
| def getvCalendarFooter(): | |
| """ vCalendar Header """ | |
| return ['END:VCALENDAR'] | |
| def getvEvent(startDate, title, weekNo, icon): | |
| """ vEvent Entry into Calendar """ | |
| eventStartString = date.strftime(startDate, '%Y%m%d') | |
| eventEndSting = date.strftime(startDate + timedelta(days=1), '%Y%m%d') | |
| if title == None: | |
| title = 'Week {}'.format(weekNo) | |
| vevent = [] | |
| vevent.append('BEGIN:VEVENT') | |
| vevent.append('DTSTART;VALUE=DATE:{}'.format(eventStartString)) | |
| vevent.append('DTEND;VALUE=DATE:{}'.format(eventEndSting)) | |
| vevent.append('SUMMARY:{}'.format(title)) | |
| if(icon == True): | |
| vevent.append('X-GOOGLE-CALENDAR-CONTENT-TITLE:{}'.format(title)) | |
| vevent.append(ICON_URL.format(weekNo)) | |
| vevent.append(ICON_ABOUT_URL) | |
| vevent.append('X-GOOGLE-CALENDAR-CONTENT-TYPE:text/html') | |
| vevent.append('X-GOOGLE-CALENDAR-CONTENT-WIDTH:205') | |
| vevent.append('X-GOOGLE-CALENDAR-CONTENT-HEIGHT:45') | |
| vevent.append('END:VEVENT') | |
| return vevent | |
| # Argument Parser | |
| parser = argparse.ArgumentParser() | |
| # Required arguments | |
| parser.add_argument('start', help='Start date of first week in yyyy-m-d format', type=getDate) | |
| parser.add_argument('weeks', help='number of weeks to generate', type=int) | |
| # Optional Arguments | |
| parser.add_argument('-n', '--name', help='Calendar name', type=str, dest='name') | |
| parser.add_argument('-t', '--event-title', help='Title for weekly event to prepend week number', type=str, dest='title') | |
| parser.add_argument('-f', '--first-week-day', help='The first day of the week', choices=['Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'], default='Mon', dest='weekday') | |
| parser.add_argument('-e', '--events-only', help='Only output events without calendar headers', action='store_true', dest='vevent') | |
| parser.add_argument('-s', '--start-number', help='The number of the first week', type=int, default=1, dest='weekNo') | |
| parser.add_argument('-i', '--icons', help='Add icons to events', action='store_true', dest='icons') | |
| # Parse Arguments | |
| args = parser.parse_args() | |
| cal = [] | |
| # Print vCal Header | |
| if args.vevent == False: | |
| cal.append(getvCalendarHeader(args.name)) | |
| weeks = args.weekNo | |
| sdate = args.start | |
| for i in range(0, args.weeks): | |
| weekTitle = args.title + " " + str(weeks) | |
| cal.append(getvEvent(sdate, weekTitle, weeks, args.icons)) | |
| sdate += timedelta(days=7) | |
| weeks += 1 | |
| if args.vevent == False: | |
| cal.append(getvCalendarFooter()) | |
| for c in cal[:]: | |
| for line in c[:]: | |
| print line | |
| print '' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For usage instructions:
Example to generate an iCal calendar for a 17-week semester starting on 2013-01-14 with weeks starting on a Monday:
This command does the same, but without generating the vCalendar code. This is used to insert into an existing calendar: