获取每个域的日历计数的正确方法是什么?

时间:2014-03-26 07:19:56

标签: python google-app-engine google-calendar-api

我正在尝试使用Calendar Resources API获取每个域的日历资源数量,并且代码返回了被视为不良结果的内容。具体来说,代码坚持所有域都具有相同的日历计数。 我有两个函数可以做到这一点,都返回相同(坏)的结果:

def getCalendarCountFor(domain, userMail, password):
    client = CalendarResourceClient(domain=domain)
    client.ClientLogin(userMail, password, "test_app")
    calendar_resources = client.GetResourceFeed()
    return len(calendar_resources.entry)

第二个版本:

def GoogleQueryCalendars(dom, admin_id, admin_pwd):
    today = datetime.datetime.now().strftime("%Y-%m-%d %H:%S")
    calendarClient = CalendarResourceClient(domain=dom)
    calendarClient.ClientLogin(email=admin_id, password=admin_pwd, source='TheSource')
    resourceCount = 0
        # loop through all the calendar feeds
    try:
        moreCalendars =calendarClient.GetResourceFeed()
    except:
        print "Exception"
    calendars = {}
    while moreCalendars.entry is not None:
        for i, cal in enumerate(moreCalendars.entry):
            str = cal.GetResourceCommonName()
            pseudoDomain = re.sub("[^A-Z\d]", "", re.search("^[^-\s]*", str).group(0)).lower()
            if pseudoDomain in calendars:
                calendars[pseudoDomain] +=1
            else:
                calendars[pseudoDomain] =1
            resourceCount +=1
        try:
            moreCalendars = calendarClient.GetNext(moreCalendars)
        except:
            break
    return resourceCount

感谢。

1 个答案:

答案 0 :(得分:2)

以下是计算日历资源的方法。

def count_resources(domain, email, password):
    client = CalendarResourceClient(domain=domain)
    client.ClientLogin(email=email, 
                       password=password, 
                       source='TheSource')

    count = 0
    uri = client.MakeResourceFeedUri()
    while uri:
        feed = client.GetResourceFeed(uri)
        count += len(feed.entry)

        next_link = feed.GetNextLink()
        uri = next_link.href if next_link else None

    return count