-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_wrapper.py
116 lines (88 loc) · 2.88 KB
/
api_wrapper.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
import json
from sys import stdout
import dota2api
from dota2api.src.exceptions import APIAuthenticationError, APIError
import time
from requests.exceptions import ConnectionError
from scrape_dota_buff import scrape_player, scrape_hero_monthly_stats
# API key can be found here https://steamcommunity.com/dev/apikey
api_key = "78AB51DECA9BAA091DC09D0FEFED65EE"
# random recent match ID to start collecting information from in descending order
latest_match_id = 3933443953
# desired size of data set
data_size = 500
original_data_size = data_size * 1.0
try:
# init key from environment variables
api = dota2api.Initialise()
except APIAuthenticationError:
# init key from api_key variable
api = dota2api.Initialise(api_key=api_key)
try:
with open('data/match.json') as fp:
match_dict = json.load(fp)
except:
match_dict = {}
heroes_dict = api.get_heroes()
items_dict = api.get_game_items()
heroes_stats_dict = scrape_hero_monthly_stats()
# save heroes data
print("[+] Saving heroes data")
with open('data/heroes.json', 'w') as fp:
json.dump(heroes_dict, fp)
# save items data
print("[+] Saving items data")
with open('data/items.json', 'w') as fp:
json.dump(items_dict, fp)
# save hero stats
print("[+] Saving hero monthly stats")
with open('data/hero_monthly_stats.json', 'w') as fp:
json.dump(heroes_stats_dict, fp)
while data_size > 0:
latest_match_id = latest_match_id - 1
percent = 100.0 - ((data_size / original_data_size) * 100)
stdout.write("\r[+] Data Collected (percent): %f " % percent)
stdout.flush()
# Practice matches cannot be queried. If they are then an error is thrown
try:
match = api.get_match_details(match_id=latest_match_id)
match_dict[latest_match_id] = match
data_size -= 1
except APIError:
pass
except ConnectionError:
pass
# save match data
print("\n[+] Saving match data")
with open('data/match.json', 'w') as fp:
json.dump(match_dict, fp)
try:
with open('data/player_stats.json') as fp:
player_stats = json.load(fp)
except:
player_stats = {}
with open('data/match.json') as fp:
match_dict = json.load(fp)
i = 0
print()
for key in match_dict:
player_list = match_dict[key]['players']
for player in player_list:
time.sleep(1)
try:
account_id = player['account_id']
if account_id in player_stats:
pass
else:
player_info = scrape_player(account_id)
player_stats[account_id] = player_info
percent = (i / (len(match_dict) * 10)) * 100
stdout.write("\r[+] Data Scraped (percent): %f " % percent)
stdout.flush()
i += 1
except KeyError:
pass
# save player stats
print("[+] Saving player stats")
with open('data/player_stats.json', 'w') as fp:
json.dump(player_stats, fp)