使用Google Calendar API和python在资源日历上重复发生事件

yuvru6vn  于 2023-01-11  发布在  Python
关注(0)|答案(1)|浏览(110)

我写了一个Python脚本与管理目录和谷歌日历API的。目标是从谷歌工作区域的所有资源日历的重复事件列表,然后修改事件重复结束后90天。我有脚本工作拉所有资源日历的电子邮件地址,将它们添加到一个列表中,然后从所有7个资源日历中提取事件。我似乎找不到一种方法来过滤和获取重复发生的事件,我的脚本获取单个事件和重复事件。我尝试使用event['recurringEventId'],但它出错了。是否有方法将结果从事件过滤为仅重复?
代码如下:

`import datetime
import os.path

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

#API scopes If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly', 
          'https://www.googleapis.com/auth/calendar.events', 
          'https://www.googleapis.com/auth/admin.directory.resource.calendar']

#Create empty list to hold resource calendar emails
cal_list = []
event_list = []

#Authorise app to access Google account
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:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.json', 'w') as token:
        token.write(creds.to_json())
        

"""Call directory api to list resource calendar email addresses
and add them to cal_list"""
with build('admin', 'directory_v1', credentials=creds) as service_admin:
    calendars = service_admin.resources().calendars().list(customer='C03r7ld3j', maxResults=40).execute()
    for calendar in calendars['items']:
        cal_list.append(calendar['resourceEmail'])
    print(cal_list)
      

service_cal = build('calendar', 'v3', credentials=creds)
#call the calendar api to pull the events from the resource calendar list
page_token = None
for el in cal_list:
    events = service_cal.events().list(calendarId=el, pageToken=page_token).execute()
    for event in events['items']:
        print(event)
    page_token = events.get('nextPageToken')`
flvlnr44

flvlnr441#

recurringEventId仅在重复发生事件的示例上设置(您仅在singleEvents=True时获得,但您不需要此设置)。但“父”重复发生事件设置了recurrence,以便您可以使用if event["recurrence"]过滤它。
使用gcsa库可以简化所有这些操作:

from gcsa.goolge_calendar import GoogleCalendar

gc = GoogleCalendar('credentials.json')

for cl in cal_list:
    for e in gc.get_events(calendar_id=cl):
        if e.recurrence:
             e.recurrence = ... # updated recurrence
             updated_event = gc.update_event(e)
             print(updated_event)

有关重复性,请参见recurrence docs

相关问题