How to use the fpl.constants.API_URLS function in fpl

To help you get started, we’ve selected a few fpl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github amosbastian / fpl / fpl / models / team.py View on Github external
async def get_players(self, return_json=False):
        """Returns a list containing the players who play for the team. Does
        not include the player's summary.

        :param return_json: (optional) Boolean. If ``True`` returns a list of
            dicts, if ``False`` returns a list of Player objects. Defaults to
            ``False``.
        :type return_json: bool
        :rtype: list
        """
        team_players = getattr(self, "players", [])

        if not team_players:
            players = await fetch(self._session, API_URLS["static"])
            players = players["elements"]
            team_players = [player for player in players
                            if player["team"] == self.id]
            self.players = team_players

        if return_json:
            return team_players

        return [Player(player, self._session) for player in team_players]
github amosbastian / fpl / fpl / models / h2h_league.py View on Github external
:rtype: list
        """
        if not self._session:
            return []

        if not logged_in(self._session):
            raise Exception(
                "Not authorised to get H2H fixtures. Log in first.")

        url_query = f"event={gameweek}&" if gameweek else ""
        has_next = True
        results = []

        while has_next:
            fixtures = await fetch(
                self._session, API_URLS["league_h2h_fixtures"].format(
                    self.league["id"], url_query, page))
            results.extend(fixtures["results"])

            has_next = fixtures["has_next"]
            page += 1

        return results
github amosbastian / fpl / fpl / models / classic_league.py View on Github external
async def get_standings(self, page):
        """Returns the league's standings of the given page.

        Information is taken from e.g.:
            https://fantasy.premierleague.com/api/leagues-classic/967/standings/?page_new_entries=1&page_standings=1&phase=1

        :param page: A page of the league's standings (default is 50 managers
            per page).
        :type page: string or int
        :rtype: dict
        """
        url = "{}?ls-page={}".format(
                API_URLS["league_classic"].format(self.league["id"]), page)
        standings = await fetch(self._session, url)
        return standings["standings"]
github amosbastian / fpl / fpl / models / player.py View on Github external
async def games_played(self):
        """The number of games where the player has played at least 1 minute.

        :rtype: int
        """
        if hasattr(self, "history"):
            fixtures = self.history
        else:
            player_summary = await fetch(
                self._session, API_URLS["player"].format(self.id))
            fixtures = player_summary["history"]

        return sum([1 for fixture in fixtures if fixture["minutes"] > 0])
github amosbastian / fpl / fpl / utils.py View on Github external
async def get_current_user(session):
    user = await fetch(session, API_URLS["me"])
    return user
github amosbastian / fpl / fpl / fpl.py View on Github external
:type return_json: bool
        :rtype: :class:`Fixture` or ``dict``
        :raises ValueError: if fixture with ``fixture_id`` not found
        """
        fixtures = await fetch(self.session, API_URLS["fixtures"])

        try:
            fixture = next(fixture for fixture in fixtures
                           if fixture["id"] == fixture_id)
        except StopIteration:
            raise ValueError(f"Fixture with ID {fixture_id} not found")
        fixture_gameweek = fixture["event"]

        gameweek_fixtures = await fetch(
            self.session,
            API_URLS["gameweek_fixtures"].format(fixture_gameweek))

        try:
            fixture = next(fixture for fixture in gameweek_fixtures
                           if fixture["id"] == fixture_id)
        except StopIteration:
            raise ValueError(
                f"Fixture with ID {fixture_id} not found in gameweek fixtures")

        if return_json:
            return fixture

        return Fixture(fixture)
github amosbastian / fpl / fpl / models / user.py View on Github external
async def get_automatic_substitutions(self, gameweek=None):
        """Returns a list containing the user's automatic substitutions each
        gameweek.

        Information is taken from e.g.:
            https://fantasy.premierleague.com/api/entry/91928/event/1/picks/

        :param gameweek: (optional): The gameweek. Defaults to ``None``.
        :rtype: list
        """
        if hasattr(self, "_picks"):
            picks = self._picks
        else:
            tasks = [asyncio.ensure_future(
                     fetch(self._session,
                           API_URLS["user_picks"].format(self.id, gameweek)))
                     for gameweek in range(1, self.current_event + 1)]
            picks = await asyncio.gather(*tasks)
            self._picks = picks

        if gameweek is not None:
            valid_gameweek(gameweek)
            try:
                return next(pick["automatic_subs"] for pick in picks
                            if pick["entry_history"]["event"] == gameweek)
            except StopIteration:
                return None

        return [p for pick in picks for p in pick["automatic_subs"]]