def runUpdate(monthStart, yearStart, monthEnd, yearEnd): curMonth = monthStart curYear = yearStart updateList = [] while (curYear < yearEnd or (curYear == yearEnd and curMonth <= monthEnd)): for card in cards: for test in tests: search = genSearchString(card, test, curYear, curMonth) results = driveService.files().list( includeTeamDriveItems=True, supportsTeamDrives=True, pageSize=100, q=search, corpora="domain").execute() files = results["files"] for file in files: datetime = getEpochTime(file["name"]) curSheet = Sheet(test, card, datetime, file["id"]) updateList = updateList + curSheet.genUpdate(sheetService) time.sleep(1.3) curYear = curYear + 1 if curMonth == 12 else curYear curMonth = (curMonth % 12) + 1 query = [] for update in updateList: query.append( UpdateOne( { "sheetId": update["sheetId"], "subtest": update["subtest"], "test": update["test"], "type": update["type"] }, {"$set": update}, upsert=True)) collection.bulk_write(query)