-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal.py
121 lines (101 loc) · 4.29 KB
/
cal.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
from __future__ import print_function
import datetime
import pickle
from dataclasses import dataclass
from typing import List
import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import constants
import models
from models import User
credentials_path = constants.GOOGLE_CREDENTIALS_PATH
token_path = constants.GOOGLE_TOKEN_PATH
@dataclass
class Event:
location: str
description: str
start: datetime.datetime
end: datetime.datetime
attendees: list
summary: str = "Your Weather Window"
timezone: str = 'America/Los_Angeles'
@staticmethod
def _format_datetime(timestamp: datetime.datetime):
return str(timestamp).replace(' ', 'T')
def to_dict(self):
return {
'summary': self.summary,
'location': self.location,
'description': self.description,
'start': {
'dateTime': self._format_datetime(self.start),
'timeZone': self.timezone,
},
'end': {
'dateTime': self._format_datetime(self.end),
'timeZone': self.timezone,
},
'attendees': [{'email': e} for e in self.attendees],
'guestsCanSeeOtherGuests': False,
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'popup', 'minutes': 10},
],
},
}
class Calendar:
def __init__(self, scopes=None, host=None, token_path=token_path):
if scopes is None:
scopes = ['https://www.googleapis.com/auth/calendar.events']
self.host = host
self.scopes = scopes
self.token_path = token_path
self.service = self._authenticate(self.scopes, self.host)
def _authenticate(self, scopes, host):
creds = None
# The file [email protected] stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if self.token_path.exists():
with open(self.token_path, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentials_path, scopes)
creds = flow.run_local_server(host=host or 'localhost',
port=0)
# Save the credentials for the next run
with open(self.token_path, 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
self.authenticated = True
return service
def create_event(self, event: Event):
event = self.service.events().insert(calendarId='primary', body=event.to_dict()).execute()
print('Event Inserted')
return event
def delete_event(self, event_id):
self.service.events().delete(calendarId='primary', eventId=event_id).execute()
def get_calendar_event(location: models.Location, window: pd.Series, attendees: List["User.email"], timezone: str):
return Event(location=location.place,
summary=f" {constants.WEATHER_EMOJI_MAPPING.get(window.icon, '🌞')} Your weather window in {location.place}",
description=f"It's going to be {window.summary}, "
f"with a probability of rain of "
f"{window.precip_probability} and feeling like "
f"{window.apparent_temperature}°C."
f"<br> <br>"
f"Jealous of your colleague's weather window?"
f" Get your own at https://weather-window-app.herokuapp.com",
start=window.weather_timestamp,
end=window.weather_timestamp + datetime.timedelta(hours=1),
attendees=attendees,
timezone=timezone)
if __name__ == '__main__':
cal = Calendar()