-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_example.py
79 lines (54 loc) · 2.86 KB
/
api_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
import os
import sys
from pprint import pprint
from dotenv import load_dotenv
from mt_leaderboard.api import MonsterTrainAPI
# auth_token stored in .env
load_dotenv()
# Access environment variables as if they came from the actual environment
auth_token = os.getenv("AUTH_TOKEN")
"""
About auth:
The Monster Train leaderboard API is neither official nor documented.
You need to own the game and an auth token is mandatory to call the game's servers/leaderboards endpoints.
Not an expert but this token is built from your Steam Id + a session auth ticket (that changes every time) generated by Steam/Monster Train.
The authentification process (steam id + session ticket => auth token) is then done through the /auth endpoint (not documented here)
As I wasn't able to get this session ticket by other means (steam api etc.), I'm using an already-generated authentification token.
The auth token seems to be of single use, or at least time limited (24h ? single session ?)
You should be able to retrieve it using an HTTP(s) network traffic analyzer tool when accessing the daily challenge leaderboard in-game.
I'm not documenting this part as this project is more of a one-shot so I can have fun crunching players' stats & builds
Just so you know, you can't reuse an existing token and will need to generate a new one for every session
(by launching the game / access the leaderboards) every time you use the client.
"""
# Example usage of the Monster Train API
# auth_token loaded from .env
# challenge_id can be either specified, or (like here) read from /challenge endpoint
# same thing applies to run_id and user_id. Here we just take the first result of /leaderboard to get this player's run
def main():
api = MonsterTrainAPI(auth_token=auth_token)
# The api responses (Pydandic) objects (Leadearboard, Challenge, Gamerun), as defined in models/
# Get challenge(s) details : no dlc, today and previous day challenges :
vanilla_challenge = api.get_challenge(dlc=0, day=1)
print("\nToday's Vanilla Challenge (no DLC):")
pprint(vanilla_challenge)
current_challenge = api.get_challenge(dlc=1, day=1)
print("\nToday's Challenge:")
previous_challenge = api.get_challenge(dlc=1, day=-1)
print("\nLast day Challenge:")
pprint(previous_challenge)
# Leaderboard : first 10 players (1st page) for today challenge :
challenge_id = current_challenge.id
leaderboard = api.get_leaderboard(challenge_id=challenge_id, offset=1, limit=10)
print("\nLeaderboard:")
pprint(leaderboard)
# Get run details from 1st player in leaderboard :
user_id = leaderboard.leaderboard[0].playerId
run_id = leaderboard.leaderboard[0].runId
gamerun = api.get_gamerun(user_id=user_id, run_id=run_id)
print("\nGame Run:")
pprint(gamerun)
if __name__ == "__main__":
main()