Google Calendar API如何查找与会者列表

时间:2018-03-12 09:05:21

标签: python google-calendar-api

我需要使用Google Calendar API查找与会者的与会者列表。要提取与会者电子邮件,我的代码如下:

eventsResult = service.events().list(calendarId='primary',timeMin=tmin,timeMax=tmax,singleEvents=True,orderBy='startTime').execute()
    events = eventsResult.get('items', [])

for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            end = event['end'].get('dateTime', event['end'].get('date'))
            attendees = event['attendees'].get('email', event['attendees'].get('email'))
            print(attendees)

但是收到错误

attendees = event['attendees'].get('email', event['attendees'].get('email'))
AttributeError: 'list' object has no attribute 'get'

我错过了什么?有关如何做的任何建议吗?

2 个答案:

答案 0 :(得分:1)

如果您正在寻找解决方案,那该怎么办? attendees的结构如下。

{
  "items": [
    {
      "attendees": [
        {
          "email": "### email ###",
          "displayName": "### name ###",
          "responseStatus": "#####"
        }
      ]
    }
  ]
}

那么这次修改怎么样?

来自:

for event in events:
            start = event['start'].get('dateTime', event['start'].get('date'))
            end = event['end'].get('dateTime', event['end'].get('date'))
            attendees = event['attendees'].get('email', event['attendees'].get('email'))
            print(attendees)

致:

for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    end = event['end'].get('dateTime', event['end'].get('date'))
    print("{0}, {1}".format(start, end))
    for attendee in event['attendees']:
        attendees = attendee.get('email', attendee.get('email'))
        print(attendees)

如果我误解了你的问题并且你已经有了解决方案,我很抱歉。

答案 1 :(得分:0)

希望此解决方案,此功能可以通过电子邮件或与会者的日历ID,与会者电子邮件的结果列表事件来获得与会者

def get_events_by_attendee(service, calendarId, attendee_email):
    now = datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
    timeMin = datetime.utcnow() - timedelta(days=3) #you can change
    timeMax = datetime.utcnow()+ timedelta(days=3)#you can change
    print(timeMin, now)
    timeMin = timeMin.isoformat() + 'Z'
    timeMax = timeMax.isoformat() + 'Z'
    print(timeMin)

    events_result = service.events().list(calendarId=calendarId,
                                          timeMin=timeMin,
                                          timeMax = timeMax,
                                          singleEvents=True,
                                          orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        print('No upcoming events found.')
    list_events = []
    for event1 in events:
        element_list = []
        eventId = event1['id']
        event = service.events().get(calendarId=calendarId, eventId=eventId).execute() #check this event have attendee or not
        try:

            if event['attendees']:
                for attendee in event['attendees']:
                    if (attendee['email']==attendee_email)and(attendee['responseStatus']=='accepted'): #responseStatus 'accept' mean attendee accept your invitation
                        print(event['attendees'])
                        element_list.append(eventId)
                        htmlLink = event['htmlLink']
                        element_list.append(htmlLink)
                        summary = event['summary']
                        element_list.append(summary)
                        try: #this code belong to my sytems, you can pass it
                            description = event['description']
                            description = description.replace('\n', '<br>')
                            description = description.split('####', 1)
                            if description == []:
                                description = event['description']
                        except Exception as e:
                            description = 'Không có mô tả####'
                            description = description.split('####', 1)
                        element_list.append(description)
                        created = event['created']
                        created = dateutil.parser.parse(created) + timedelta(hours=7) #change the timezone
                        created = datetime(created.year, created.month, created.day, created.hour, created.minute)
                        element_list.append(created)
                        updated = event['updated']
                        updated = dateutil.parser.parse(updated) + timedelta(hours=7)#change the timezone
                        updated = datetime(updated.year, updated.month, updated.day, updated.hour, updated.minute)
                        element_list.append(updated)
                        start = event['start'].get('dateTime', event['start'].get('date'))
                        start = dateutil.parser.parse(start) + timedelta(hours=7)#change the timezone
                        start = datetime(start.year, start.month, start.day, start.hour, start.minute)
                        element_list.append(start)
                        end = event['end'].get('dateTime', event['end'].get('date'))
                        end = dateutil.parser.parse(end) + timedelta(hours=7)
                        end = datetime(end.year, end.month, end.day, end.hour, end.minute)
                        element_list.append(end)
                        status = event['status']
                        element_list.append(status)
                        attendees = event['attendees']
                        element_list.append(attendees)
                        list_events.append(element_list)
                    else:
                        pass
        except Exception as e:
            pass
    return list_events