-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack-ical.py
102 lines (81 loc) · 2.6 KB
/
slack-ical.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/python
from icalendar import Calendar
import icalendar
from datetime import datetime
from datetime import timedelta
import logging
import json
# Import requests and set it up to use cache
import requests
from httpcache import CachingHTTPAdapter
s = requests.Session()
s.mount('http://', CachingHTTPAdapter())
s.mount('https://', CachingHTTPAdapter())
###
cachefile='/tmp/slackical.cache'
slackbotname="MyBotName"
slackwebhookurl="https://hooks.slack.com/services/YOUR/URL/HERE"
channelFeeds = [
{
'calFeed': 'https://YOUR.ICAL.URL.HERE',
'channel': '#channel',
},
{
'calFeed': 'https://ANOTHER.ICAL.URL.HERE',
'channel': '@PRIVATEMESSAGE',
},
]
def getFeed( calFeed ):
message = ""
date = ""
headers = {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
}
requests.packages.urllib3.disable_warnings()
r = requests.get(calFeed, headers=headers)
if r.status_code == 304:
# read from cached file
cf=open(cachefile, 'r')
caldata=cf.read()
else:
caldata=r.content
cf=open(cachefile, 'w')
cf.write(caldata)
cf.close()
todayDates=[]
tomorrowDates=[]
cal = Calendar.from_ical(caldata)
for event in cal.walk('VEVENT'):
message=event.get('SUMMARY')
date=event.get('DTSTART').dt
if date == datetime.today().date():
todayDates.append({'Line': message, 'Date': date})
elif date == datetime.today().date() + timedelta(days=1):
tomorrowDates.append({'Line': message, 'Date': date})