Upload google_calendar.py
Browse files- google_calendar.py +43 -0
google_calendar.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pickle
|
| 3 |
+
import datetime
|
| 4 |
+
from google.oauth2.credentials import Credentials
|
| 5 |
+
from google_auth_oauthlib.flow import InstalledAppFlow
|
| 6 |
+
from google.auth.transport.requests import Request
|
| 7 |
+
from googleapiclient.discovery import build
|
| 8 |
+
|
| 9 |
+
SCOPES = ['https://www.googleapis.com/auth/calendar']
|
| 10 |
+
|
| 11 |
+
def get_calendar_service():
|
| 12 |
+
creds = None
|
| 13 |
+
if os.path.exists('token.pickle'):
|
| 14 |
+
with open('token.pickle', 'rb') as token:
|
| 15 |
+
creds = pickle.load(token)
|
| 16 |
+
if not creds or not creds.valid:
|
| 17 |
+
if creds and creds.expired and creds.refresh_token:
|
| 18 |
+
creds.refresh(Request())
|
| 19 |
+
else:
|
| 20 |
+
flow = InstalledAppFlow.from_client_secrets_file(
|
| 21 |
+
'credentials.json', SCOPES)
|
| 22 |
+
creds = flow.run_local_server(port=0)
|
| 23 |
+
with open('token.pickle', 'wb') as token:
|
| 24 |
+
pickle.dump(creds, token)
|
| 25 |
+
service = build('calendar', 'v3', credentials=creds)
|
| 26 |
+
return service
|
| 27 |
+
|
| 28 |
+
def add_event(summary, description, start_time, end_time):
|
| 29 |
+
service = get_calendar_service()
|
| 30 |
+
event = {
|
| 31 |
+
'summary': summary,
|
| 32 |
+
'description': description,
|
| 33 |
+
'start': {
|
| 34 |
+
'dateTime': start_time,
|
| 35 |
+
'timeZone': 'America/Bogota',
|
| 36 |
+
},
|
| 37 |
+
'end': {
|
| 38 |
+
'dateTime': end_time,
|
| 39 |
+
'timeZone': 'America/Bogota',
|
| 40 |
+
},
|
| 41 |
+
}
|
| 42 |
+
event = service.events().insert(calendarId='primary', body=event).execute()
|
| 43 |
+
return event.get('htmlLink')
|