def store_players(r): FIELDS = [ "id", "code", "first_name", "second_name", "web_name", "team", "team_code", "photo", "element_type", "now_cost", "status", "news", "news_added", ] field_getter = itemgetter(*FIELDS) player_data = {} players = iter(r.json()["elements"]) for player in players: data = {key: value for key, value in zip(FIELDS, field_getter(player))} player_data[data.get("id")] = data logger.info(f'Player - {data.get("web_name")}') redis.set(PLAYER_ID_KEY(data.get("web_name"), data.get("team_code")), data.get("id")) redis.set(PLAYER_DATA_KEY, json.dumps(player_data))
def cache_ml_player_count(*args, **kwargs): for ml in Marblelympics.objects.all(): print('CACHE ML PLAYER COUNT - ', ml) count = ml.players_participating.count() redis.set( ml.cache_key, count, ex=constants.PLAYER_COUNT_CACHE_DURATION )
def cache_event_player_count(*args, **kwargs): for event in Event.objects.all(): print('CACHE EVENT PLAYER COUNT - ', event) count = event.player_entries.count() redis.set( event.cache_key, count, ex=constants.EVENT_ENTRY_COUNT_CACHE_DURATION )
def player_count(self): count = redis.get(self.cache_key) if not count: count = self.players_participating.count() redis.set(self.cache_key, count, ex=constants.PLAYER_COUNT_CACHE_DURATION) return count
def entry_count(self): count = redis.get(self.cache_key) if not count: count = self.player_entries.count() redis.set(self.cache_key, count, ex=constants.EVENT_ENTRY_COUNT_CACHE_DURATION) return count
def store_teams(r): FIELDS = ["id", "code", "name", "short_name", "strength"] field_getter = itemgetter(*FIELDS) team_data = {} teams = iter(r.json()["teams"]) for team in teams: data = {key: value for key, value in zip(FIELDS, field_getter(team))} team_data[data.get("id")] = data logger.info(f'Team - {data.get("name")}') redis.set(TEAM_DATA_KEY, json.dumps(team_data))
def store_gameweeks(r): FIELDS = ["id", "name", "deadline_time", "finished"] field_getter = itemgetter(*FIELDS) gameweek_data = {} gameweeks = iter(r.json()["events"]) for gameweek in gameweeks: data = { key: value for key, value in zip(FIELDS, field_getter(gameweek)) } gameweek_data[data.get("id")] = data logger.info(f'Gameweek - {data.get("name")}') redis.set(GAMEWEEK_DATA_KEY, json.dumps(gameweek_data)) configure_gameweek_updates(gameweek_data)
def fetch_fixtures(start=1, end=38): logger.info("fetch_fixtures") URL = "https://fantasy.premierleague.com/api/fixtures/" teams = get_team_data() fixtures = get_team_fixtures_data() gameweek_fixtures = get_gameweek_fixtures_data() for gw in range(start, end + 1): gw = str(gw) logger.info(f"Fixtures GW#{gw}") gameweek_fixtures[gw] = [] for team in teams.keys(): fixtures[team][gw] = [] r = requests.get(URL, params={"event": gw}) data = r.json() for match in data: event, team_a, team_h, kickoff_time = ( match.get("event"), match.get("team_a"), match.get("team_h"), match.get("kickoff_time"), ) fixtures[str(team_h)][str(event)].append({ "opponent": team_a, "location": "H", "gw": event }) fixtures[str(team_a)][str(event)].append({ "opponent": team_h, "location": "A", "gw": event }) gameweek_fixtures[str(event)].append({ "home": team_h, "away": team_a, "kickoff_time": kickoff_time }) redis.set(TEAM_FIXTURES_DATA_KEY, json.dumps(fixtures)) redis.set(GAMEWEEK_FIXTURES_DATA_KEY, json.dumps(gameweek_fixtures))
def update_gameweek(gameweek): Gameweek.objects.all().update(active=False) selected_gameweek = Gameweek.objects.filter(gw_id=gameweek) if selected_gameweek: selected_gameweek.update(active=True) redis.set(CURRENT_GAMEWEEK_KEY, gameweek)