[英]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: 然后,我遍历日历中的每个事件并进行测试:
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.