如何在添加前检查Google日历查看事件是否已经存在

2024-04-23 17:38:22 发布

您现在位置:Python中文网/ 问答频道 /正文

有一个网站安排了一些活动,我可以把这些信息拉下来,解析,然后放到我的个人谷歌日历上。在我向Google日历添加事件之前,我想检查一下在我的Google日历中是否已经存在具有相同标题和时间的事件。如果是,跳过该事件。如果没有,请张贴。在

我的想法是从今天开始抓取所有的日期,然后得到接下来的X个事件数,就像它在Google自己的Quickstart tutorial中所做的那样。然后,对于每个潜在的新事件,检查它是否与日历中已经存在的事件的日期、时间和描述相匹配。在

以下是我认为可行的,但没有[编辑以更新代码]:

现在它不发布任何事件,即使时间没有被填满。在

        new_event = {
            'summary': event_summary,
            'location': event_location,
            'description': '',
            'start': {
                'dateTime': event_start,
                'timeZone': 'America/Chicago',
            },
            'end': {
                'dateTime': event_end,
                'timeZone': 'America/Chicago',
            },
            'reminders': {
                'useDefault': True,
            },


        }

        now = datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time

        events_result = service.events().list(calendarId=calendar_ID,
                                              timeMin=now,
                                              maxResults=50, singleEvents=True,
                                              orderBy='startTime').execute()
        events = events_result.get('items', [])

        for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            summary = event['summary']

            if start == event_start and summary == event_summary:
                break
            else:
                service.events().insert(calendarId=calendar_ID, body=new_event).execute()

任何帮助将不胜感激-我是自学成才,所以我的知识和能力是有限的。在


Tags: eventnewgetdatetimegoogle时间事件location
1条回答
网友
1楼 · 发布于 2024-04-23 17:38:22

我能在一点帮助下解决这个问题:

使用:

 new_event = {
            'summary': event_summary,
            'location': event_location,
            'description': '',
            'start': {
                'dateTime': event_start,
                'timeZone': 'America/Chicago',
            },
            'end': {
                'dateTime': event_end,
                'timeZone': 'America/Chicago',
            },
            'reminders': {
                'useDefault': True,
            },

        }

代码如下:

^{pr2}$

相关问题 更多 >