Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions kickbase_api/kickbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from kickbase_api.models.market import Market
from kickbase_api.models.market_player import MarketPlayer
from kickbase_api.models.player import Player
from kickbase_api.models.player_stats import PlayerStats
from kickbase_api.models.response.league_stats_response import LeagueStatsResponse
from kickbase_api.models.user import User

Expand Down Expand Up @@ -211,6 +212,17 @@ def league_user_players(self, league: Union[str, LeagueData], user: Union[str, U
else:
raise KickbaseException()

def league_user_player_stats(self, league: Union[str, LeagueData], player: Union[str, Player]) -> PlayerStats:
league_id = self._get_league_id(league)
player_id = self._get_player_id(player)

r = self._do_get("/leagues/{}/players/{}/stats".format(league_id, player_id), True)

if r.status_code == 200:
return PlayerStats(r.json()["leaguePlayer"])
else:
raise KickbaseException()

def league_collect_gift(self, league: Union[str, LeagueData]) -> True:
league_id = self._get_league_id(league)

Expand Down Expand Up @@ -402,31 +414,33 @@ def exchange_custom_token(self, chat_token: str):
"Content-Type": "application/json",
"Accept": "application/json"
}

data = {
"returnSecureToken": True,
"token": chat_token
}

r = requests.post(self._url_for_google_identity_toolkit(
"/v3/relyingparty/verifyCustomToken?key={}".format(self.google_identity_toolkit_api_key)), data=json.dumps(data),
"/v3/relyingparty/verifyCustomToken?key={}".format(self.google_identity_toolkit_api_key)),
data=json.dumps(data),
headers=headers)

if r.status_code == 200:
j = r.json()
self.firebase_token = j["idToken"]
self.firebase_token_expire = datetime.now(timezone.utc) + timedelta(seconds=int(j["expiresIn"]))
else:
raise KickbaseException("There was an error exchanging custom token for firebase token")

def _update_firebase_token(self):
token = self.chat_token()
self.exchange_custom_token(token)

def chat_messages(self, league: Union[str, LeagueData], page_size: int = 30, next_page_token: str = None) -> ([ChatItem], str):
def chat_messages(self, league: Union[str, LeagueData], page_size: int = 30, next_page_token: str = None) -> (
[ChatItem], str):
if self.google_identity_toolkit_api_key is None:
return []

league_id = self._get_league_id(league)

if not self._is_token_valid():
Expand All @@ -441,9 +455,12 @@ def chat_messages(self, league: Union[str, LeagueData], page_size: int = 30, nex
"Authorization": "Bearer {}".format(self.firebase_token)
}
if next_page_token is None:
r = requests.get(self._url_for_firestore("/chat/{}/messages?pageSize={}".format(league_id, page_size)), headers=headers)
r = requests.get(self._url_for_firestore("/chat/{}/messages?pageSize={}".format(league_id, page_size)),
headers=headers)
else:
r = requests.get(self._url_for_firestore("/chat/{}/messages?pageSize={}&pageToken={}".format(league_id, page_size, next_page_token)), headers=headers)
r = requests.get(self._url_for_firestore(
"/chat/{}/messages?pageSize={}&pageToken={}".format(league_id, page_size, next_page_token)),
headers=headers)

if r.status_code == 200:
j = r.json()
Expand All @@ -458,7 +475,7 @@ def chat_messages(self, league: Union[str, LeagueData], page_size: int = 30, nex
def post_chat_message(self, message: str, league: Union[str, LeagueData]):
if self.google_identity_toolkit_api_key is None:
return

league_id = self._get_league_id(league)

if not self._is_token_valid():
Expand All @@ -472,7 +489,7 @@ def post_chat_message(self, message: str, league: Union[str, LeagueData]):
"Accept": "application/json",
"Authorization": "Bearer {}".format(self.firebase_token)
}

data = {
"fields": {
"userId": {
Expand Down Expand Up @@ -500,7 +517,7 @@ def post_chat_message(self, message: str, league: Union[str, LeagueData]):
}
}

r = requests.post(self._url_for_firestore("/chat/{}/messages".format(league_id)),
r = requests.post(self._url_for_firestore("/chat/{}/messages".format(league_id)),
data=json.dumps(data), headers=headers)

if r.status_code == 200:
Expand Down Expand Up @@ -552,7 +569,7 @@ def _auth_cookie(self):
return "kkstrauth={}".format(self.token)

def _do_get(self, endpoint: str, authenticated: bool = False):
if authenticated and not self._is_token_valid():
if authenticated and not self._is_token_valid():
self.login(self._username, self._password)

headers = {
Expand All @@ -578,7 +595,7 @@ def _do_post(self, endpoint: str, data: dict, authenticated: bool = False):
return requests.post(self._url_for_endpoint(endpoint), data=json.dumps(data), headers=headers)

def _do_put(self, endpoint: str, data: dict, authenticated: bool = False):
if authenticated and not self._is_token_valid():
if authenticated and not self._is_token_valid():
self.login(self._username, self._password)

headers = {
Expand All @@ -591,7 +608,7 @@ def _do_put(self, endpoint: str, data: dict, authenticated: bool = False):
return requests.put(self._url_for_endpoint(endpoint), data=json.dumps(data), headers=headers)

def _do_delete(self, endpoint: str, authenticated: bool = False):
if authenticated and not self._is_token_valid():
if authenticated and not self._is_token_valid():
self.login(self._username, self._password)

headers = {
Expand Down
15 changes: 15 additions & 0 deletions kickbase_api/models/player_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from kickbase_api.models.base_model import BaseModel


class PlayerStats(BaseModel):
market_value_change: int = None
market_value_change_percent: float = None

def __init__(self, d: dict = {}):
self._json_transform = {
}
self._json_mapping = {
"marketValueChange": "market_value_change",
"marketValueChangePercent": "market_value_change_percent"
}
super().__init__(d)
7 changes: 7 additions & 0 deletions tests/test_kickbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def test_league_user_players(logged_in_kickbase):
assert players is not None


@pytest.mark.online
def test_league_user_player_stats(logged_in_kickbase):
kickbase, user, leagues = logged_in_kickbase
stats = kickbase.league_user_player_stats(leagues[0], "44")
assert stats is not None


@pytest.mark.online
def test_league_user_players_match_day(logged_in_kickbase):
kickbase, user, leagues = logged_in_kickbase
Expand Down