async def export(request): db = request.app["db"] profileId = request.match_info.get("profileId") # Who made the request? session = await aiohttp_session.get_session(request) session_user = session.get("user_name") game_list = [] game_counter = 0 failed = 0 if profileId is not None: if profileId == "all_games" and session_user in request.app[ "fishnet_versions"]: cursor = db.game.find() else: cursor = db.game.find({"us": profileId}) async for doc in cursor: try: # print(game_counter) # log.info("%s %s %s" % (doc["d"].strftime("%Y.%m.%d"), doc["_id"], C2V[doc["v"]])) pgn_text = pgn(doc) if pgn_text is not None: game_list.append(pgn_text) game_counter += 1 except Exception: failed += 1 log.error( "Failed to load game %s %s %s (early games may contain invalid moves)", doc["_id"], C2V[doc["v"]], doc["d"].strftime("%Y.%m.%d")) continue print('failed/all:', failed, game_counter) pgn_text = "\n".join(game_list) return web.Response(text=pgn_text, content_type="text/pgn")
async def main(): client = ma.AsyncIOMotorClient(MONGO_HOST) db = client[MONGO_DB_NAME] for month in MONTHS: yearmonth = "%s%02d" % (YEAR, month) game_list = [] game_counter = 0 failed = 0 cursor = None print("---", yearmonth[:4], yearmonth[4:]) filter_cond = { "$and": [ { "$expr": { "$eq": [{ "$year": "$d" }, int(yearmonth[:4])] } }, { "$expr": { "$eq": [{ "$month": "$d" }, int(yearmonth[4:])] } }, ] } cursor = db.game.find(filter_cond) if cursor is not None: async for doc in cursor: try: # print(game_counter) # log.info("%s %s %s" % (doc["d"].strftime("%Y.%m.%d"), doc["_id"], C2V[doc["v"]])) pgn_text = pgn(doc) if pgn_text is not None: game_list.append(pgn_text) game_counter += 1 except Exception: failed += 1 log.error( "Failed to load game %s %s %s (early games may contain invalid moves)", doc["_id"], C2V[doc["v"]], doc["d"].strftime("%Y.%m.%d"), ) continue print("failed/all:", failed, game_counter) pgn_text = "\n".join(game_list) with bz2.open(yearmonth + ".pgn", "wt") as f: f.write(pgn_text)
async def export(request): db = request.app["db"] users = request.app["users"] profileId = request.match_info.get("profileId") if profileId is not None and profileId not in users: await asyncio.sleep(3) return web.Response(text="") tournamentId = request.match_info.get("tournamentId") # Who made the request? session = await aiohttp_session.get_session(request) session_user = session.get("user_name") game_list = [] game_counter = 0 failed = 0 cursor = None if profileId is not None: cursor = db.game.find({"us": profileId}) elif tournamentId is not None: cursor = db.game.find({"tid": tournamentId}) elif session_user in ADMINS: yearmonth = request.match_info.get("yearmonth") print("---", yearmonth[:4], yearmonth[4:]) filter_cond = { "$and": [ {"$expr": {"$eq": [{"$year": "$d"}, int(yearmonth[:4])]}}, {"$expr": {"$eq": [{"$month": "$d"}, int(yearmonth[4:])]}}, ] } cursor = db.game.find(filter_cond) if cursor is not None: async for doc in cursor: try: # print(game_counter) # log.info("%s %s %s" % (doc["d"].strftime("%Y.%m.%d"), doc["_id"], C2V[doc["v"]])) pgn_text = pgn(doc) if pgn_text is not None: game_list.append(pgn_text) game_counter += 1 except Exception: failed += 1 log.error( "Failed to load game %s %s %s (early games may contain invalid moves)", doc["_id"], C2V[doc["v"]], doc["d"].strftime("%Y.%m.%d"), ) continue print("failed/all:", failed, game_counter) pgn_text = "\n".join(game_list) return web.Response(text=pgn_text, content_type="text/pgn")
async def export(request): db = request.app["db"] profileId = request.match_info.get("profileId") # Who made the request? session = await aiohttp_session.get_session(request) session_user = session.get("user_name") game_list = [] if profileId is not None: cursor = db.game.find({"us": profileId}) async for doc in cursor: # filter out private games if "p" in doc and doc["p"] == 1 and session_user != doc["us"][ 0] and session_user != doc["us"][1]: continue try: game_list.append(pgn(doc)) except Exception: log.error("Failed to load game %s %s" % (doc["_id"], C2V[doc["v"]])) pgn_text = "\n".join(game_list) return web.Response(text=pgn_text, content_type="text/pgn")