async def update_players(): """Updates all players in the database.""" logger.info("Updating FPL players in database.") async with aiohttp.ClientSession() as session: fpl = FPL(session) players = await fpl.get_players(include_summary=True, return_json=True) for player in players: player["team"] = team_converter(player["team"]) requests = [ ReplaceOne({"id": player["id"]}, player, upsert=True) for player in players ] database.players.bulk_write(requests) create_text_indexes() logger.info("Adding Understat data to players in database.") understat_players = await get_understat_players() for player in understat_players: # Only update FPL player with desired attributes understat_attributes = { attribute: value for attribute, value in player.items() if attribute in desired_attributes } # Use player's full name and team to try and find the correct player search_string = f"{player['player_name']} {player['team_title']}" players = database.players.find({ "$text": { "$search": search_string } }, { "score": { "$meta": "textScore" } }).sort([("score", { "$meta": "textScore" })]) try: relevant_player = list(players)[0] except IndexError: continue database.players.update_one({"id": relevant_player["id"]}, {"$set": understat_attributes})
async def main(): with open("E_average_fixture_difficulties.json", "r") as f: fixture_data = json.load(f) async with aiohttp.ClientSession() as session: fpl = FPL(session) players = await fpl.get_players() player_dict = dict() for x in players: id = x.id team_code = x.team team_name = team_converter(team_code) pos = position_converter(x.element_type) name = x.web_name cost = x.now_cost / 10 points = x.total_points minutes = x.minutes pp90 = round(x.pp90, 2) played = await x.games_played fixtures_remaining = fixture_data[team_name]["num_fixtures"] average_difficulty = fixture_data[team_name]["difficulties"][pos] player_dict[id] = { "team_name": team_name, "team_code": team_code, "pos": pos, "name": name, "cost": cost, "points": points, "minutes": minutes, "points_per_90": pp90, "games_played": played, "fixtures_remaining": fixtures_remaining, "average_difficulty": average_difficulty } with open("G_players_data.json", "w", encoding="utf-8") as f: json.dump(player_dict, f, indent=4, ensure_ascii=False)
async def test_team_converter(self, loop, fpl): teams = await fpl.get_teams() for team in teams: assert team_converter(team.id) == team.name