[英]Filter results from json response to only show recurring events with no end date. Python and Google Calendar API
I'm using the Google Calendar API to get a list of Calendar events which are recurring and have no end date.我正在使用 Google Calendar API 来获取重复出现且没有结束日期的日历事件列表。 I've been trying to filter the json response to only return recurring events in the recurrence field, this is an example: 'recurrence': ['RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20230112T235959Z;BYDAY=FR']
我一直在尝试过滤 json 响应以仅返回重复字段中的重复事件,这是一个示例: 'recurrence': ['RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20230112T235959Z;BYDAY=FR']
I want to filter out the response to not return events which have an end date.我想过滤掉对不返回具有结束日期的事件的响应。 The UNTIL= part above means the recurring event will end in April.
上面的UNTIL=部分表示重复事件将在四月结束。 I only want the events to be returned if the recurrence field is like below: recurrence': ['RRULE:FREQ=WEEKLY;BYDAY=WE']
如果重复字段如下所示,我只希望返回事件: recurrence': ['RRULE:FREQ=WEEKLY;BYDAY=WE']
I've tried various ways to filter events with regex, substrings etc but nothing seems to work.我尝试了各种方法来使用正则表达式、子字符串等来过滤事件,但似乎没有任何效果。 I think a list comprehension may work here, but not sure how to go about it.
我认为列表理解可能在这里起作用,但不确定如何 go 了解它。 Any help is appreciated.
任何帮助表示赞赏。 my code is below:
我的代码如下:
try:
#call the calendar api to pull the recurring events from the resource calendar list
page_token = None
for el in cal_list:
events = cal_service.events().list(calendarId=el, pageToken=page_token).execute()
for event in events['items']:
if 'recurrence' in event: **#return recurring events only, I've tested and this works**
event_list.append(event)
print(event_list)
page_token = events.get('nextPageToken')
except HttpError as error:
print('An error occurred: %s' % error)
I've tried using data.find() to find UNTIL= substring but it errors out.我试过使用 data.find() 来查找 UNTIL= substring 但它出错了。 I tried using regex and it tells me it can only be used on a string.
我尝试使用正则表达式,它告诉我它只能用于字符串。
I've tried if 'recurrence' in event and 'UNTIL=' not in event: and it errors out.我已经尝试过 if 'recurrence' in event 和 'UNTIL=' not in event:并且它出错了。
recurrence
is a list of strings, so: recurrence
是一个字符串列表,所以:
if 'recurrence' in event and 'UNTIL=' not in event['recurrence'][0]:
...
or to be precise:或者准确地说:
if 'recurrence' in event and all('UNTIL=' not in r for e in event['recurrence']):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.