This repository has been archived by the owner on Mar 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_rawData.py
191 lines (157 loc) · 5.59 KB
/
fetch_rawData.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 7 16:19:50 2020
@author: Karl Roush and Elijah Smith
"""
import cassiopeia as cass
import json
from datetime import timedelta
from pathlib import Path
from cassiopeia import Summoner, Match
from cassiopeia.data import Season, Queue
import time
def getAPI_key():
#reads the API key from local file
file= open("../api_key.txt","r")
return file.read()
def make_matchID_list(match_history):
#converts t
matchID_list=[]
for item in match_history:
matchID_list.append(item.id)
return matchID_list
def init_playerData(data_fileName, matchStats):
with open(data_fileName, "w") as outfile:
json.dump(matchStats, outfile)
outfile.close()
def getStats(match, summoner):
# https://readthedocs.org/projects/cassiopeia/downloads/pdf/latest/
p = match.participants[summoner]
# End of game stats
endGame = p.stats
# #maybe some kind of vision score weighted by minute? (should be exponential w/ gametime)
# Timeline stats; see page 43 of docs
# currently only looking at 0-10 min mark
timeData = p.timeline
try:
cs_per_min = timeData.creeps_per_min_deltas['0-10']
except:
cs_per_min = None
try:
csd_per_min = timeData.cs_diff_per_min_deltas['0-10']
except:
csd_per_min = None
try:
dmgDiff_per_min = timeData.damage_taken_diff_per_min_deltas['0-10']
except:
dmgDiff_per_min = None
try:
xpDiff_per_min= timeData.xp_diff_per_min_deltas['0-10']
except:
xpDiff_per_min = None
match_stats= {}
try:
match_stats['MatchID'] = match.id
except:
match_stats['MatchID'] = None
try:
match_stats['gold_earned'] = endGame.gold_earned
except:
match_stats['gold_earned'] = None
try:
match_stats['gold_spent'] = endGame.gold_spent
except:
match_stats['gold_spent'] = None
try:
match_stats['total_damage'] = endGame.total_damage_dealt
except:
match_stats['total_damage'] = None
try:
match_stats['total_damage_champs'] = endGame.total_damage_dealt_to_champions
except:
match_stats['total_damage_champs'] = None
try:
match_stats['vision_score'] = endGame.vision_score
except:
match_stats['vision_score'] = None
try:
match_stats['Win'] = endGame.win
except:
match_stats['Win'] = None
match_stats['cs_per_min'] = cs_per_min
match_stats['csd_per_min'] = csd_per_min
match_stats['dmgDiff_per_min'] = dmgDiff_per_min
match_stats['xpDiff_per_min'] = xpDiff_per_min
return match_stats
def getPlayerData(summoner, player_name, player_region):
### GET THE MATCH HISTORY
# for soloQ
match_history = summoner.match_history(queues={cass.Queue.ranked_solo_fives})
matchID_list= make_matchID_list(match_history)
### CREATING FILE TO SAVE PLAYER'S STATS
data_fileName= player_name + "_matchStats.json"
new_matchStats= {
"MatchID": [],
"gold_earned": [],
"gold_spent": [],
"total_damage": [],
"total_damage_champs": [],
"vision_score": [],
"Win": [],
"cs_per_min": [],
"csd_per_min": [],
"dmgDiff_per_min": [],
"xpDiff_per_min": []
}
checkfile=Path("./"+data_fileName)
if not checkfile.is_file():
print("\nPlayer stats file does not exist.")
print("Creating initial stats file...")
init_playerData(data_fileName, new_matchStats)
# old match stats
with open(data_fileName, 'r') as openfile:
past_stats = json.load(openfile)
openfile.close()
### BEGIN ANALYZING MATCHES IN MATCH HISTORY
try:
last_analyzed_matchID= past_stats["MatchID"][0]
except:
last_analyzed_matchID= None
#need to prepend stats so that the newest match stats are first in the list!
for match in match_history:
if (match.id == last_analyzed_matchID) or (match.id== match_history[150].id):
# only want new matches, limited to the newest 150
break
elif match.duration < timedelta(minutes=3, seconds= 15):
# skip remakes
pass
else:
# grab the stats from each match
match_stats= getStats(match, summoner)
for key, value in match_stats.items():
#append the match stats to the new_matchStats dictionary
new_matchStats[key].append(value)
### APPEND THE STATS FROM THE NEW MATCHES TO FILE
for key, value in new_matchStats.items():
# combined=itertools.chain(new_stats[key], saved_stats[key])
combined= new_matchStats[key]+past_stats[key]
past_stats[key]= combined
with open(data_fileName, 'w') as output_data_file:
json.dump(past_stats, output_data_file)
output_data_file.close()
if __name__ == '__main__':
start_time = time.time()
#%% INITIALIZATION
cass.set_riot_api_key(getAPI_key()) #or replace with your own api key
cass.set_default_region("NA") #or replace with another region
#cache is not actually used
with open('cache.json', 'r') as cache_file:
cache = json.load(cache_file)
cache_file.close()
#%% SETTING THE PLAYER TO BE ANALYZED
player_name= "C9 Zven"
player_region= "NA"
summoner = Summoner(name=player_name, region=player_region)
#%% GET THE PLAYER DATA
getPlayerData(summoner, player_name, player_region)
print("--- %s seconds ---" % (time.time() - start_time))