Skip to content

Commit

Permalink
Merge branch 'get_data_on_init' into apply_live_bonus
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmadden86 committed Oct 19, 2019
2 parents 45c52a5 + 2b87fbb commit 9121d30
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
38 changes: 22 additions & 16 deletions fpl/fpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import itertools
import os

import requests

from .constants import API_URLS
from .models.classic_league import ClassicLeague
from .models.fixture import Fixture
Expand All @@ -44,6 +46,14 @@ class FPL:

def __init__(self, session):
self.session = session
static = requests.get(API_URLS["static"]).json() # use synchronous request
for k, v in static.items():
try:
v = {w["id"]: w for w in v}
except (KeyError, TypeError):
pass
setattr(self, k, v)
setattr(self, "current_gameweek", next(event for event in static["events"] if event["is_current"])['id'])

async def get_user(self, user_id=None, return_json=False):
"""Returns the user with the given ``user_id``.
Expand Down Expand Up @@ -91,13 +101,13 @@ async def get_teams(self, team_ids=None, return_json=False):
:type return_json: bool
:rtype: list
"""
url = API_URLS["static"]
teams = await fetch(self.session, url)
teams = teams["teams"]
teams = getattr(self, "teams")

if team_ids:
team_ids = set(team_ids)
teams = [team for team in teams if team["id"] in team_ids]
teams = [team for team in teams.values() if team["id"] in team_ids]
else:
teams = [team for team in teams.values()]

if return_json:
return teams
Expand Down Expand Up @@ -145,9 +155,8 @@ async def get_team(self, team_id, return_json=False):
"""
assert 0 < int(
team_id) < 21, "Team ID must be a number between 1 and 20."
url = API_URLS["static"]
teams = await fetch(self.session, url)
team = next(team for team in teams["teams"]
teams = getattr(self, "teams")
team = next(team for team in teams.values()
if team["id"] == int(team_id))

if return_json:
Expand Down Expand Up @@ -226,11 +235,10 @@ async def get_player(self, player_id, players=None, include_summary=False,
:raises ValueError: Player with ``player_id`` not found
"""
if not players:
players = await fetch(self.session, API_URLS["static"])
players = players["elements"]
players = getattr(self, "elements")

try:
player = next(player for player in players
player = next(player for player in players.values()
if player["id"] == player_id)
except StopIteration:
raise ValueError(f"Player with ID {player_id} not found")
Expand Down Expand Up @@ -263,11 +271,10 @@ async def get_players(self, player_ids=None, include_summary=False,
:type return_json: bool
:rtype: list
"""
players = await fetch(self.session, API_URLS["static"])
players = players["elements"]
players = getattr(self, "elements")

if not player_ids:
player_ids = [player["id"] for player in players]
player_ids = [player["id"] for player in players.values()]

tasks = [asyncio.ensure_future(
self.get_player(
Expand Down Expand Up @@ -422,11 +429,10 @@ async def get_gameweek(self, gameweek_id, include_live=False,
:rtype: :class:`Gameweek` or ``dict``
"""

static_gameweeks = await fetch(self.session, API_URLS["static"])
static_gameweeks = static_gameweeks["events"]
static_gameweeks = getattr(self, "events")

try:
static_gameweek = next(gameweek for gameweek in static_gameweeks if
static_gameweek = next(gameweek for gameweek in static_gameweeks.values() if
gameweek["id"] == gameweek_id)
except StopIteration:
raise ValueError(f"Gameweek with ID {gameweek_id} not found")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"pytest-aiohttp",
"pytest-cov",
"pytest-mock",
"pytest",
"pytest", 'requests'
],
entry_points="""
[console_scripts]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_fpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ async def test_init(self, loop):
session = aiohttp.ClientSession()
fpl = FPL(session)
assert fpl.session is session
keys = [
"events",
"game_settings",
"phases",
"teams",
"elements",
"element_types",
"element_stats",
"total_players",
"current_gameweek",
]
assert all([hasattr(fpl, key) for key in keys])
assert all([isinstance(getattr(fpl, key), dict) for key in keys[:-3]])
assert isinstance(getattr(fpl, keys[-3]), list)
assert all([isinstance(getattr(fpl, key), int) for key in keys[-2:]])
await session.close()

async def test_user(self, loop, fpl):
Expand Down

0 comments on commit 9121d30

Please sign in to comment.