-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
365 lines (319 loc) · 9.7 KB
/
server.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
353
354
355
356
357
358
359
360
361
362
363
364
365
from flask import Flask, jsonify, request
from flask_cors import CORS, cross_origin
import requests
from lxml import html
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
app = Flask(__name__)
CORS(app)
API_KEY = "4E8E3E7A328FAA7814C46E719092F581"
MATCH_HISTORY_ENDPOINT = "http://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/v001/"
MATCH_DETAILS_ENDPOINT = "http://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/v1"
FRIENDS_ENDPOINT = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/"
NAMES_ENDPOINT = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/"
DB_HISTORY_ENDPOINT = "https://www.dotabuff.com/players/" # 123123123
# /matches?lobby_type=ranked_matchmaking®ion=india
def get_history_endpoint(steamid, region):
return DB_HISTORY_ENDPOINT + str(steamid) + "/matches?lobby_type=ranked_matchmaking®ion=" + region
CLUSTERS = { # nicked from https://github.com/odota/dotaconstants
"111": 1,
"112": 1,
"113": 1,
"121": 2,
"122": 2,
"123": 2,
"124": 2,
"131": 3,
"132": 3,
"133": 3,
"134": 3,
"135": 3,
"136": 3,
"137": 3,
"138": 3,
"144": 19,
"145": 19,
"151": 5,
"152": 5,
"153": 5,
"154": 5,
"155": 5,
"156": 5,
"161": 6,
"171": 7,
"172": 7,
"181": 8,
"182": 8,
"183": 8,
"184": 8,
"185": 8,
"186": 8,
"187": 8,
"188": 8,
"191": 9,
"192": 9,
"193": 9,
"201": 10,
"202": 10,
"204": 10,
"211": 11,
"212": 11,
"213": 11,
"223": 18,
"224": 12,
"225": 17,
"227": 20,
"231": 13,
"232": 25,
"241": 14,
"242": 14,
"251": 15,
"261": 16
}
REGIONS = { # again, nicked from dotaconstants
"0": "AUTOMATIC",
"1": "US WEST",
"2": "US EAST",
"3": "EUROPE",
"5": "SINGAPORE",
"6": "DUBAI",
"7": "AUSTRALIA",
"8": "STOCKHOLM",
"9": "AUSTRIA",
"10": "BRAZIL",
"11": "SOUTHAFRICA",
"12": "PW TELECOM SHANGHAI",
"13": "PW UNICOM",
"14": "CHILE",
"15": "PERU",
"16": "INDIA",
"17": "PW TELECOM GUANGDONG",
"18": "PW TELECOM ZHEJIANG",
"19": "JAPAN",
"20": "PW TELECOM WUHAN",
"25": "PW UNICOM TIANJIN"
}
DB_REGIONS = [
'us_west',
'us_east',
'europe_west',
'south_korea',
'se_asia',
'chile',
'australia',
'russia',
'europe_east',
'south_america',
'south_africa',
'china',
'peru',
'dubai',
'india'
]
DB_TO_OD = {
'us_west': "US WEST",
'us_east': "US EAST",
'europe_west': "EUROPE",
'south_korea': "PW UNICOM",
'se_asia': "SINGAPORE",
'chile' : "CHILE",
'australia': "AUSTRALIA",
'russia': "EUROPE",
'europe_east': "EUROPE",
'south_america': "BRAZIL",
'south_africa': "SOUTHAFRICA",
'china': "PW TELECOM GUANGDONG",
'peru': "PERU",
'dubai': "DUBAI",
'india': "INDIA"
}
# Helpers
def fetch_friend_ids(steamid):
payload = { 'key': API_KEY, 'steamid': steamid, 'relationship': 'friend'}
response = requests.get(FRIENDS_ENDPOINT, params=payload)
friends = response.json()['friendslist']['friends']
ids = []
for friend in friends:
ids.append(friend['steamid'])
return ids
def fetch_friends(ids):
ids_str = ''
for id in ids:
ids_str = ids_str + id + ','
payload = { 'key': API_KEY, 'steamids': ids_str}
response = requests.get(NAMES_ENDPOINT, params=payload)
profiles = response.json()['response']['players']
names = []
for profile in profiles:
names.append({'name': profile['personaname'], 'id': profile['steamid']})
return names
def fetch_matches(steamid):
# grab the final match_id and request again if you want more than 100
payload = {
'key': API_KEY,
'account_id': steamid,
'matches_requested': 100 # 100 max
}
response = requests.get(MATCH_HISTORY_ENDPOINT, params=payload)
# try: # make sure response is parseable
# response.json()
# except ValueError:
# print response
# return []
if response.json()['result']['status'] != 1: # check if player match history is public
return []
matches = response.json()['result']['matches']
match_ids = []
for match in matches:
match_ids.append(match['match_id'])
return match_ids
def fetch_match_details(match_id, steamid):
# grab the final match_id and request again if you want more than 100
payload = {
'key': API_KEY,
'match_id': match_id,
}
response = requests.get(MATCH_DETAILS_ENDPOINT, params=payload)
# try: # make sure response is parseable
# response.json()
# except ValueError:
# print response
# return False
details = response.json()['result']
# did the player win?
player_is_radiant = True
for player in details['players']:
if 'account_id' not in player: # I think bots don't have account_id?
continue
if str(steamid) == str(player['account_id']):
if player['player_slot'] < 5:
player_is_radiant = True
else:
player_is_radiant = False
if (player_is_radiant and details['radiant_win']) or (not player_is_radiant and not details['radiant_win']):
player_won = True
else:
player_won = False
data = {
"player_won": player_won,
"server_cluster": details['cluster'],
"ranked?": str(details['lobby_type']) == "7"
}
return data
def calculate_winrate_by_server(match_details):
server_list = {}
for item in match_details:
region_code = CLUSTERS[str(item['server_cluster'])] # show regions rather than data centers
region = REGIONS[str(region_code)]
if region not in server_list:
server_list[region] = []
server_list[region].append({
'won': item['player_won'],
'ranked': item['ranked?']
})
winrate_list = []
for server in server_list:
ranked = {
'wins': 0,
'losses': 0,
'games': 0
}
unranked = {
'wins': 0,
'losses': 0,
'games': 0
}
for result in server_list[server]:
if result['ranked']:
ranked['games'] = ranked['games'] + 1
if result['won']:
ranked['wins'] = ranked['wins'] + 1
else:
ranked['losses'] = ranked['losses'] + 1
else:
unranked['games'] = unranked['games'] + 1
if result['won']:
unranked['wins'] = unranked['wins'] + 1
else:
unranked['losses'] = unranked['losses'] + 1
ranked_winrate_for_server = float(ranked['wins'])/(ranked['games']) if ranked['games'] > 0 else None
unranked_winrate_for_server = float(unranked['wins'])/(unranked['games']) if unranked['games'] > 0 else None
winrate_list.append({
'server': server,
'stats': {
'ranked': {
'games': ranked['games'],
'winrate': ranked_winrate_for_server,
'wins': ranked['wins'],
'losses': ranked['losses']
},
'unranked': {
'games': unranked['games'],
'winrate': unranked_winrate_for_server,
'wins': unranked['wins'],
'losses': unranked['losses']
}
}
})
return winrate_list
# Routes
@app.route("/api/v1/friends/names", methods=['GET'])
def friends():
args = request.args.to_dict()
ids = fetch_friend_ids(args['steamid'])
friends = fetch_friends(ids)
return jsonify(friends)
@app.route("/api/v1/server_winrates", methods=['GET'])
def server_winrates():
args = request.args.to_dict()
matches = fetch_matches(args['steamid'])
match_details = []
for match_id in matches:
match = fetch_match_details(match_id, args['steamid'])
if match:
match_details.append(match)
winrates = calculate_winrate_by_server(match_details)
return jsonify(winrates)
@app.route("/api/v2/server_winrates", methods=['GET'])
def server_winrates_v2():
args = request.args.to_dict()
# fake a 'real' request so DB doesn't hit us with a 429
session = requests.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'})
db_winrates = {}
for region in DB_REGIONS:
response = session.get(get_history_endpoint(args['steamid'], region))
if response.status_code == 429:
# oh no, rate limited!
print "Rate limited. Reverting to slower Steam API method..."
return server_winrates() # guess we'll do it the hard way
tree = html.fromstring(response.content)
wr_list = tree.xpath('//span[@class="color-stat-win"]/text()')
if len(wr_list) > 0:
winrate = wr_list[0]
db_winrates[region] = winrate
winrates = []
for server in db_winrates:
new_name = DB_TO_OD[server]
if db_winrates[server][0] == "0":
winrate = 0
elif db_winrates[server][0:3] == "100":
winrate = 1
else:
winrate = float("0." + db_winrates[server][0:2] + db_winrates[server][3:5])
winrates.append({
'server': new_name,
'stats': {
'ranked': {
'winrate': winrate,
'games': 10
},
'unranked': { # mocked out 'neutral' data
'winrate': 0.5,
'games': 10
}
}
}) # massage into expected format
return jsonify(winrates)
if __name__ == "__main__":
app.run()