Skip to content

Commit 4be2325

Browse files
author
extreme4all
committed
f/highscore-rank
1 parent 32de70b commit 4be2325

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

osrs/asyncio/osrs/hiscores.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ class PlayerStats(BaseModel):
4141
activities: list[Activity]
4242

4343

44+
class HighscoreRank(BaseModel):
45+
name: str
46+
score: int
47+
rank: int
48+
49+
4450
class Hiscore:
4551
BASE_URL = "https://secure.runescape.com"
4652

@@ -108,3 +114,44 @@ async def get(
108114
return PlayerStats(**data), total_time
109115

110116
return PlayerStats(**data)
117+
118+
async def get_rank(
119+
self,
120+
mode: Mode,
121+
table: int,
122+
category: int,
123+
top_rank: int,
124+
size: int,
125+
session: ClientSession | None = None,
126+
) -> list[HighscoreRank]:
127+
logger.debug("Performing hiscores lookup on ")
128+
url = f"{self.BASE_URL}/m={mode.value}/ranking.json"
129+
params = {
130+
"table": table,
131+
"category": category,
132+
"toprank": top_rank,
133+
"size": size,
134+
}
135+
136+
_session = ClientSession() if session is None else session
137+
138+
async with _session.get(url, proxy=self.proxy, params=params) as response:
139+
if response.history and any(r.status == 302 for r in response.history):
140+
error_msg = (
141+
f"Redirection occured: {response.url} - {response.history[0].url}"
142+
)
143+
raise UnexpectedRedirection(error_msg)
144+
elif response.status != 200:
145+
response.raise_for_status()
146+
raise Undefined()
147+
data = await response.json()
148+
if session is None:
149+
await _session.close()
150+
return [
151+
HighscoreRank(
152+
name=d["name"],
153+
score=int(d["score"].replace(",", "")),
154+
rank=int(d["rank"].replace(",", "")),
155+
)
156+
for d in data
157+
]

tests/test_async_osrs_hiscore.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,20 @@ async def test_get_default_no_session():
4646
)
4747
assert player_stats.skills, "Skills data should not be empty"
4848
assert player_stats.activities, "Activities data should not be empty"
49+
50+
51+
@pytest.mark.asyncio
52+
async def test_get_valid_ranking():
53+
hiscore_instance = Hiscore()
54+
async with ClientSession() as session:
55+
player_stats = await hiscore_instance.get_rank(
56+
mode=HSMode.OLDSCHOOL,
57+
session=session,
58+
table=24, # sailing
59+
category=0,
60+
size=50, # Number of results to return
61+
top_rank=0, # Get top 50 player
62+
)
63+
64+
# Assertions to confirm the response is correct
65+
assert isinstance(player_stats, list)

0 commit comments

Comments
 (0)