-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCalSyncHAB.py
127 lines (94 loc) · 6.11 KB
/
CalSyncHAB.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import httplib2
import os
import datetime
import argparse as AP
import Settings as S
import warnings
import requests
import time
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
Flags = AP.ArgumentParser(parents=[tools.argparser]).parse_args()
def GetCredentials():
with warnings.catch_warnings():
warnings.simplefilter('ignore')
CredentialStore = Storage(S.CredentialFilePath)
Credentials = CredentialStore.get()
if not Credentials or Credentials.invalid:
AuthenticationFlow = client.flow_from_clientsecrets(S.CalendarClientSecretFile, S.CalendarScope)
AuthenticationFlow.user_agent = S.ApplicationName
Credentials = tools.run_flow(AuthenticationFlow, CredentialStore, Flags)
return Credentials
def Main():
Credentials = GetCredentials()
HTTPAuthorization = Credentials.authorize(httplib2.Http())
CalendarService = discovery.build('calendar', 'v3', http = HTTPAuthorization)
CurrentTime = datetime.datetime.utcnow().isoformat() + 'Z'
CalendarEvents = CalendarService.events().list(
calendarId = S.CalendarId,
timeMin = CurrentTime,
maxResults = S.CalendarMaxEvents,
singleEvents = True,
orderBy = 'startTime').execute()
RetrievedEvents = CalendarEvents.get('items', [])
MaxEvents = int(S.CalendarMaxEvents)
if not RetrievedEvents:
print('No upcoming events found.')
if S.OpenHABPort.strip() != '':
TrimmedHostAndPort = S.OpenHABHostName.strip() + ':' + S.OpenHABPort.strip()
else:
TrimmedHostAndPort = S.OpenHABHostName.strip()
EventCounter = 0
for SingleEvent in range(0, MaxEvents):
EventCounter += 1
CalendarEventSummaryItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Summary'
OpenHABResponse = requests.post(CalendarEventSummaryItemURL, data = '', allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventLocationItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Location'
OpenHABResponse = requests.post(CalendarEventLocationItemURL, data = '', allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventDescriptionItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Description'
OpenHABResponse = requests.post(CalendarEventDescriptionItemURL, data = '', allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventStartTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_StartTime'
OpenHABResponse = requests.post(CalendarEventStartTimeItemURL, data = '1909-12-19T00:00:00.000+0100', allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventEndTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_EndTime'
OpenHABResponse = requests.post(CalendarEventEndTimeItemURL, data = '1909-12-19T00:00:00.000+0100', allow_redirects = True, headers={'Content-type': 'text/plain'})
time.sleep(2)
EventCounter = 0
for SingleEvent in RetrievedEvents:
EventSummary = ''
EventLocation = ''
EventDescription = ''
EventStartTime = None
EventEndTime = None
EventCounter += 1
if 'summary' in SingleEvent:
EventSummary = SingleEvent['summary']
if 'location' in SingleEvent:
EventLocation = SingleEvent['location']
if 'description' in SingleEvent:
EventDescription = SingleEvent['description']
if 'start' in SingleEvent:
EventStartTime = SingleEvent['start'].get('dateTime', SingleEvent['start'].get('date'))
try:
datetime.datetime.strptime(EventStartTime, '%Y-%m-%dT%H:%M:%S' + S.CalendarTimeZone)
except ValueError:
EventStartTime = EventStartTime + 'T00:00:00' + S.CalendarTimeZone
if 'end' in SingleEvent:
EventEndTime = SingleEvent['end'].get('dateTime', SingleEvent['end'].get('date'))
try:
datetime.datetime.strptime(EventEndTime, '%Y-%m-%dT%H:%M:%S' + S.CalendarTimeZone)
except ValueError:
EventEndTime = EventEndTime + 'T00:00:00' + S.CalendarTimeZone
CalendarEventSummaryItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Summary'
OpenHABResponse = requests.post(CalendarEventSummaryItemURL, data = EventSummary.encode('utf-8'), allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventLocationItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Location'
OpenHABResponse = requests.post(CalendarEventLocationItemURL, data = EventLocation.encode('utf-8'), allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventDescriptionItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_Description'
OpenHABResponse = requests.post(CalendarEventDescriptionItemURL, data = EventDescription.encode('utf-8'), allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventStartTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_StartTime'
OpenHABResponse = requests.post(CalendarEventStartTimeItemURL, data = EventStartTime, allow_redirects = True, headers={'Content-type': 'text/plain'})
CalendarEventEndTimeItemURL = 'http://' + TrimmedHostAndPort + '/rest/items/' + S.OpenHABItemPrefix + 'Event' + str(EventCounter) + '_EndTime'
OpenHABResponse = requests.post(CalendarEventEndTimeItemURL, data = EventEndTime, allow_redirects = True, headers={'Content-type': 'text/plain'})
if __name__ == '__main__':
Main()