forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumCalendar.py
75 lines (53 loc) · 2.09 KB
/
terrariumCalendar.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
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
from datetime import datetime, timedelta, date
from operator import attrgetter
from icalendar import Calendar, Event
from icalevents.icalevents import events
import os.path
class terrariumCalendar(object):
ICS_FILE = 'calendar.ics'
def __init__(self):
if not os.path.isfile(terrariumCalendar.ICS_FILE):
ical = Calendar()
ical.add('prodid', '-//TerrariumPI calendar//terrarium.theyosh.nl//')
ical.add('version', '2.0')
event = Event()
event.add('uid','1')
event.add('summary', 'TerrariumPI initial github commit')
event.add('location', 'https://github.com/theyosh/TerrariumPI/commit/526d39a9ceac57768c6fffe6ffe19afd71782952')
event.add('dtstart', date(2016,1,14))
event.add('dtend', date(2016,1,14))
event.add('dtstamp', datetime.now())
ical.add_component(event)
with open(terrariumCalendar.ICS_FILE, 'wb') as fp:
fp.write(ical.to_ical())
self.__ical_data = None
with open(terrariumCalendar.ICS_FILE, 'rb') as fp:
self.__ical_data = fp.read()
def get_events(self,start,end):
if start is None:
start = datetime.now() - timedelta(days=1)
return sorted(events(string_content=self.__ical_data,start=start,end=end), key=attrgetter('start'))
def create_event(self,uid,title,message,location = None,start = None,stop = None):
ical = Calendar.from_ical(self.__ical_data)
if start is None:
start = datetime.now()
if stop is None:
stop = start
event = Event()
event.add('uid',str(datetime.now()) + '/' + str(start) + '/' + str(uid))
event.add('summary', title)
event.add('description', message)
if location is not None:
event.add('location', location)
event.add('dtstart', start)
event.add('dtend', stop)
event.add('dtstamp', datetime.now())
ical.add_component(event)
with open(terrariumCalendar.ICS_FILE, 'wb') as fp:
fp.write(ical.to_ical())
self.__ical_data = ical.to_ical()
def get_ical(self):
return self.__ical_data