-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcalmanager.py
90 lines (75 loc) · 3.58 KB
/
gcalmanager.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
from __future__ import print_function
import datetime
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar']
class GCalManager:
def __init__(self, calendar_id):
self.calendar_id = calendar_id
self.creds = None
self.service = None
if os.path.exists('token.json'):
self.creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not self.creds or not self.creds.valid:
if self.creds and self.creds.expired and self.creds.refresh_token:
self.creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
self.creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(self.creds.to_json())
self.service = build('calendar', 'v3', credentials=self.creds)
def set_calendar(self, calendar_id):
self.calendar_id = calendar_id
def create_event(self, name, desc, start_time, end_time, date):
event = None
try:
event = {
'summary': name,
#'location': 'Parc TecnoCampus Mataró-Maresme, Carrer d\'Ernest Lluch, 32, 08302 Mataró, Barcelona',
'description': desc,
'start': {
'dateTime': date + 'T' + start_time,
'timeZone': 'Europe/Madrid',
},
'end': {
'dateTime': date + 'T' + end_time,
'timeZone': 'Europe/Madrid',
},
}
event = self.service.events().insert(calendarId=self.calendar_id, body=event).execute()
except HttpError as error:
print('An error occurred: %s' % error)
except:
print('Unknown error')
return event
def get_next_events(self, n):
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events_result = self.service.events().list(calendarId=self.calendar_id, timeMin=now,
maxResults=n, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
return events
def create_event(self, event_id):
event = self.service.events().get(calendarId=self.calendar_id, eventId=event_id).execute()
return event
def delete_event(self, event_id):
deleted = self.service.events().delete(calendarId=self.calendar_id, eventId=event_id).execute()
return deleted
def move_event(self, event_id, destination_calendar_id):
moved = self.service.events().move(calendarId=self.calendar_id, eventId=event_id, destination=destination_calendar_id).execute()
return moved
def quickAdd(self, text):
created = self.service.events().quickAdd(calendarId=self.calendar_id, text=text).execute()
return created
def update_event(self, event_id, body):
updated = self.service.events().quickAdd(calendarId=self.calendar_id, evendId=event_id, body=body).execute()
return updated