-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
288 lines (253 loc) · 9 KB
/
parse.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
import argparse
import requests
from steam import webapi as steam_webapi
from steam.steamid import SteamID
from enum import Enum
STRATZ_API = 'https://api.stratz.com/graphql'
STRATZ_TOKEN = None
STEAM_TOKEN = None
class Medal(Enum):
UNRANKED = 0
HERALD=1
GUARDIAN=2
CRUSADER=3
ARCHON=4
LEGEND=5
ANCIENT=6
DIVINE=7
IMMORTAL=8
@classmethod
def from_int(cls, value):
if value == None:
return cls(0)
return cls(value // 10)
class RankMedal:
def __init__(self, num):
self.medal = Medal.from_int(num)
self.stars = num % 10 if num != None else None
def __str__(self):
return '{}{}'.format(self.medal.name.title(), '[{}]'.format(self.stars) if self.stars else '')
class Json2Obj:
def __init__(self, data):
self.__dict__ = data
for i in self.__dict__.keys():
child = self.__dict__[i]
if isinstance(child, dict):
if len(child) > 0:
self.__dict__[i] = Json2Obj(child)
if isinstance(child, list):
self.__dict__[i] = []
for item in child:
if isinstance(item, dict):
self.__dict__[i].append(Json2Obj(item))
else:
self.__dict__[i].append(item)
class BearerAuth(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers["authorization"] = "Bearer " + self.token
return r
class Profile:
def __init__(self, account_id):
self.steamID = SteamID(account_id)
self.stratz_data = None
self.steam_data = None
self._n_friends = -1
self._n_games_owned = -1
self.fetch()
@property
def rank(self) -> RankMedal:
return RankMedal(self.stratz_data.steamAccount.seasonRank)
@property
def medal(self) -> Medal:
self.rank.medal
@property
def medal_stars(self) -> int:
return self.rank.stars
@property
def rank_int(self) -> int:
return self.stratz_data.steamAccount.seasonRank
@property
def n_played(self) -> int:
return self.stratz_data.matchCount
@property
def winrate(self) -> float:
return self.stratz_data.winCount / self.stratz_data.matchCount
@property
def dota_anonymous(self) -> bool:
return self.stratz_data.steamAccount.isAnonymous
@property
def dota_level(self) -> int:
return self.stratz_data.steamAccount.dotaAccountLevel
@property
def steam_anonymous(self) -> bool:
"""
This represents whether the profile is visible or not, and if it is visible, why you are allowed to see it.
Note that because this WebAPI does not use authentication, there are only two possible values returned:
1 - the profile is not visible to you (Private, Friends Only, etc),
3 - the profile is "Public", and the data is visible.
Mike Blaszczak's post on Steam forums says,
"The community visibility state this API returns is different than the privacy state.
It's the effective visibility state from the account making the request to the account
being viewed given the requesting account's relationship to the viewed account."
"""
return self.stratz_data.steamAccount.communityVisibleState != 3
@property
def stratz_anmymous(self) -> bool:
return self.stratz_data.steamAccount.isStratzAnonymous
@property
def stratz_smurf(self) -> bool:
return self.stratz_data.steamAccount.smurfFlag == 1
@property
def steam_profile_set_up(self) -> bool:
return self.steam_data.profilestate == 1
@property
def n_friends(self) -> int or None:
if self._n_friends == -1:
if not self.steam_anonymous:
data = steam_webapi.get(
interface='ISteamUser',
method='GetFriendList',
version=1,
params={
'key': STEAM_TOKEN,
'steamid': self.steamID.as_64,
'relationship': 'friend'
}
)
self._n_friends = len(data['friendslist']['friends'])
else:
self._n_friends = None
return self._n_friends
@property
def default_pfp(self) -> bool:
#check if profile picture matches default
if self.steam_data.avatarfull == 'https://avatars.akamai.steamstatic.com/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_full.jpg':
#'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fece7b7c3c0e0e9e9d7b7c3c0e0e9e9d7b7c3c0e_full.jpg':
return True
return False
@property
def n_games_owned(self) -> int or None:
if self._n_games_owned == -1:
#if not self.steam_anonymous:
data = steam_webapi.get(
interface='IPlayerService',
method='GetOwnedGames',
version=1,
params={
'key': STEAM_TOKEN,
'steamid': self.steamID.as_64,
'include_appinfo': 0,
'include_played_free_games': 0,
'format': 'json'
}
)
if 'games' in data['response']:
#self._n_games_owned = data['response']['game_count']
self._n_games_owned = len(data['response']['games'])
else:
self._n_games_owned = None
return self._n_games_owned
def flags(self):
flags = [
'rank',
'n_played',
'winrate',
'dota_anonymous',
'dota_level',
'steam_anonymous',
'stratz_anmymous',
'stratz_smurf',
'steam_profile_set_up',
'n_friends',
'default_pfp',
'n_games_owned',
]
flags_dict = {flag: getattr(self, flag) for flag in flags}
return flags_dict
def _fetch_stratz(self):
query = (
"{\n"
" player(steamAccountId: %s) {\n"
" firstMatchDate\n"
" lastMatchDate\n"
" matchCount\n"
" names {\n"
" name\n"
" lastSeenDateTime\n"
" }\n"
" steamAccount {\n"
" avatar\n"
" isAnonymous\n"
" isDotaPlusSubscriber\n"
" isStratzAnonymous\n"
" name\n"
" seasonRank\n"
" smurfFlag\n"
" timeCreated\n"
" dotaAccountLevel\n"
" communityVisibleState\n"
" battlepass {\n"
" eventId\n"
" level\n"
" }\n"
" profileUri\n"
" }\n"
" steamAccountId\n"
" winCount\n"
" ranks {\n"
" asOfDateTime\n"
" rank\n"
" seasonRankId\n"
" }\n"
" }\n"
"}\n"
) % (
self.steamID.id
)
response = requests.post(
STRATZ_API, json={'query': query}, auth=BearerAuth(STRATZ_TOKEN))
try:
self.stratz_data = Json2Obj(response.json()['data']['player'])
except Exception as e:
print(e)
raise e
def _fetch_steam(self):
data = steam_webapi.get(
interface='ISteamUser',
method='GetPlayerSummaries',
version=2,
#key=STEAM_TOKEN,
params={
'key': STEAM_TOKEN,
'steamids':self.steamID.as_64
}
)
self.steam_data = Json2Obj(data['response']['players'][0])
def fetch(self):
self._fetch_stratz()
self._fetch_steam()
if __name__ == '__main__':
# Argument parser
parser = argparse.ArgumentParser(description='Detect elements in images')
parser.add_argument('-s', '--stratz_token', required=True,
type=str, help='Stratz API token')
parser.add_argument('-v', '--valve_token', required=True, type=str,
help='Valve API token')
# parse known argguments
args, unknown = parser.parse_known_args()
STRATZ_TOKEN = args.stratz_token
STEAM_TOKEN = args.valve_token
players = [
Profile(95251565), # me
Profile(89428432), # P[A]conar
Profile(1252911151), #some smurf
Profile(76561198853650108), #smurf no PFP
Profile(76561197987953741), #not smurf
]
for p in players:
print(p.stratz_data.steamAccount.name, '|'.join([f'{k}:{v}' for k, v in p.flags().items()]))
#p1_matches = get_matches(95251565)
#p2_matches = get_matches(89428432)
pass