-
Notifications
You must be signed in to change notification settings - Fork 0
/
IcsStreamIndexer.py
61 lines (49 loc) · 1.91 KB
/
IcsStreamIndexer.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
from icalendar import Calendar, Event
import requests
import threading
import datetime
from dateutil.parser import parse
from dateutil.tz import tzoffset
class IcsStreamIndexer:
def __init__(self, uri, margin=300, refreshInterval=0):
self.uri = uri
self.refreshInterval = refreshInterval
self.margin = datetime.timedelta(hours=0, minutes=0, seconds=margin)
self.update()
# 0 means one time
if ( self.refreshInterval ):
self.setInterval( self.update, self.refreshInterval )
def setInterval(self, func, sec):
def func_wrapper():
self.setInterval( func, sec )
func()
t = threading.Timer(sec, func_wrapper)
t.start()
return t
def update(self):
response = requests.get(self.uri)
self.schedule = Calendar.from_ical(response.text)
#print( self.schedule )
def getStreamLocations(self):
locations = set()
if ( self.schedule ):
for event in self.schedule.walk('vevent'):
location = str(event['location'])
if ( location ):
locations.add( str(event['location']) )
return list( locations )
def getActiveStreams(self,timestamp=datetime.datetime.now(tzoffset('UTC+2', 2*3600))):
if ( not self.schedule ):
return {}
locations = {}
# Distill active talks
for event in self.schedule.walk('vevent'):
start = event.get('dtstart').dt - self.margin
end = event.get('dtend').dt + self.margin + self.margin
location = str(event['location'])
# TODO: process description as stream uri
#description = str(event['description']) # HTML
#print( description )
if ( timestamp > start and timestamp < end ):
locations[ location ] = event['summary']
return locations