-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
255 lines (209 loc) · 9.68 KB
/
main.py
File metadata and controls
255 lines (209 loc) · 9.68 KB
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
#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
import datetime
import io
import os
import re
import requests
from bs4 import BeautifulSoup
from user_agent import generate_user_agent
import json
dir_path = '/root/Dota_stats'
# Parsing site for info
def get_steam_info(steam_url):
id_url = f"https://steamid.io/lookup/{steam_url.split('/')[4]}"
response = requests.get(url=id_url, headers={'user-agent': f'{generate_user_agent()}'})
soup = BeautifulSoup(response.text, 'lxml')
steam32ID = soup.find_all("dd", {"class": "value short"})[1].find("a")
steam32ID = re.split(r":|]", steam32ID.text)[-2]
nickname = soup.find_all("dd", {"class": "value"})[6].text
return steam32ID, nickname
def get_All_match_info(player_id):
AllMatchInfo_url = f'https://api.opendota.com/api/players/{player_id}/matches?limit=100'
AllMatchInfo_response = requests.get(AllMatchInfo_url)
AllMatchInfo = json.loads(AllMatchInfo_response.text)
AllMatchInfo_result = []
with open(os.path.join(dir_path, "json/ALMatchInfo.json"), "w+", encoding="utf-8") as f:
for i in AllMatchInfo:
# Heroes
with open(os.path.join(dir_path, "json/heroes.json"), "r") as heroes:
heroes = json.load(heroes)
for index in heroes:
if index["id"] == i["hero_id"]:
hero = index["localized_name"]
hero_name = index["name"]
# lobby_type
with open(os.path.join(dir_path, "json/lobby_type.json"), "r") as lobbyType:
lobbyType = json.load(lobbyType)
lobby_type = lobbyType[f"{i['lobby_type']}"]["name"].split("_")
lobby_type.remove("lobby")
lobby_type.remove('type')
# Match_result
if i['radiant_win'] and i['player_slot'] < 128:
match_result = 'Win'
if i['radiant_win'] and i['player_slot'] > 127:
match_result = 'Lose'
if i['radiant_win'] == False and i['player_slot'] > 127:
match_result = 'Win'
if i['radiant_win'] == False and i['player_slot'] < 128:
match_result = 'Lose'
# skill
if i['skill'] == 1:
skill = "Normal"
elif i['skill'] == 2:
skill = "High skill"
elif i['skill'] == 3:
skill = " Very high skill"
else:
skill = ''
AllMatchInfo_result.append({
"assists": i['assists'],
"deaths": i['deaths'],
'duration': str(datetime.timedelta(seconds=i["duration"])),
"game_mode": i['game_mode'],
"hero": hero,
"hero_name": hero_name,
"kills": i['kills'],
"leaver_status": i['leaver_status'],
"lobby_type": ' '.join(w[0].upper() + w[1:] for w in lobby_type),
"match_id": i['match_id'],
"party_size": i['party_size'],
"player_slot": i['player_slot'],
"radiant_win": i['radiant_win'],
"match_result": match_result,
"skill": skill,
'start_time': datetime.datetime.fromtimestamp(i["start_time"]).strftime('%d %B %Y %H:%M:%S'),
"version": i['version']
})
f.write(json.dumps(AllMatchInfo_result, sort_keys=True, indent=4, ensure_ascii=False))
return AllMatchInfo[0]["match_id"]
def get_match_info(match_id):
matchInfo_url = f'https://api.opendota.com/api//matches/{match_id}'
matchInfo_response = requests.get(matchInfo_url)
matchInfo = json.loads(matchInfo_response.text)
with open(os.path.join(dir_path, "json/matchInfo.json"), "w+", encoding="utf-8") as f:
f.write(json.dumps(matchInfo, sort_keys=True, indent=4, ensure_ascii=False))
def get_heroes():
heroes_url = f'https://api.opendota.com/api/heroes'
heroes_response = requests.get(heroes_url)
heroes = json.loads(heroes_response.text)
with open(os.path.join(dir_path, "json/heroes.json"), "w+", encoding='utf-8') as f:
f.write(json.dumps(heroes, sort_keys=True, indent=4, ensure_ascii=False))
def get_steam_profile(player_id):
steamProfile_url = f'https://api.opendota.com/api/players/{player_id}'
steamProfile_response = requests.get(steamProfile_url)
steamProfile = json.loads(steamProfile_response.text)
return steamProfile
def get_wl_profile(player_id):
wl_url = f'https://api.opendota.com/api/players/{player_id}/wl'
wl_response = requests.get(wl_url)
wl = json.loads(wl_response.text)
return wl
# Work with API and writing to files
def get_data(steam_url):
player_id = get_steam_info(steam_url)[0]
get_All_match_info(player_id)
get_heroes()
steamProfile = get_steam_profile(player_id)
with open(os.path.join(dir_path, "json/SteamProfile.json"), "w+", encoding='utf-8') as f:
f.write(json.dumps(steamProfile, sort_keys=True, indent=4))
wl = get_wl_profile(player_id)
with open(os.path.join(dir_path, "json/wl.json"), "w+", encoding='utf-8') as f:
f.write(json.dumps(wl, sort_keys=True, indent=4))
def short_info_profile():
profile_list = []
with open(os.path.join(dir_path, "json/ProfileInfo.json"), "w+", encoding='utf-8') as GameProfileInfo:
with open(os.path.join(dir_path, "json/SteamProfile.json"), "r", encoding='utf-8') as SteamProfile:
SteamProfile = json.load(SteamProfile)
with open(os.path.join(dir_path, "json/wl.json"), "r", encoding='utf-8') as wl:
wl = json.load(wl)
if f"/static/images/rank_star/{list(str(SteamProfile['rank_tier']))[1]}.png" != "0":
rank_star = f"/static/images/rank_star/{list(str(SteamProfile['rank_tier']))[1]}.png"
rank_icon = f"/static/images/rank_icon/{list(str(SteamProfile['rank_tier']))[0]}.png"
else:
rank_icon = f"/static/images/rank_icon/{list(str(SteamProfile['rank_tier']))[0]}.png"
rank_star = ""
profile_list.append({
'win': wl["win"],
'lose': wl["lose"],
'winrate': round(wl["win"] / ((wl["win"] + wl["lose"]) / 100), 2),
'avatar': SteamProfile['profile']['avatarfull'],
'personaname': SteamProfile['profile']['personaname'],
'profileurl': SteamProfile['profile']['profileurl'],
'rank_icon': rank_icon,
'rank_star': rank_star
})
GameProfileInfo.write(json.dumps(profile_list, sort_keys=True, indent=4))
# Parsing info from json files
def create_result():
with open(os.path.join(dir_path, "json/matchInfo.json"), "r", encoding='utf-8') as f:
f = json.load(f)
result = []
for i in f["players"]:
# Steam info
try:
steam = get_steam_profile(i["account_id"])
avatar_medium = steam["profile"]["avatarmedium"]
profileurl = steam["profile"]["profileurl"]
nickname = i["personaname"]
except:
nickname = "?"
avatar_medium = ""
profileurl = ''
# if str(i["account_id"]) == get_steam_info(steam_url)[0]:
with open(os.path.join(dir_path, "json/heroes.json"), "r") as heroes:
heroes = json.load(heroes)
for b in heroes:
if b["id"] == i["hero_id"]:
hero = b["localized_name"]
hero_localized_name = b["name"].split("npc_dota_hero_")[1]
hero_png = f"https://cdn.dota2.com/apps/dota2/images/heroes/{hero_localized_name}_sb.png"
# Match result
if i["win"] == 1:
match_result = "Won"
else:
match_result = "Loss"
# Side
if i["isRadiant"]:
side = "Radiant"
else:
side = "Dire"
result.append({
'match_id': i["match_id"],
'profileurl': profileurl,
'nickname': nickname,
'match_result': match_result,
'avatar': avatar_medium,
'kills': i["kills"],
'deaths': i["deaths"],
'assists': i["assists"],
'level': i["level"],
'xp_per_min': i["xp_per_min"],
'gold_per_min': i["gold_per_min"],
'total_value': i["total_gold"],
'hero_damage': i["hero_damage"],
'hero_healing': i["hero_healing"],
# 'party_size': i["party_size"],
'tower_damage': i["tower_damage"],
'last_hits': i["last_hits"],
'denies': i["denies"],
'CPM': round(i["last_hits"] / (i["duration"] / 60), 1),
'KDA': i["kda"],
'hero': hero,
'hero_png': hero_png,
# 'lane_role': lane_role,
'side': side,
'dire_score': f["dire_score"],
'radiant_score': f["radiant_score"],
'radiant_win': f["radiant_win"],
'duration': str(datetime.timedelta(seconds=i["duration"])),
'start_time': datetime.datetime.fromtimestamp(i["start_time"]).strftime('%d %B %Y %H:%M:%S'),
})
with io.open(os.path.join(dir_path, "json/result.json"), "w+", encoding='utf-8') as f:
f.write(json.dumps(result, ensure_ascii=False, sort_keys=True, indent=4))
def Main(steam_url):
get_data(steam_url)
short_info_profile()
if __name__ == "__main__":
# Main(sys.argv[1]) # Take first parametr from autostart.sh
Main("https://steamcommunity.com/profiles/76561198092347401/")