def updateSummoner(epochTime):
    # get all summoners which haven't been checked within
    # 5 + ((levels away from 30) * 5) days
    # if the summoner was inactive for some time
    # instead only check every 1/4th of the time he was inactive
    # (e.g inactive for 4 months = check after a month)
    query = "SELECT * FROM summoner WHERE revisionDate > ({epochTime} - (({epochTime} - revisionDate) / 4)) and lastSummonerCheck <= NOW() - INTERVAL 5 + (ABS(summonerLevel - 30)*5) DAY".format(epochTime=epochTime)
    result = db.selectFetchAll(query)

    summonerIds = []
    # for each summoner we have to check
    for row in result:
        # add summonerId to the array
        # so we can check as many Ids with as little calls as possible
        # (40 at a time)
        summonerIds.append(row[1])
        if len(summonerIds) >= 40:
            # get json object from the Riot API
            result = api.getSummonerByIDs(summonerIds, "euw")
            # if the return value is a dictionary
            # and not a 404 or some other error code
            if type(result) is dict:
                # update the summoner entries in the database
                db.updateSummoner(result)
            summonerIds = []
    # check the remaining Ids after the loop
    if len(summonerIds) > 0:
        result = api.getSummonerByIDs(summonerIds, "euw")
        if type(result) is dict:
            db.updateSummoner(result)
def getNewSummoner():
    error = 0

    # get the currently highest summonerId from the database
    summonerIDPointer = db.selectFetchOne("SELECT summonerId FROM summoner ORDER BY summonerId DESC")
    if summonerIDPointer is None:
        summonerIDPointer = 0
    else:
        summonerIDPointer = summonerIDPointer[0] + 1
    print(summonerIDPointer)

    while True:
        # check the next 40 ids
        summonerIDs = []
        for i in range(summonerIDPointer, summonerIDPointer + 39):
            summonerIDs.append(i)
        response = api.getSummonerByIDs(summonerIDs, "euw")
        if type(response) is dict:
            error = 0
            db.insertSummoner(response)
        else:
            error += 1

        # if there are more than 400 empty ids it's fair to assume this was the last one
        if error >= 20:
            print("50 Errors in a row... fairly sure that's all for now.")
            break

        summonerIDPointer += 40