-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteamapi.py
148 lines (130 loc) · 4.56 KB
/
steamapi.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
from datetime import datetime
from urllib import urlopen, urlencode
try:
from simplejson import loads
except:
from json import loads
import re, sys
API_HOST = 'http://api.steampowered.com'
APP_IDS = {
'tf2': 440,
'tf2beta': 520,
'portal2': 620,
}
class SteamAPIClient:
def __init__(self, api_key, lang=None):
self.api_key = api_key
self.language = lang
self.base_schemas = {}
def send_request(self, path, params):
params.update({
'key': self.api_key,
'format': 'json',
})
url = API_HOST + path + '?' + urlencode(params)
rsp = urlopen(url)
data = loads(rsp.read())
data['last-modified'] = datetime.strptime(rsp.headers['last-modified'],
'%a, %d %b %Y %H:%M:%S %Z')
return data
def get_schema(self, app_id, lang=None):
path = '/IEconItems_%s/GetSchema/v0001/' % app_id
params = {
'language': lang or self.language,
}
return self.send_request(path, params)
def get_player_items(self, app_id, steam_id):
path = '/IEconItems_%s/GetPlayerItems/v0001/' % app_id
params = {
'steamid': steam_id,
}
return self.send_request(path, params)
def get_asset_prices(self, app_id, lang=None):
path = '/ISteamEconomy/GetAssetPrices/v0001/'
params = {
'appid': app_id,
'language': lang or self.language,
}
return self.send_request(path, params)
def get_asset_class_info(self, app_id, class_ids, lang=None):
path = '/ISteamEconomy/GetAssetClassInfo/v0001/'
params = {
'appid': app_id,
'language': lang or self.language,
'class_count': len(class_ids),
}
for i, class_id in enumerate(class_ids):
params['classid%s' % i] = class_id
return self.send_request(path, params)
def resolve_vanity_url(self, custom_name):
path = '/ISteamUser/ResolveVanityURL/v0001/'
params = {
'vanityurl': custom_name,
}
return self.send_request(path, params)
def get_news_for_app(self, app_id, count=20, length=255):
path = '/ISteamNews/GetNewsForApp/v0002'
params = {
'appid': app_id,
'count': count,
'length': length,
}
return self.send_request(path, params)
def get_global_achievement_percentages_for_app(self, game_id):
path = '/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002'
params = {
'gameid': game_id,
}
return self.send_request(path, params)
def get_player_summaries(self, steam_ids):
path = '/ISteamUser/GetPlayerSummaries/v0002/'
params = {
'steamids': ','.join([str(x) for x in steam_ids]),
}
return self.send_request(path, params)
def get_friend_list(self, steam_id, relationship='all'):
path = '/ISteamUser/GetFriendList/v1'
params = {
'steamid': steam_id,
'relationship': relationship,
}
return self.send_request(path, params)
def get_server_schema(self, app_id):
schema = self.get_schema(app_id)['result']
url = schema['items_game_url']
rsp = urlopen(url)
schema = parse_schema(rsp)
return schema
def get_translation(self, app_id, lang='en_US'):
if not self.base_schemas.get(app_id):
self.base_schemas[app_id] = self.get_schema(app_id, None)
translations = {}
tokens = extract_tokens(self.base_schemas[app_id])
schema = self.get_schema(app_id, lang)
for path,token in tokens:
trans = extract_token_by_path(schema, path)
translations[token] = trans
return translations
def extract_tokens(data, path=[]):
tokens = []
if type(data) == dict:
gen = data.items()
elif type(data) == list:
gen = enumerate(data)
else:
raise Exception('non dict/list passed to extract_tokens')
for k,v in gen:
if type(v) in [dict,list]:
tokens.extend(extract_tokens(v, path + [k]))
elif isinstance(v, basestring):
if v.startswith('#'):
tokens.append( (path + [k], v) )
return tokens
def extract_token_by_path(schema, path):
obj = schema
for node in path:
if type(node) == int:
obj = obj[node]
elif isinstance(node, basestring):
obj = obj.get(node)
return obj.encode('utf8')