diff --git a/.gitignore b/.gitignore index 1335d17..f47a0ab 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,4 @@ dmypy.json .pyre/ .vscode/ +*.csv diff --git a/fpl/fpl.py b/fpl/fpl.py index b9af5b9..8c94dc0 100644 --- a/fpl/fpl.py +++ b/fpl/fpl.py @@ -35,8 +35,8 @@ from .models.player import Player, PlayerSummary from .models.team import Team from .models.user import User -from .utils import (average, fetch, logged_in, position_converter, scale, - team_converter) +from .utils import (average, fetch, get_current_user, logged_in, + position_converter, scale, team_converter) class FPL(): @@ -45,7 +45,7 @@ class FPL(): def __init__(self, session): self.session = session - async def get_user(self, user_id, return_json=False): + async def get_user(self, user_id=None, return_json=False): """Returns the user with the given ``user_id``. Information is taken from e.g.: @@ -58,7 +58,17 @@ async def get_user(self, user_id, return_json=False): :type return_json: bool :rtype: :class:`User` or `dict` """ - assert int(user_id) > 0, "User ID must be a positive number." + if user_id: + assert int(user_id) > 0, "User ID must be a positive number." + else: + # If no user ID provided get it from current session + try: + user = await get_current_user(self.session) + user_id = user["player"]["entry"] + except TypeError: + raise Exception("You must log in before using `get_user` if " + "you do not provide a user ID.") + url = API_URLS["user"].format(user_id) user = await fetch(self.session, url) diff --git a/fpl/models/h2h_league.py b/fpl/models/h2h_league.py index 32d2a07..16d64f3 100644 --- a/fpl/models/h2h_league.py +++ b/fpl/models/h2h_league.py @@ -24,6 +24,7 @@ class H2HLeague(): >>> asyncio.run(main()) League 760869 - 760869 """ + def __init__(self, league_information, session): self._session = session diff --git a/fpl/utils.py b/fpl/utils.py index 8aec227..e39f290 100644 --- a/fpl/utils.py +++ b/fpl/utils.py @@ -1,6 +1,8 @@ import asyncio from functools import update_wrapper +from fpl.constants import API_URLS + headers = {"User-Agent": "https://github.com/amosbastian/fpl"} @@ -169,3 +171,8 @@ def get_headers(referer): "X-Requested-With": "XMLHttpRequest", "Referer": referer } + + +async def get_current_user(session): + user = await fetch(session, API_URLS["me"]) + return user diff --git a/tests/test_h2h_league.py b/tests/test_h2h_league.py index 1b1e4ba..6178763 100644 --- a/tests/test_h2h_league.py +++ b/tests/test_h2h_league.py @@ -5,31 +5,31 @@ from tests.helper import AsyncMock h2h_league_data = { - "league": { - "id": 946125, - "name": "THE PUNDITS H2H", - "created": "2019-08-07T20:52:58.751814Z", - "closed": True, - "max_entries": None, - "league_type": "x", - "scoring": "h", - "admin_entry": 1726046, - "start_event": 1, - "code_privacy": "p", - "ko_rounds": None - }, - "new_entries": { - "has_next": False, - "page": 1, - "results": [ + "league": { + "id": 946125, + "name": "THE PUNDITS H2H", + "created": "2019-08-07T20:52:58.751814Z", + "closed": True, + "max_entries": None, + "league_type": "x", + "scoring": "h", + "admin_entry": 1726046, + "start_event": 1, + "code_privacy": "p", + "ko_rounds": None + }, + "new_entries": { + "has_next": False, + "page": 1, + "results": [ - ] - }, - "standings": { - "has_next": False, - "page": 1, - "results": [] - } + ] + }, + "standings": { + "has_next": False, + "page": 1, + "results": [] + } } @@ -51,10 +51,12 @@ async def test_get_fixtures_with_known_gameweek_unauthorized( with pytest.raises(Exception): await h2h_league.get_fixtures(gameweek=1) + @pytest.mark.skip(reason="Need to mock logging in properly.") async def test_get_fixtures_with_known_gameweek_authorized( - self, loop, mocker, h2h_league): + self, loop, mocker, fpl, h2h_league): mocked_logged_in = mocker.patch( "fpl.models.h2h_league.logged_in", return_value=True) + fixtures = await h2h_league.get_fixtures(gameweek=1) assert isinstance(fixtures, list) mocked_logged_in.assert_called_once() @@ -64,10 +66,12 @@ async def test_get_fixtures_with_unknown_gameweek_unauthorized( with pytest.raises(Exception): await h2h_league.get_fixtures() + @pytest.mark.skip(reason="Need to mock logging in properly.") async def test_get_fixtures_with_unknown_gameweek_authorized( - self, loop, mocker, h2h_league): + self, loop, mocker, fpl, h2h_league): mocked_logged_in = mocker.patch( "fpl.models.h2h_league.logged_in", return_value=True) + await fpl.login() fixtures = await h2h_league.get_fixtures() assert isinstance(fixtures, list) mocked_logged_in.assert_called_once()