-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_calendar.py
86 lines (72 loc) · 2.75 KB
/
google_calendar.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
import os
import datetime
import os.path
from base64 import b64decode, b64encode
from tempfile import NamedTemporaryFile
from datetime import datetime, timedelta
from typing import Any, Dict
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
import pytz
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.events.owned']
CALENDAR_ID = os.environ['CALENDAR_ID']
if token := os.getenv('TOKEN'):
with NamedTemporaryFile('wb') as f:
f.write(b64decode(token.encode('utf-8')))
f.seek(0)
creds = Credentials.from_authorized_user_file(f.name)
if creds.expired and creds.refresh_token:
creds.refresh(Request())
# https://developers.google.com/calendar/api/quickstart/python
service = build('calendar', 'v3', credentials=creds)
else:
raise Exception()
def get_token(credentials_filename: str):
flow = InstalledAppFlow.from_client_secrets_file(credentials_filename, SCOPES)
creds = flow.run_local_server(port=0)
return b64encode(creds.to_json().encode('utf-8')).decode('utf-8')
def next_n_events(n=100):
tz = pytz.timezone('America/New_York')
now = (datetime.now(tz) - timedelta(days=1)).isoformat()
events_result = service.events().list(
calendarId=CALENDAR_ID,
timeMin=now,
maxResults=n,
singleEvents=True,
orderBy='startTime',
showDeleted=False,
).execute()
return events_result.get('items', [])
def upsert_event(calendar_id: str, summary: str, start: datetime, end: datetime, description: str, existing_event: Dict[str, Any] | None):
event = {
'summary': summary,
'start': {
'dateTime': start.strftime('%Y-%m-%dT%H:%M:%S%z'),
'timeZone': 'America/New_York',
},
'end': {
'dateTime': end.strftime('%Y-%m-%dT%H:%M:%S%z'),
'timeZone': 'America/New_York',
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'popup', 'minutes': 30},
],
},
'extendedProperties': {
'private': {
'portlandCalendarId': calendar_id,
},
}
}
if description:
event['description'] = description
if existing_event:
return service.events().update(calendarId=CALENDAR_ID, eventId=existing_event['id'], body=event).execute()
return service.events().insert(calendarId=CALENDAR_ID, body=event).execute()
def delete_event(existing_event: Dict[str, Any]):
service.events().delete(calendarId=CALENDAR_ID, eventId=existing_event['id']).execute()