Spaces:
No application file
No application file
File size: 3,844 Bytes
93dae80 | 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 | import json
from services import create_service
client_secret = 'credentials.json'
def construct_google_calendar_client(client_secret):
"""
Constructs a Google Calendar API client.
Parameters:
- client_secret (str): The path to the client secret JSON file.
Returns:
- service: The Google Calendar API service instance.
"""
API_NAME = 'calendar'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/calendar']
service = create_service(client_secret, API_NAME, API_VERSION, SCOPES)
return service
calendar_service = construct_google_calendar_client(client_secret=client_secret)
def create_calendar_list(calendar_name):
"""
Creates a new calendar list.
Parameters:
- calendar_name (str): The name of the new calendar list.
Returns:
- dict: A dictionary containing the ID of the new calendar list.
"""
calendar_list = {
'summary': calendar_name
}
created_calendar_list = calendar_service.calendarList().insert(body=calendar_list).execute()
return created_calendar_list
def list_calendar_list(max_capacity=200):
"""
Lists calendar lists until the total number of items reaches max_capacity.
Parameters:
- max_capacity (int or str, optional): The maximum number of calendar lists to retrieve. Defaults to 200.
If a string is provided, it will be converted to an integer.
Returns:
- list: A list of dictionaries containing cleaned calendar list information with 'id', 'name', and 'description'.
"""
if isinstance(max_capacity, str):
max_capacity = int(max_capacity)
all_calendars = []
all_calendars_cleaned = []
next_page_token = None
capacity_tracker = 0
while True:
calendar_list = calendar_service.calendarList().list(
maxResults=min(200, max_capacity - capacity_tracker),
pageToken=next_page_token
).execute()
calendars = calendar_list.get('items', [])
all_calendars.extend(calendars)
capacity_tracker += len(calendars)
if capacity_tracker >= max_capacity:
break
next_page_token = calendar_list.get('nextPageToken')
if not next_page_token:
break
for calendar in all_calendars:
all_calendars_cleaned.append(
{
'id': calendar['id'],
'name': calendar['summary'],
'description': calendar.get('description', '')
})
return all_calendars_cleaned
def list_calendar_events(calendar_id, max_capacity=20):
"""
Lists events from a specified calendar until the total number of events reaches max_capacity.
Parameters:
- calendar_id (str): The ID of the calendar from which to list events.
- max_capacity (int or str, optional): The maximum number of events to retrieve. Defaults to 20.
If a string is provided, it will be converted to an integer.
Returns:
- list: A list of events from the specified calendar.
"""
if isinstance(max_capacity, str):
max_capacity = int(max_capacity)
all_events = []
next_page_token = None
capacity_tracker = 0
while True:
events_list = calendar_service.events().list(
calendarId=calendar_id,
maxResults=min(250, max_capacity - capacity_tracker),
pageToken=next_page_token
).execute()
events = events_list.get('items', [])
all_events.extend(events)
capacity_tracker += len(events)
if capacity_tracker >= max_capacity:
break
next_page_token = events_list.get('nextPageToken')
if not next_page_token:
break
return all_events |