-
Notifications
You must be signed in to change notification settings - Fork 1
/
steam_web_api.py
108 lines (88 loc) · 3.13 KB
/
steam_web_api.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
import requests
class SteamWebApi:
api_key: str = '2OEIYCPSNRMH6CFG'
base_url: str = 'https://www.steamwebapi.com/steam/api/'
def __init__(self, api_key: str = None, debug: bool = False):
"""
:param api_key: str
:param debug: bool
"""
if api_key is not None:
self.api_key = api_key
self.debug = debug
def _get_inventory_request(self, steam_id: str, game: str = 'csgo', language: str = 'english', parse: bool = True):
"""
:param steam_id: str
:param game: str
:param language: str
:param parse: str
:return: Response
"""
if self.debug:
print(
f'GET {self.base_url}inventory?steam_id={steam_id}&game={game}&language={language}&parse={parse}'
f'&key={self.api_key}')
response = requests.get(f'{self.base_url}inventory', params={
'steam_id': steam_id,
'game': game,
'language': language,
'parse': parse,
'key': self.api_key
})
if self.debug:
print(f'Status Code: {response.status_code}')
return response
def get_inventory(self, steam_id: str, game: str = 'csgo', language: str = 'english', parse: bool = True):
"""
:param steam_id: str
:param game: str
:param language: str
:param parse: str
:return: list
"""
response = self._get_inventory_request(steam_id, game, language, parse)
return response.json()
def get_inventory_worth(self, steam_id: str, game: str = 'csgo', language: str = 'english', parse: bool = True):
"""
:param steam_id: str
:param game: str
:param language: str
:param parse: bool
:return: dict
"""
response = self._get_inventory_request(steam_id, game, language, parse)
inventory = response.json()
return {
'worth': sum(float(item['priceavg']) for item in inventory),
'inventory': inventory
}
def get_steam_id(self, steam_id: str):
"""
:param steam_id: str
:return: dict
"""
if self.debug:
print(f'GET {self.base_url}steamid?steam_id={steam_id}&key={self.api_key}')
response = requests.get(f'{self.base_url}steamid', params={
'steam_id': steam_id,
'key': self.api_key
})
if self.debug:
print(f'Status Code: {response.status_code}')
return response.json()
def get_profile(self, steam_id: str, url_or_username: str = None):
"""
:param steam_id: str
:param url_or_username: str
:return: dict
"""
if self.debug:
print(f'GET {self.base_url}profile?steam_id={steam_id}&url={url_or_username}&key={self.api_key}')
response = requests.get(f'{self.base_url}profile', params={
'steam_id': steam_id,
'url': url_or_username,
'key': self.api_key
})
if self.debug:
print(f'Status Code: {response.status_code}')
return response.json()