forked from metakgp/gyft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_ics.py
187 lines (142 loc) · 5.03 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
## Adds your timetable from `data.txt` to Google Calendar.
from __future__ import print_function
import os
import json
import datetime
import sys
# this script works only with Python 3
if sys.version_info[0] != 3:
print("This script works only with Python 3")
sys.exit(1)
import re
from icalendar import Calendar, Event
import dates
WORKING_DAYS = dates.get_dates()
import build_event
from update_subjects_json import update_sub_list
import argparse
import getpass
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input")
parser.add_argument("-o", "--output")
args = parser.parse_args()
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)
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
cal = Calendar()
cal.add("prodid", "-//Your Timetable generated by GYFT//mxm.dk//")
cal.add("version", "1.0")
"""
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
"""
def next_weekday(d, weekday):
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
###
"""
Creates an ICS file `timetable.ics` with the timetable data present inside the
input file `data.txt`
"""
def main():
# 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)
found_missing_sub = False
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(
"ERROR: Our subjects database does not have %s in it."
% subject_code
)
summary = input(
"INPUT: Please input the name of the course %s: " % subject_code
)
subjects[subject_code] = str(summary)
update_sub_list(subject_code, summary)
summary = summary.title()
found_missing_sub = True
# Find location of this class
location = data[day][time][1]
for lectureBegin, [periodBegin, 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)
if found_missing_sub:
print(
"Subject list has been updated. Please commit, push and raise a pull request at github.com/metakgp/gyft."
)
with open(OUTPUT_FILENAME, "wb") as f:
f.write(cal.to_ical())
print("INFO: Your timetable has been written to %s" % OUTPUT_FILENAME)
if __name__ == "__main__":
main()