-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_remainder.py
35 lines (27 loc) · 1.08 KB
/
class_remainder.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
from icalendar import *
import urllib.request
import datetime
def start_time(event):
return event['DTSTART'].dt
def lecturize(event):
return event['SUMMARY'].encode('utf-8')
def location(event):
return event['LOCATION'].encode('utf-8')
def get_latest_session(sessions):
right_now = datetime.datetime.now(sessions[0][1].tzinfo)
smallest_diff = sessions[0][1] - right_now
s = sessions[0]
for session in sessions:
current_diff = session[1] - right_now
if right_now < session[1] and smallest_diff > current_diff: # and smallest_diff < right_now - right_now:
s = session
smallest_diff = session[1] - right_now
return s
def get_stuff(url):
file = urllib.request.urlopen(url)
g = file.read()
gcal = Calendar.from_ical(g)
sessions = [(lecturize(e), start_time(e), location(e)) for e in gcal.walk('vevent')]
session = get_latest_session(sessions)
return str(session[0]) + '\n' + str(session[1]) + '\n' + str(session[2])
#return str(sessions[0][0]) + '\n' + str(sessions[0][1]) + '\n' + str(sessions[0][2])