Example #1
0
def recursiveTestGenerator(
    username, password, testInterval, totalCount, startList):
    """A recursive test generator.
   
    Start from a list of user id, and get all the profile of these id and
    their friends and friend of the friends.
    Every time it gets a user profile, it will yield the
    (id, UserInfo, ErrorCode)
    
    Args:
        @username {string} the user name of the agent.
        @password {string} the password of the agent.
        @testInterval {float} the interval time between every request.
        @totalCount {integer} total number of profile to get.
        @startList {List} a list of user id to start test.
    """
    agent = RenrenAgent(username, password)
    info, error = agent.login()
    if not error:
        log.info(info['name'])
        log.info(info['href'])
    else:
        log.error('Login error(username, password): ' +\
                username + ', ' + password)
    count = 1
    visitList = []
    for elem in startList:
        visitList.append((elem, None))
    while visitList:
        # Get the element to do requet.
        elem = visitList[0]
        id = elem[0]
        log.info('processing(' + str(count) + '): ' + id)
        visitList = visitList[1:]
        info, errorCode = agent.getProfile(id)
        # Error handle
        if errorCode:
            if elem[1]:
                log.warning('Error happen when getProfile. Refer id: ' +\
                            str(elem[1]) + '. Refer page url: ' +\
                            agent.getProfileUrl(str(elem[1])))
            else:
                log.warning('Error happen when getProfile, no refer id.')
            continue
        # Yield result
        yield (id, info, errorCode)
        # Result handle
        if len(visitList) < totalCount - count:
            newList = []
            if info.friendList:
                for ele in info.friendList:
                    newList += [(ele, id)]
            if info.recentVisitedList:
                for ele in info.recentVisitedList:
                    newList += [(ele, id)]
            visitList += newList
        # Acc
        count += 1
        if count > totalCount:
            return
        time.sleep(testInterval)