Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if not set(players_in).isdisjoint(players_out):
raise Exception("Player ID can't be in both lists.")
user_team = await self.get_team()
team_ids = [player["element"] for player in user_team]
if not set(team_ids).isdisjoint(players_in):
raise Exception(
"Cannot transfer a player in who is already in the user's team.")
if set(team_ids).isdisjoint(players_out):
raise Exception(
"Cannot transfer a player out who is not in the user's team.")
players = await fetch(self._session, API_URLS["players"])
player_ids = [player["id"] for player in players]
if set(player_ids).isdisjoint(players_in):
raise Exception("Player ID in `players_in` does not exist.")
# Send POST requests with `confirmed` set to False; this basically
# checks if there are any errors from FPL's side for this transfer,
# e.g. too many players from the same team, or not enough money.
payload = self._get_transfer_payload(
players_out, players_in, user_team, players, wildcard, free_hit)
headers = get_headers(
"https://fantasy.premierleague.com/a/squad/transfers")
post_response = await post(
self._session, API_URLS["transfers"], json.dumps(payload), headers)
if "non_form_errors" in post_response:
Information is taken from e.g.:
https://fantasy.premierleague.com/api/fixtures/
https://fantasy.premierleague.com/api/fixtures/?event=1
:param list fixture_ids: A list of fixture IDs.
:param return_json: (optional) Boolean. If ``True`` returns a list of
``dict``s, if ``False`` returns a list of :class:`Fixture`
objects. Defaults to ``False``.
:type return_json: bool
:rtype: list
"""
if not fixture_ids:
return []
fixtures = await fetch(self.session, API_URLS["fixtures"])
fixture_gameweeks = set(fixture["event"] for fixture in fixtures
if fixture["id"] in fixture_ids)
tasks = [asyncio.ensure_future(
fetch(self.session,
API_URLS["gameweek_fixtures"].format(gameweek)))
for gameweek in fixture_gameweeks]
gameweek_fixtures = await asyncio.gather(*tasks)
merged_fixtures = list(itertools.chain(*gameweek_fixtures))
fixtures = [fixture for fixture in merged_fixtures
if fixture["id"] in fixture_ids]
if return_json:
return fixtures
dicts, if ``False`` returns a list of TeamFixture objects.
Defaults to ``False``.
:type return_json: bool
:rtype: list
"""
fixtures = getattr(self, "fixtures", [])
if fixtures:
return fixtures
players = getattr(self, "players", [])
if not players:
await self.get_players()
player = self.players[0]
url = API_URLS["player"].format(player["id"])
player_summary = await fetch(self._session, url)
self.fixtures = player_summary["fixtures"]
if return_json:
return self.fixtures
# TODO: create TeamFixture
return self.fixtures
async def get_chips_history(self, gameweek=None):
"""Returns a list containing the chip history of the user.
Information is taken from e.g.:
https://fantasy.premierleague.com/api/entry/91928/history
:param gameweek: (optional): The gameweek. Defaults to ``None``.
:rtype: list
"""
if hasattr(self, "_history"):
history = self._history
else:
history = await fetch(
self._session, API_URLS["user_history"].format(self.id))
self._history = history
if gameweek is not None:
valid_gameweek(gameweek)
try:
return next(chip for chip in history["chips"]
if chip["event"] == gameweek)
except StopIteration:
return None
return history["chips"]
async def get_user_history(self, gameweek=None):
"""Returns a list containing the user's history for each gameweek,
or a dictionary of the user's history for the given gameweek.
:rtype: list or dict
"""
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(self.started_event,
self.current_event + 1)]
picks = await asyncio.gather(*tasks)
self._picks = picks
if gameweek is not None:
valid_gameweek(gameweek)
try:
return next(pick["entry_history"] for pick in picks
if pick["entry_history"]["event"] == gameweek)
except StopIteration:
return None
return [history["entry_history"] for history in picks]
Information is taken from e.g.:
https://fantasy.premierleague.com/api/leagues-h2h-matches/league/946125/
:param league_id: A H2H league's ID.
:type league_id: string or int
:param return_json: (optional) Boolean. If ``True`` returns a ``dict``,
if ``False`` returns a :class:`H2HLeague` object. Defaults to
``False``.
:type return_json: bool
:rtype: :class:`H2HLeague` or ``dict``
"""
if not logged_in(self.session):
raise Exception("User must be logged in.")
url = API_URLS["league_h2h"].format(league_id)
league = await fetch(self.session, url)
if return_json:
return league
return H2HLeague(league, session=self.session)
:param list fixture_ids: A list of fixture IDs.
:param return_json: (optional) Boolean. If ``True`` returns a list of
``dict``s, if ``False`` returns a list of :class:`Fixture`
objects. Defaults to ``False``.
:type return_json: bool
:rtype: list
"""
if not fixture_ids:
return []
fixtures = await fetch(self.session, API_URLS["fixtures"])
fixture_gameweeks = set(fixture["event"] for fixture in fixtures
if fixture["id"] in fixture_ids)
tasks = [asyncio.ensure_future(
fetch(self.session,
API_URLS["gameweek_fixtures"].format(gameweek)))
for gameweek in fixture_gameweeks]
gameweek_fixtures = await asyncio.gather(*tasks)
merged_fixtures = list(itertools.chain(*gameweek_fixtures))
fixtures = [fixture for fixture in merged_fixtures
if fixture["id"] in fixture_ids]
if return_json:
return fixtures
return [Fixture(fixture) for fixture in fixtures]
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])
:type page: string or int
: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