-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeCalendar.py
85 lines (66 loc) · 1.87 KB
/
writeCalendar.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
from pathlib import Path
from icalendar import Calendar, Event, vText
from datetime import datetime, timezone, tzinfo
from pytz import timezone
import re
import os
import json
cal = Calendar()
months = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
"May": 5,
"Jun": 6,
"Jul": 7,
"Aug": 8,
"Sep": 9,
"Oct": 10,
"Nov": 11,
"Dec": 12
}
def parseDate(date_str):
tokenized_date = date_str.split(",")
year = int(tokenized_date[2])
# tokenizes month and date
# ex: ['','Apr', '16']
tokenized_mon_dd = tokenized_date[1].split(" ")
month = tokenized_mon_dd[1]
day = int(tokenized_mon_dd[2])
return datetime(year, months[month], day, 0, 0, 0, 0, timezone("US/Eastern"))
def parseTime(time_str):
tokenized_time = re.split("([0-9]+):([0-9]+)\ (AM|PM)", time_str)
hh = int(tokenized_time[1])
mm = int(tokenized_time[2])
am_pm = tokenized_time[3]
if (am_pm == "PM" and hh != 12):
hh += 12
elif (hh == 12 and am_pm == "AM"):
hh = 0
return hh, mm
cal = Calendar()
def writeToCalendar(row):
event = Event()
event.add("summary", "Shift")
start_date = parseDate(row["Date"])
hh, mm = parseTime(row["Shift Start"])
start_date = start_date.replace(hour=hh, minute=mm)
event.add("dtstart", start_date)
end_date = parseDate(row["Date"])
hh, mm = parseTime(row["Shift End"])
end_date = end_date.replace(hour=hh, minute=mm)
event.add("dtend", end_date)
f = open("options.json")
options = json.load(f)
event["location"] = vText(options["worklocation"])
# Add event to calenar
cal.add_component(event)
return
def save():
directory = str(Path(__file__).parent) + "/output"
print("ics file will be outputted to ", directory)
f = open(os.path.join(directory, "hours.ics"), "wb")
f.write(cal.to_ical())
f.close()
return