Skip to content

Commit

Permalink
Updated to new riot-id / account endpoints (#450)
Browse files Browse the repository at this point in the history
updated to new riot-id / account endpoints
  • Loading branch information
jjmaldonis authored May 5, 2024
1 parent 9406d05 commit 9bbdf4d
Show file tree
Hide file tree
Showing 46 changed files with 867 additions and 265 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ import cassiopeia as cass

cass.set_riot_api_key("YOUR_KEY") # This overrides the value set in your configuration/settings.

summoner = cass.get_summoner(name="Perkz", region="NA")
print("{name} is a level {level} summoner on the {region} server.".format(name=summoner.name,
account = cass.get_account(name="Perkz", tagline="Style", region="NA")
summoner = account.summoner
print("{name} is a level {level} summoner on the {region} server.".format(name=account.name_with_tagline,
level=summoner.level,
region=summoner.region))

Expand Down
2 changes: 2 additions & 0 deletions cassiopeia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_status,
get_summoner,
get_summoner_spells,
get_account,
get_version,
get_versions,
get_champion_rotations,
Expand All @@ -51,6 +52,7 @@
Versions,
Maps,
Summoner,
Account,
ChampionMastery,
ChampionMasteries,
Match,
Expand Down
13 changes: 12 additions & 1 deletion cassiopeia/_configuration/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib
import inspect
import copy
import os

from datapipelines import (
DataPipeline,
Expand Down Expand Up @@ -126,7 +127,7 @@ def get_default_config():
"pipeline": {
"Cache": {},
"DDragon": {},
"RiotAPI": {"api_key": "RIOT_API_KEY"},
"RiotAPI": {"api_key": "$RIOT_API_KEY"},
},
"logging": {
"print_calls": True,
Expand Down Expand Up @@ -163,6 +164,16 @@ def __init__(self, settings):
logger.setLevel(level)
for handler in logger.handlers:
handler.setLevel(level)
if (
_defaults.get("pipeline", {})
.get("RiotAPI", {})
.get("api_key", "")
.startswith("$")
):
riot_api_key_env_var_name = _defaults["pipeline"]["RiotAPI"]["api_key"][1:]
riot_api_key = os.environ.get(riot_api_key_env_var_name, None)
if riot_api_key:
self.set_riot_api_key(riot_api_key)

@property
def pipeline(self) -> DataPipeline:
Expand Down
16 changes: 13 additions & 3 deletions cassiopeia/cassiopeia.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .core import (
Champion,
Summoner,
Account,
ChampionMastery,
Rune,
Item,
Expand Down Expand Up @@ -168,10 +169,19 @@ def get_summoner(
*,
id: str = None,
account_id: str = None,
name: str = None,
region: Union[Region, str] = None
puuid: str = None,
region: Union[Region, str] = None,
) -> Summoner:
return Summoner(id=id, account_id=account_id, name=name, region=region)
return Summoner(id=id, account_id=account_id, puuid=puuid, region=region)


def get_account(
*,
puuid: str = None,
name: str = None,
tagline: str = None,
) -> Account:
return Account(puuid=puuid, name=name, tagline=tagline)


def get_champion(key: Union[str, int], region: Union[Region, str] = None) -> Champion:
Expand Down
1 change: 1 addition & 0 deletions cassiopeia/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
)
from .champion import ChampionRotation
from .summoner import Summoner
from .account import Account
from .championmastery import ChampionMastery, ChampionMasteries
from .match import Match, MatchHistory
from .spectator import CurrentMatch, FeaturedMatches
Expand Down
180 changes: 180 additions & 0 deletions cassiopeia/core/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
from typing import Union

from datapipelines import NotFoundError
from merakicommons.cache import lazy_property
from merakicommons.container import searchable

from ..data import Region, Platform, Continent
from .common import CoreData, CassiopeiaGhost, ghost_load_on
from ..dto.account import AccountDto


##############
# Data Types #
##############


class AccountData(CoreData):
_dto_type = AccountDto
_renamed = {"gameName": "name", "tagLine": "tagline"}


##############
# Core Types #
##############


@searchable(
{
str: ["name", "tagline", "region", "platform", "continent", "puuid"],
Region: ["region"],
Platform: ["platform"],
Continent: ["continent"],
}
)
class Account(CassiopeiaGhost):
_data_types = {AccountData}

def __init__(
self,
*,
puuid: str = None,
name: str = None,
tagline: str = None,
region: Union[Region, str] = None,
):
kwargs = {"region": region}

if puuid is not None:
kwargs["puuid"] = puuid
if name is not None:
kwargs["name"] = name
if tagline is not None:
kwargs["tagline"] = tagline
super().__init__(**kwargs)

@classmethod
def __get_query_from_kwargs__(
cls,
*,
puuid: str = None,
name: str = None,
tagline: str = None,
region: Union[Region, str],
) -> dict:
query = {"region": region}
if puuid is not None:
query["puuid"] = puuid
if name is not None:
query["name"] = name
if tagline is not None:
query["tagline"] = tagline
return query

def __get_query__(self):
query = {"region": self.region, "platform": self.platform}
try:
query["puuid"] = self._data[AccountData].puuid
except AttributeError:
pass
try:
query["name"] = self._data[AccountData].name
except AttributeError:
pass
try:
query["tagline"] = self._data[AccountData].tagline
except AttributeError:
pass
assert "puuid" in query or ("name" in query and "tagline" in query)
return query

def __eq__(self, other: "Account"):
if not isinstance(other, Account) or self.region != other.region:
return False
s = {}
o = {}
if hasattr(self._data[AccountData], "puuid"):
s["puuid"] = self.puuid
if hasattr(other._data[AccountData], "puuid"):
o["puuid"] = other.puuid
if hasattr(self._data[AccountData], "name"):
s["name"] = self.name
if hasattr(other._data[AccountData], "name"):
o["name"] = other.name
if hasattr(self._data[AccountData], "tagline"):
s["tagline"] = self.tagline
if any(s.get(key, "s") == o.get(key, "o") for key in s):
return True
else:
return self.id == other.id

def __str__(self):
puuid = "?"
name = "?"
tagline = "?"
if hasattr(self._data[AccountData], "puuid"):
puuid = self.puuid
if hasattr(self._data[AccountData], "name"):
name = self.name
if hasattr(self._data[AccountData], "tagline"):
tagline = self.tagline
return f"Account(puuid={puuid}, name={name}, tagline='{tagline}')"

@property
def exists(self):
try:
if not self._Ghost__all_loaded:
self.__load__()
# Make sure we can access these attributes
self.puuid
self.name
self.tagline
return True
except (AttributeError, NotFoundError):
return False

@lazy_property
def region(self) -> Region:
"""The region for this summoner."""
return Region(self._data[AccountData].region)

@lazy_property
def platform(self) -> Platform:
"""The platform for this summoner."""
return self.region.platform

@lazy_property
def continent(self) -> Platform:
"""The continent for this summoner."""
return self.region.continent

@CassiopeiaGhost.property(AccountData)
@ghost_load_on
def puuid(self) -> str:
return self._data[AccountData].puuid

@CassiopeiaGhost.property(AccountData)
@ghost_load_on
def name(self) -> str:
return self._data[AccountData].name

@CassiopeiaGhost.property(AccountData)
@ghost_load_on
def tagline(self) -> str:
return self._data[AccountData].tagline

@property
def name_with_tagline(self) -> str:
return f"{self.name} #{self.tagline}"

# Special core methods

@property
def summoner(self) -> "Summoner":
from .summoner import Summoner

return Summoner(puuid=self.puuid, region=self.region)


# Add circular references at the bottom
from .summoner import Summoner
26 changes: 10 additions & 16 deletions cassiopeia/core/championmastery.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,24 @@ class ChampionMastery(CassiopeiaGhost):
def __init__(
self,
*,
summoner: Union[Summoner, int, str] = None,
summoner: Union[Summoner, str] = None,
champion: Union[Champion, int, str] = None,
region: Union[Region, str] = None,
_account_id: str = None
):
kwargs = {"region": region}

if _account_id is not None:
if not isinstance(summoner, Summoner) and _account_id is not None:
summoner = Summoner(account_id=_account_id, region=region)

if summoner is not None:
elif summoner is not None:
if isinstance(summoner, Summoner):
self.__class__.summoner.fget._lazy_set(self, summoner)
elif isinstance(summoner, str):
if len(summoner) < 35:
# It's a summoner name
summoner = Summoner(name=summoner, region=region)
if 70 < len(summoner) < 85:
# puuids should always have 78 characters, but I don't know that for sure. The range 70-85 is likely good.
summoner = Summoner(puuid=summoner, region=region)
self.__class__.summoner.fget._lazy_set(self, summoner)
else:
# It's probably a summoner id
else: # Assume the summoner is a summonerId
kwargs["summonerId"] = summoner

if champion is not None:
Expand All @@ -147,20 +145,16 @@ def __init__(
def __get_query_from_kwargs__(
cls,
*,
summoner: Union[Summoner, int, str],
summoner: Union[Summoner, str],
champion: Union[Champion, int, str],
region: Union[Region, str]
) -> dict:
query = {"region": region}
if isinstance(summoner, Summoner):
query["summoner.id"] = summoner.id
elif isinstance(summoner, str):
if len(summoner) < 35:
# It's a summoner name
query["summoner.id"] = Summoner(name=summoner, region=region).id
else:
# It's probably a summoner id
query["summoner.id"] = summoner
# It's probably a summoner id
query["summoner.id"] = summoner

if isinstance(champion, Champion):
query["champion.id"] = champion.id
Expand Down
8 changes: 1 addition & 7 deletions cassiopeia/core/league.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ def losses(self) -> int:
def summoner(self) -> Summoner:
return Summoner(
id=self._data[LeagueEntryData].summonerId,
name=self._data[LeagueEntryData].summonerName,
region=self.region,
)

Expand Down Expand Up @@ -375,12 +374,7 @@ def __get_query_from_kwargs__(cls, *, summoner: Union[Summoner, str]) -> dict:
if isinstance(summoner, Summoner):
query["summoner.id"] = summoner.id
elif isinstance(summoner, str):
if len(summoner) < 35:
query["summoner.id"] = Summoner(
name=summoner, region=summoner.region
).id
else:
query["summoner.id"] = summoner
query["summoner.id"] = summoner
assert "summoner.id" in query
return query

Expand Down
Loading

0 comments on commit 9bbdf4d

Please sign in to comment.