-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.py
More file actions
207 lines (165 loc) · 5.81 KB
/
client.py
File metadata and controls
207 lines (165 loc) · 5.81 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import requests as request
from retroachievements import __version__
_BASE_URL = "https://retroachievements.org/API/"
class RAClient:
"""
Main class for accessing the RetroAhievements Web API
"""
headers = {
"User-Agent": "RetroAchievements-api-python/" + __version__}
def __init__(self, username, api_key):
self.username = username
self.api_key = api_key
def url_params(self, params=None):
"""
Inserts the auth and query params into the request
"""
if params is None:
params = {}
params.update({"z": self.username, "y": self.api_key})
return params
# URL construction
def _call_api(self, endpoint=None, params=None, timeout=30, headers=None):
if endpoint is None:
endpoint = {}
req = request.get(
f"{_BASE_URL}{endpoint}",
params=self.url_params(params),
timeout=timeout,
headers=headers,
)
return req
# User endpoints
def get_user_points(self, user: str) -> dict:
"""
Get a user's total hardcore and softcore points
Params:
u: Username to query
"""
result = self._call_api("API_GetUserPoints.php?", {"u": user}).json()
return result
def get_user_summary(self, user: str,
recent_games=0,
recent_cheevos=10) -> dict:
"""
Get a user's exhaustive profile metadata
Params:
u: Username to query
g: Number of recent games to fetch, default = 0
a: Number of recent achievements to fetch, default = 10
"""
result = self._call_api(
"API_GetUserSummary.php?",
{"u": user, "g": recent_games, "a": recent_cheevos},
).json()
return result
def get_recent_achievements(self, user: str) -> dict:
"""
Get a user's most recent achievement within the last hour
Params:
u: Username to query
"""
result = self._call_api("API_GetUserRecentAchievements.php?", {"u": user}).json()
return result
def get_game_progress(self, user: str, game: int) -> dict:
"""
Get a users recent game info and progress
Params:
g: Game ID
u: Username to query
"""
result = self._call_api("API_GetGameInfoAndUserProgress.php?", {"g": game, "u": user}).json()
return result
def get_achievements_on_day(self, user: str, date: int) -> dict:
"""
Get a user's cheevos from a specific date
Params:
u: Username to query
d: Date to query
"""
result = self._call_api("API_GetAchievementsEarnedOnDay.php?", {"u": user, "d": date}).json()
return result
def get_achievements_range(self, user: str, f: int, t: int) -> dict:
"""
Get a user's cheevos from a specific date
Params:
u: Username to query
f: From date to query (must be in epoch timestamp format)
t: To date to query (must be in epoch timestamp format)
"""
result = self._call_api("API_GetAchievementsEarnedOnDay.php?", {"u": user, "f": f, "t": t }).json()
return result
def get_all_completion_progress(self, user: str) -> dict:
"""
Get a user's info and progress on all games
Params:
u: Username to query
"""
result = self._call_api("API_GetUserCompletionProgress.php?", {"u": user}).json()
return result
def get_awards_badges(self, user: str) -> list:
"""
Get a user's awards and badges on RA
Params:
u: Username to query
"""
result = self._call_api("API_GetUserAwards.php?", {"u": user}).json()
return result
# Game endpoints
def get_game(self, game: int) -> dict:
"""
Get basic metadata about a game
Params:
i: The game ID to query
"""
result = self._call_api("API_GetGame.php?", {"i": game}).json()
return result
def get_game_extended(self, game: int) -> dict:
"""
Get extended metadata about a game
Params:
i: The game ID to query
"""
result = self._call_api("API_GetGameExtended.php?", {"i": game}).json()
return result
def get_achievement_count(self, game: int) -> dict:
"""
Get the list of achievement ID's for a game
Params:
i: The game ID to query
"""
result = self._call_api(
"API_GetAchievementCount.php?", {"i": game}).json()
return result
def get_achievement_distribution(self, game: int) -> dict:
"""
Get how many players have unlocked how many achievements for a game
Params:
i: The game ID to query
"""
result = self._call_api(
"API_GetAchievementDistribution.php?", {"i": game}
).json()
return result
# System Endpoints
def get_console_ids(self) -> list:
"""
Get the complete list of console ID and name pairs on the site
Params:
None
"""
result = self._call_api("API_GetConsoleIDs.php?", {}).json()
return result
def get_game_list(self, system: int, has_cheevos=0, hashes=0) -> dict:
"""
Get the complete list of games for a console
Params:
i: The system ID to query
f: If 1, only returns games that have achievements (default = 0)
h: If 1, also return the supported hashes for games (default = 0)
"""
result = self._call_api(
"API_GetGameList.php?", {
"i": system, "f": has_cheevos, "h": hashes}
).json()
return result