-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_ics.py
168 lines (127 loc) · 4.64 KB
/
generate_ics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from __future__ import print_function
import os
import json
import datetime
import re
from icalendar import Calendar
import dates
import build_event
import argparse
WORKING_DAYS = dates.get_dates()
DEBUG = False
GENERATE_ICS = True
TIMETABLE_DICT_RE = (
"([0-9]{1,2}):([0-9]{1,2}):([AP])M-([0-9]{1,2}):([0-9]{1,2}):([AP])M"
)
timetable_dict_parser = re.compile(TIMETABLE_DICT_RE)
cal = Calendar()
cal.add("prodid", "-//Your Timetable generated by GYFT//mxm.dk//")
cal.add("version", "1.0")
def next_weekday(d, weekday):
"""
Given a starting timestamp d and a weekday number d (0-6)
return the timestamp of the next time this weekday is going to happen
"""
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
return d + datetime.timedelta(days_ahead)
def get_stamp(argument, date):
"""
argument is a 3-tuple such as
('10', '14', 'A') : 1014 HRS on date
('10', '4', 'P') : 2204 HRS on date
"""
hours_24_format = int(argument[0])
# Note:
# 12 PM is 1200 HRS
# 12 AM is 0000 HRS
if argument[2] == "P" and hours_24_format != 12:
hours_24_format = (hours_24_format + 12) % 24
if argument[2] == "A" and hours_24_format == 12:
hours_24_format = 0
return build_event.generateIndiaTime(
date.year, date.month, date.day, hours_24_format, int(argument[1])
)
# days to number
days = {}
days["Monday"] = 0
days["Tuesday"] = 1
days["Wednesday"] = 2
days["Thursday"] = 3
days["Friday"] = 4
days["Saturday"] = 5
###
def main():
"""
Creates an ICS file `timetable.ics` with the timetable data
present inside the input file `data.txt`
"""
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input")
parser.add_argument("-o", "--output")
args = parser.parse_args()
INPUT_FILENAME = args.input if args.input else "data.txt"
if not os.path.exists(INPUT_FILENAME):
print("Input file", INPUT_FILENAME, "does not exist.")
os._exit(1)
OUTPUT_FILENAME = "timetable.ics" if args.output is None else args.output
# Get your timetable
with open(INPUT_FILENAME) as data_file:
data = json.load(data_file)
# Get subjects code and their respective name
with open("subjects.json") as data_file:
subjects = json.load(data_file)
for day in data:
startDates = [next_weekday(x[0], days[day]) for x in WORKING_DAYS]
for time in data[day]:
# parsing time from time_table dict
# currently we only parse the starting time
# duration of the event is rounded off to the closest hour
# i.e 17:00 - 17:55 will be shown as 17:00 - 18:00
parse_results = timetable_dict_parser.findall(time)[0]
lectureBeginsStamps = [
get_stamp(parse_results[:3], start) for start in startDates
]
durationInHours = data[day][time][2]
# Find the name of this course
# Use subject name if available, else ask the user for the subject
# name and use that
# TODO: Add labs to `subjects.json`
subject_code = data[day][time][0]
summary = subject_code
description = subject_code
if subject_code in subjects.keys():
summary = subjects[subject_code].title()
else:
print(
"\n :( Our subjects database does not have %s in it." % subject_code
)
summary = input(
"\t Please input the name of the course %s: " % subject_code
)
subjects[subject_code] = str(summary)
summary = summary.title()
# Find location of this class
location = data[day][time][1]
for lectureBegin, [_, periodEnd] in zip(lectureBeginsStamps, WORKING_DAYS):
event = build_event.build_event_duration(
summary,
description,
lectureBegin,
durationInHours,
location,
"weekly",
periodEnd,
)
cal.add_component(event)
if DEBUG:
print(event)
with open(OUTPUT_FILENAME, "wb") as f:
f.write(cal.to_ical())
print("\n:) Your timetable has been written to %s" % OUTPUT_FILENAME)
print("You can now add this file to your Calendar")
print("To add it to google Calendar visit tutorial at :")
print(" https://goo.gl/WvdUsP \n")
if __name__ == "__main__":
main()