代码之家  ›  专栏  ›  技术社区  ›  nad

Google日历API如何查找与会者列表

  •  1
  • nad  · 技术社区  · 6 年前

    我需要使用Google日历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 回复  |  直到 6 年前
        1
  •  2
  •   Tanaike    6 年前

    如果您正在寻找解决方案,这个怎么样?结构 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)
    

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

        2
  •  0
  •   Linh Nguyễn Ngọc    4 年前

    我希望此I解决方案、此功能可以通过电子邮件或与会者的日历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