summaryrefslogtreecommitdiff
path: root/cal.py
blob: 348fa324c1bbf132dcb30787566deae3ce494d35 (plain)
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
import icalendar
import recurring_ical_events
import urllib.request
import datetime
from settings import getSettings

calendars = getSettings("calendars")
today = datetime.date.today()
output = []

'''
    For some reason Icloud calendars seem to be reversed,
    so the must be reversed to put events in the proper order.
'''


def isIcloud(url):
    index = url.find("icloud")
    if index > -1:
        return True
    return False


def getEvents(url, cName):
    ical_string = urllib.request.urlopen(url).read()
    calendar = icalendar.Calendar.from_ical(ical_string)
    events = recurring_ical_events.of(calendar).at(today)
    if len(events) != 0:
        if isIcloud(url):
            events.reverse()
        output.append(f"\nToday's events from {cName} calendar:")
        for event in events:
            name = event["SUMMARY"]
            try:
                start = event["DTSTART"].dt.strftime("%H:%M")
                end = event["DTEND"].dt.strftime("%H:%M")
                output.append(f"{name} from {start} to {end}")
            except:
                output.append(f"{name} All Day")


def getEventsFromCal():
    for calendar in calendars:
        url = calendars[calendar]
        getEvents(url, calendar)
    output[-1] = output[-1] + "\n"
    return output