-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_service.py
More file actions
69 lines (53 loc) · 1.96 KB
/
calendar_service.py
File metadata and controls
69 lines (53 loc) · 1.96 KB
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
from datetime import datetime, timedelta
import os.path
import pickle
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from google.auth.transport.requests import Request
SCOPES = ["https://www.googleapis.com/auth/calendar"]
def get_calendar_service():
creds = None
if os.path.exists("token_calendar.pkl"):
with open("token_calendar.pkl", "rb") as token:
creds = pickle.load(token)
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.json",
SCOPES,
)
creds = flow.run_local_server(port=0)
with open("token_calendar.pkl", "wb") as token:
pickle.dump(creds, token)
return build("calendar", "v3", credentials=creds)
def format_due_display(due_date, time):
start_date = datetime.fromisoformat(f"{due_date}T{time}")
return start_date.strftime("%b %d, %I:%M %p")
def create_calendar_event(title, due_date, description, time="18:00:00"):
try:
service = get_calendar_service()
start_date = datetime.fromisoformat(f"{due_date}T{time}")
end_date = start_date + timedelta(hours=1)
event = {
"summary": title,
"description": description,
"start": {
"dateTime": start_date.isoformat(),
"timeZone": "Asia/Kolkata",
},
"end": {
"dateTime": end_date.isoformat(),
"timeZone": "Asia/Kolkata",
},
}
event_result = service.events().insert(
calendarId="primary",
body=event,
).execute()
print("Calendar: OK Event created")
return event_result
except Exception as error:
print(f"Calendar: ERROR {error}")
return None