【问题标题】:Retrieving a user's google calendar at the point in time在该时间点检索用户的谷歌日历
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      线路有问题

      service.events().list(calendarId=id, timeMin=utc_dt, maxResults=1, singleEvents=True).execute()

      maxResults=1 表示您访问的每个日历中最多列出 1 个事件!

      如果您想检索超过 1 个事件 - 跳过此参数。

      【讨论】:

      • 你是 100% 正确的.. 但具有讽刺意味的是,无论你没有得到每 maxResult 数量的事件。例如:当我执行 maxResult 1 时,我会以 JSON 形式返回 3 个事件。我最好的猜测是它单独计算“每个”日历而不是总数。因此,如果您有 3 个日历,则每个日历需要 1 个,而不是全部只需要 1 个。
      • 这是真的 - 每个日历都计算在内,但每个日历可能仍然有多个事件?!
      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 2019-09-08
      • 2020-11-04
      • 1970-01-01
      • 2021-03-31
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多