From 41c5d0386c47d698693df8c0ed052251bf51666e Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 28 May 2021 05:30:30 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Begin=20work=20on=20the=20crypt=20d?= =?UTF-8?q?ailies=20service?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalpack/services/cryptdailies.py | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 royalpack/services/cryptdailies.py diff --git a/royalpack/services/cryptdailies.py b/royalpack/services/cryptdailies.py new file mode 100644 index 00000000..6ad89871 --- /dev/null +++ b/royalpack/services/cryptdailies.py @@ -0,0 +1,64 @@ +""" +Service that fetches Crypt of the NecroDancer daily run scores, and broadcasts them to the chat. + +Data is fetched from the `Toofz API`_, which in turn fetches it from the Steam leaderboard using `SteamKit`_. + +.. _Toofz API: https://api.toofz.com/help +.. _SteamKit: https://github.com/SteamRE/SteamKit +""" + +import royalnet.royaltyping as t +import aiohttp +import asyncio +import datetime + + +async def get_leaderboards_info() -> list[t.JSON]: + """ + :return: A list of all available leaderboards. + """ + + async with aiohttp.ClientSession() as session: + async with session.get("https://api.toofz.com/leaderboards/dailies") as request: + data = await request.json() + return data["leaderboards"] + + +async def get_latest_leaderboard_info() -> dict[str, t.JSON]: + """ + :return: List data about the latest leaderboard. + """ + + leaderboards = await get_leaderboards_info() + latest = leaderboards[0] + assert latest["production"] is True + assert latest["product"] == "amplified" + assert datetime.datetime.fromtimestamp(latest["date"]).date() == datetime.date.today() + assert latest["total"] > 0 + return latest + + +async def get_all_leaderboard_entries(lbid): + """ + :param lbid: The leaderboard id to retrieve entries of. + :return: All entries from the daily leaderboard with the specific lbid. + """ + + total = None + offset = 0 + result = [] + + while total is None or offset < total: + async with aiohttp.ClientSession() as session: + async with session.get(f"https://api.toofz.com/leaderboards/dailies/{lbid}/entries?offset={offset}&limit=100") as request: + print(f"{total=} {offset=} {result=}") + data = await request.json() + assert data["leaderboard"]["id"] == lbid + if total is None: + total = data["total"] + assert data["total"] == total + offset += len(data["entries"]) + result = [*result, *data["entries"]] + await asyncio.sleep(1) + + return result