简体   繁体   English

过滤来自 json 响应的结果,以仅显示没有结束日期的重复事件。 Python 和谷歌日历 API

[英]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.

相关问题 使用 Google 日历 API 和 python 的资源日历上的重复事件 - Recurring events on resource calendars using Google Calendar API and python 解析来自 api 的 JSON 响应并过滤结果 - Parse JSON response from api and filter results 使用Google Calendar API在夏令时中创建重复活动的错误? - Bug creating recurring events in daylight savings time with Google Calendar API? Google日历重复事件的发生时间(Python API) - Time of occurence of a Google calendar recurring event (Python API) Google Calendar API Python-导入多个事件 - Google Calendar API Python - Import Multiple Events QML 日历和 Google 日历 API 在 Python 事件集成 - QML Calendar and Google Calendar API in Python events integration 如何使用 Google 日历 API 将定期事件移动到 Python 中的其他日历? - How to move a recurring event to an other calendar in Python using Google Calendar API? python中来自Google日历的即将发生的事件 - Upcoming events from Google Calendar in python 如何在谷歌日历中返回特定日期的所有事件 API - How to return all events on a specific date in Google Calendar API 如何获取用于在 python 中过滤 google fit API 结果的日期的日期格式? - How can I get the date format for a date used to filter results in the google fit API in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM