Python 检索用户';谷歌日历的时间点

Python 检索用户';谷歌日历的时间点,python,flask,google-api,google-calendar-api,Python,Flask,Google Api,Google Calendar Api,我试图检索用户当前的谷歌日历事件(即在时间点)。正如您所见,我必须在用户的每个谷歌日历中进行嵌套循环。然而,我并不总是得到正确的事件。我不确定我犯了什么错误,也不确定是否有更好/更简洁的方法 utc_dt = datetime.now(timezone.utc).astimezone().isoformat() service = build('calendar', 'v3', credentials=credentials) events_result =service

我试图检索用户当前的谷歌日历事件(即在时间点)。正如您所见,我必须在用户的每个谷歌日历中进行嵌套循环。然而,我并不总是得到正确的事件。我不确定我犯了什么错误,也不确定是否有更好/更简洁的方法

    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}
utc\u dt=datetime.now(timezone.utc).astimezone().isoformat()
服务=生成('calendar','v3',凭据=凭据)
events_result=service.calendarList().list().execute()
events=events\u result.get('items',[])
Ids=[item['id']for item in events]#获取更多颜色,因为事件占用的时间与AlCals占用的时间不同
进口皮茨
utc=pytz.utc
AllCals=[service.events().list(calendarId=id,timeMin=utc\u dt,maxResults=1,singleEvents=True)。针对id中的id执行()
calEvent=[]
now=datetime.now()
对于所有日历:
对于日历中的项目[“项目”]:
如果项目[“开始”]和项目[“结束”]中的“日期时间”:
如果datetime.strTime(项目[“开始”][“日期时间”],%Y-%m-%dT%H:%m:%S%z”):
calEvent.append(项目)
now=datetime.now(pytz.utc)
name=max(如果datetime.strtime(dt[“开始”][“日期时间”],%Y-%m-%dt%H:%m:%S%z))
线路有问题

service.events()

maxResults=1
意味着在您访问的每个日历中,最多列出一个事件


如果要检索多个事件,请跳过此参数。

行中有问题

events_result = service.events().list(calendarId=id, timeMin=utc_dt,
                                              maxResults=10, singleEvents=True,
                                              orderBy='startTime').execute()
service.events()

maxResults=1
意味着在您访问的每个日历中,最多列出一个事件


如果要检索多个事件,请跳过此参数。

必须说明获取事件结果的顺序,否则将重新运行随机事件。添加参数
orderBy='startTime'
,如果要从开始日期获取事件数,请在
maxResults
中传递该数字,否则删除
maxResults
参数

events_result = service.events().list(calendarId=id, timeMin=utc_dt,
                                              maxResults=10, singleEvents=True,
                                              orderBy='startTime').execute()

您必须提及获取事件结果的顺序,否则它将重新运行随机事件。添加参数
orderBy='startTime'
,如果要从开始日期获取事件数,请在
maxResults
中传递该数字,否则删除
maxResults
参数

events_result = service.events().list(calendarId=id, timeMin=utc_dt,
                                              maxResults=10, singleEvents=True,
                                              orderBy='startTime').execute()

你说得100%对。。但具有讽刺意味的是,无论什么,您都无法按
maxResult
的数量返回事件。例如:当我执行
maxResult
1时,我得到3个JSON事件。我最好的猜测是,它计算“每个”日历本身,而不是总数。因此,如果你有3个日历,它将从每个日历中抽取1个,而不是从所有日历中抽取1个。这是真的-每个日历计数,但每个日历可能仍有多个事件?!你说得100%对。。但具有讽刺意味的是,无论什么,您都无法按
maxResult
的数量返回事件。例如:当我执行
maxResult
1时,我得到3个JSON事件。我最好的猜测是,它计算“每个”日历本身,而不是总数。因此,如果你有3个日历,它将从每个日历中抽取1个,而不是从所有日历中抽取1个。这是真的-每个日历计数,但每个日历可能仍有多个事件?!