-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
352 lines (254 loc) · 10.9 KB
/
main.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import logging
import time
from config import settings
import requests
logger = logging.getLogger(__name__)
def get_player_summaries(user_id: int) -> dict | None:
"""
Return a list of information about a person.
:user_id <int>: Player ID.
"""
url = f'https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' \
f'key={settings.key}&' \
f'steamids={user_id}'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'User ID - {user_id}. '
f'Status code - {response.status_code}.')
return None
if not response.json()['response']['players']:
logger.warning(f'User - {user_id}. '
f'There is no such user')
return None
try:
for item in response.json()['response']['players'][0].items():
print(item)
except Exception as _ex:
logger.warning(f'Error is {_ex}. '
f'Function - get_player_summaries()')
return None
return response.json()
def get_players_summaries(user_ids: list[int]) -> list[dict]:
"""
Return a list of information about people.
:user_ids <list[int]>: List of players IDs.
"""
lst_users = []
for user_id in user_ids:
url = f'https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?' \
f'key={settings.key}&' \
f'steamids={user_id}'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'User ID - {user_id}. '
f'Status code - {response.status_code}.')
continue
if not response.json()['response']['players']:
logger.warning(f'User - {user_id}. '
f'There is no such user')
continue
lst_users.append(response.json())
for user in response.json()['response']['players'][0].items():
print(user)
print()
time.sleep(0.5)
return lst_users
def get_global_achievement_percentages_for_app(game_id: int) -> dict | None:
"""
Returns on global achievements overview of a specific game in percentages.
:game_id <int>: Game ID.
"""
url = f'https://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?' \
f'gameid={game_id}&' \
f'format=json'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Game ID - {game_id}. '
f'Status code - {response.status_code}.')
return None
try:
for achieve in response.json()['achievementpercentages']['achievements']:
print(achieve)
except Exception as _ex:
logger.warning(f'Error is {_ex}. '
f'Function - get_global_achievement_percentages_for_app()')
return None
return response.json()
def get_news_for_app(app_id: int, count: int, max_length: int) -> dict | None:
"""
Return the latest of a game specified by its appID.
:app_id <int>: AppID of the game you want the news of.
:count <int>: How much news entries you want to get returned.
:max_length <int>: Maximum length of each news entry.
"""
url = f'https://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?' \
f'appid={app_id}&' \
f'count={count}&' \
f'maxlength={max_length}&' \
f'format=json'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'App ID - {app_id}. '
f'Count - {count}. '
f'Max Length - {max_length}. '
f'Status code - {response.status_code}.')
return None
for news in response.json()['appnews']['newsitems']:
for article in news.items():
print(article)
print()
return response.json()
def get_friend_list(steam_id: int, relationship: str) -> dict | None:
"""
Returns the friend list of any Steam user,
provided their Steam Community profile visibility is set to “Public”.
:steam_id <int>: User ID.
:relationship <str>: Relationship filter. Possibles values: all, friend.
"""
url = f'https://api.steampowered.com/ISteamUser/GetFriendList/v0001/?' \
f'key={settings.key}&' \
f'steamid={steam_id}&' \
f'relationship={relationship}'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Steam ID - {steam_id}. '
f'Relationship - {relationship}. '
f'Status code - {response.status_code}.')
return None
for friends in response.json()['friendslist']['friends']:
for friend in friends.items():
print(friend)
print()
return response.json()
def get_player_achievements(steam_id: int, app_id: int) -> dict | None:
"""
Returns a list of achievements for this user by app ID.
:steam_id <int>: 64 bit Steam ID to return friend list for.
:app_id <int>: The ID for the game you're requesting.
"""
url = f'https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?' \
f'appid={app_id}&' \
f'key={settings.key}&' \
f'steamid={steam_id}'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Steam ID - {steam_id}. '
f'App ID - {app_id}. '
f'Status code - {response.status_code}.')
return None
print('Name:', response.json()['playerstats']['gameName'])
print()
for achievement in response.json()['playerstats']['achievements']:
print(achievement)
print()
return response.json()
def get_user_stats_for_game(steam_id: int, app_id: int) -> dict | None:
"""
Returns a list of achievements for this user by app ID.
:steam_id <int>: 64 bit Steam ID to return friend list for.
:app_id <int>: The ID for the game you're requesting.
"""
url = f'https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?' \
f'appid={app_id}&' \
f'key={settings.key}&' \
f'steamid={steam_id}'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Steam ID - {steam_id}. '
f'App ID - {app_id}. '
f'Status code - {response.status_code}.')
return None
if not response.json()['playerstats'].get('achievements', False):
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'App ID - {app_id}. '
f'User ID - {steam_id} doesn\'t have a game {app_id}.')
return None
print('Name:', response.json()['playerstats']['gameName'])
print()
for achievement in response.json()['playerstats']['achievements']:
print(achievement)
print()
return response.json()
def get_owned_games(steam_id: int) -> dict | None:
"""
Return a list of games a player owns along with some playtime information,
if the profile is publicly visible.
Private, friends-only, and other privacy settings are not supported unless
you are asking for your own personal details
(ie the WebAPI key you are using is linked to the steamid you are requesting).
:steam_id <int>: The SteamID of the account.
"""
url = f'https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?' \
f'key={settings.key}&' \
f'steamid={steam_id}&' \
f'include_played_free_games=1&' \
f'include_appinfo=1'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Steam ID - {steam_id}. '
f'Status code - {response.status_code}.')
return None
if not response.json()['response']:
logger.warning(f'Steam ID - {steam_id}. Response is empty.')
return None
print(f'Games count - {response.json()["response"]["game_count"]}')
print()
for game in response.json()['response']['games']:
print(game)
print()
return response.json()
def get_recently_played_games(steam_id: int, count: int) -> dict | None:
"""
Return a list of games a player has played in the last two weeks,
if the profile is publicly visible.
Private, friends-only, and other privacy settings are not supported
unless you are asking for your own personal details
(ie the WebAPI key you are using is linked to the steamid you are requesting).
:steam_id <int>: The SteamID of the account.
:count <int>: Optionally limit to a certain number of games
(the number of games a person has played in the last 2 weeks is typically very small).
"""
url = f'https://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?' \
f'key={settings.key}&' \
f'steamid={steam_id}&' \
f'count={count}&' \
f'format=json'
response = requests.get(url)
if response.status_code >= 400:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Steam ID - {steam_id}. '
f'Count - {count}. '
f'Status code - {response.status_code}.')
return None
if not response.json()['response']['total_count']:
logger.warning(f'Key - {str(settings.key)[:6]}... '
f'Count - {count}. '
f'Steam ID - {steam_id} has not played recently')
return None
print('Games count -', response.json()['response']['total_count'])
print()
for game in response.json()['response']['games']:
print(game)
print()
return response.json()
def main():
try:
# get_player_summaries(76561197963562688)
# get_players_summaries([76561197963562688, 76561197963562688])
# get_global_achievement_percentages_for_app(440)
# get_news_for_app(440, 3, 300)
# get_friend_list(76561197960435530, 'all')
# get_player_achievements(76561199087475515, 1091500)
# get_user_stats_for_game(76561199087475515, 1091500)
# get_owned_games(76561199087475515)
# get_recently_played_games(76561199087475515, 3)
pass
except Exception as _ex:
logger.critical(f'Error is {_ex}. '
f'Function - main()')
if __name__ == '__main__':
main()