-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_client.py
More file actions
240 lines (201 loc) · 7.85 KB
/
calendar_client.py
File metadata and controls
240 lines (201 loc) · 7.85 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import datetime
import os.path
import pytz # NEW: Import for time zone handling
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"]
def get_calendar_service():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
# 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:
# IMPORTANT: The user must provide their own 'credentials.json' file
# from the Google Cloud Console.
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
# This is the correct, manual flow for a command-line environment.
# 1. Generate the authorization URL.
# The redirect_uri must be set to 'urn:ietf:wg:oauth:2.0:oob' for the
# manual, 'out-of-band' (OOB) flow. This must also be an authorized
# redirect URI in your Google Cloud Console project.
flow.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
auth_url, _ = flow.authorization_url(prompt="consent")
print("Please go to this URL to authorize access:")
print(auth_url)
# 2. Have the user enter the authorization code.
code = input("Enter the authorization code: ")
# 3. Exchange the code for credentials.
flow.fetch_token(code=code)
creds = flow.credentials
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("calendar", "v3", credentials=creds)
return service
except HttpError as error:
print(f"An error occurred: {error}")
return None
def get_primary_calendar_timezone(service):
"""Fetch the time zone of the primary calendar."""
try:
calendar = service.calendars().get(calendarId="primary").execute()
return calendar.get("timeZone", "UTC")
except HttpError as error:
print(f"Error fetching time zone: {error}")
return "UTC"
def get_events_in_range(days_in_future=30):
"""
Fetches all events within a given future range from today.
"""
service = get_calendar_service()
if not service:
return []
now = datetime.datetime.utcnow()
time_min = now.isoformat() + "Z"
time_max = (now + datetime.timedelta(days=days_in_future)).isoformat() + "Z"
try:
events_result = (
service.events()
.list(
calendarId="primary",
timeMin=time_min,
timeMax=time_max,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
return events_result.get("items", [])
except HttpError as error:
print(f"An error occurred while fetching events: {error}")
return []
def get_daily_events():
"""
Fetches all events for the current day from the user's primary calendar.
"""
service = get_calendar_service()
if not service:
return []
# Get the user's time zone
timezone_str = get_primary_calendar_timezone(service)
user_tz = pytz.timezone(timezone_str)
# Get current time in user's time zone
now = datetime.datetime.now(user_tz)
# Set start of day (00:00) and end of day (23:59:59.999999) in user's time zone
time_min = now.replace(hour=0, minute=0, second=0, microsecond=0).isoformat()
time_max = now.replace(hour=23, minute=59, second=59, microsecond=999999).isoformat()
print(f"Getting events for today from {time_min} to {time_max} in {timezone_str}")
try:
events_result = (
service.events()
.list(
calendarId="primary",
timeMin=time_min,
timeMax=time_max,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
if not events:
print("No upcoming events found for today.")
return []
return events
except HttpError as error:
print(f"An error occurred: {error}")
return []
def create_event(summary, start_time, end_time, timezone="UTC", recurrence=None):
"""
Creates an event on the user's primary calendar.
Args:
summary: Event title
start_time: ISO format start time
end_time: ISO format end time
timezone: Timezone (default: UTC)
recurrence: List of RRULE strings for recurring events
Example: ['RRULE:FREQ=WEEKLY;COUNT=10;BYDAY=MO']
"""
service = get_calendar_service()
if not service:
return None
# Fetch the actual time zone of the primary calendar
actual_timezone = get_primary_calendar_timezone(service)
event = {
"summary": summary,
"start": {"dateTime": start_time, "timeZone": actual_timezone},
"end": {"dateTime": end_time, "timeZone": actual_timezone},
}
# Add recurrence rules if provided
if recurrence:
event["recurrence"] = recurrence
try:
created_event = (
service.events()
.insert(calendarId="primary", body=event)
.execute()
)
if recurrence:
print(f"Recurring event created: {created_event.get('htmlLink')}")
else:
print(f"Event created: {created_event.get('htmlLink')}")
return created_event
except HttpError as error:
print(f"An error occurred: {error}")
return None
def update_event(event_id, new_start_time, new_end_time):
"""
Updates an existing event with new start and end times.
"""
service = get_calendar_service()
if not service:
return None
# Fetch the actual time zone of the primary calendar
actual_timezone = get_primary_calendar_timezone(service)
try:
# First, get the existing event to preserve other details
existing_event = service.events().get(calendarId="primary", eventId=event_id).execute()
# Update only the start and end times
existing_event['start'] = {"dateTime": new_start_time, "timeZone": actual_timezone}
existing_event['end'] = {"dateTime": new_end_time, "timeZone": actual_timezone}
# Update the event
updated_event = (
service.events()
.update(calendarId="primary", eventId=event_id, body=existing_event)
.execute()
)
print(f"Event updated: {updated_event.get('htmlLink')}")
return updated_event
except HttpError as error:
print(f"An error occurred while updating event: {error}")
return None
def delete_event(event_id):
"""
Deletes an event from the user's primary calendar.
"""
service = get_calendar_service()
if not service:
return None
try:
service.events().delete(calendarId="primary", eventId=event_id).execute()
print(f"Event deleted successfully: {event_id}")
return True
except HttpError as error:
print(f"An error occurred while deleting event: {error}")
return False