【发布时间】:2021-01-03 02:47:57
【问题描述】:
我正在尝试检索用户当前的谷歌日历事件(即在时间点)。如您所见,我必须创建一个嵌套循环来遍历用户的每个 google 日历。然而,我并不总是得到正确的事件。我不确定我的错误是什么,也不确定是否有更好/更简洁的方法。
utc_dt = datetime.now(timezone.utc).astimezone().isoformat()
service = build('calendar', 'v3', credentials=credentials)
events_result =service.calendarList().list().execute()
events = events_result.get('items', [])
Ids = [item['id'] for item in events] #getting more colors because events is not taking the time AlCals is taking
import pytz
utc=pytz.UTC
AllCals = [service.events().list(calendarId=id, timeMin=utc_dt, maxResults=1, singleEvents=True).execute() for id in Ids ]
calEvent = []
now = datetime.now()
for calendar in AllCals:
for item in calendar["items"]:
if "dateTime" in item["start"] and item["end"]:
if datetime.strptime(item["start"]["dateTime"], "%Y-%m-%dT%H:%M:%S%z"):
calEvent.append(item)
now = datetime.now(pytz.utc)
name = max(dt['summary'] for dt in calEvent if datetime.strptime(dt["start"]["dateTime"], "%Y-%m-%dT%H:%M:%S%z") < now)
return {'current event':name}
【问题讨论】:
标签: python flask google-api google-calendar-api