简体   繁体   English

如何使用Python确定Google日历中的当前事件?

[英]How can I determine the current event in Google Calendar using Python?

I'm building a thermostat that pulls its target temperatures from a Google Calendar. 我正在建立一个可从Google日历中提取其目标温度的恒温器。 In doing so, I need to determine the event happening now in said Google Calendar. 为此,我需要确定所说Google日历中现在正在发生的事件。 At present, I'm fetching the .ics file from Google and then parsing it with Python icalendar library . 目前,我正在从Google获取.ics文件,然后使用Python icalendar library对其进行解析。

I then iterate through every event in the calendar and test: 然后,我遍历日历中的每个事件并进行测试:

  1. Is the event today? 今天是活动吗?
  2. Has the event started? 活动开始了吗?
  3. Has the event not ended? 活动还没有结束吗?

After checking every event (assuming there are no overlapping events), I get one event where the answer is YES for all three questions. 检查完每个事件(假设没有重叠事件)后,我得到一个事件,所有三个问题的答案均​​为是。 That's the current event. 那是当前事件。

Is there a better way to do this? 有一个更好的方法吗? In particular, is there any way to avoid iterating through every event in the calendar? 特别是,是否有任何方法可以避免对日历中的每个事件进行迭代?

You should use the Google Calendar API . 您应该使用Google Calendar API You can ask it for the events within a certain period of time, and it'll respond with some nice JSON that will give you the information. 您可以要求它在一段时间内提供事件,并且它将以一些不错的JSON进行响应,该JSON将为您提供信息。 There's even a Python client that will take care of translating your requests to and from JSON. 甚至还有一个Python客户端都会负责将您的请求与JSON相互转换。

There isn't a way of querying for the current event directly, but you can certainly give it a limited min and max time, so you'll only have a small number of events to search through. 没有直接查询当前事件的方法,但是您可以给它指定最小和最大时间,因此您只能搜索少量事件。 If your events are regular and of predictable duration, you may be able to make it just give you one. 如果您的活动是定期的并且持续时间可预测,那么您也许可以做到这一点。

events = client.events().list(calendarId='primary',
                              timeMin='2011-12-22T09:00:00Z',
                              timeMax='2011-12-22T22:00:00Z').execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用谷歌日历 api for python 取消事件甚至事件系列时,如何删除它们? - How can I delete events or even event series when they are cancelled using google calendar api for python? 当我使用谷歌日历 API Python 输入新事件时,如何自定义谷歌日历的事件 ID? - How can I custom my Event ID of Google Calendar when I input new event with Google Calendar API Python? 如何使用Google Python API编辑辅助日历 - How can I edit a secondary calendar using google python API 如何在谷歌日历 api 中检查事件是否完成 - How can I check if event is complete in google calendar api 如何使用Python和Google日历API创建Google日历活动 - How to create a Google calendar event with Python and Google calendar API 如何使用 Google 日历 API 将定期事件移动到 Python 中的其他日历? - How to move a recurring event to an other calendar in Python using Google Calendar API? 如何通过Python在Google日历中设置Invitee? - How can I set Invitee in Google Calendar through Python? 谷歌日历事件持续时间 Python - Google Calendar Event Duration Python Google Calendar API无法获取事件colorid-Python - Google calendar API can't get event colorid - Python 我可以使用谷歌日历生成报告 - Can i generate reporting using google calendar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM