-
Notifications
You must be signed in to change notification settings - Fork 2
/
hon_chart.py
211 lines (135 loc) · 4.68 KB
/
hon_chart.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
import re
#import matplotlib.pyplot
#from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import zipfile
import os
import urllib
import urllib2
data_dir = os.getcwd()+'\\data\\'
while True:
#figsize = 10, 10
plt.figure#(figsize=figsize) #create the graph
while True:
try:
replay_id = raw_input('Enter Match ID: ')
response = urllib2.urlopen('http://www.honrec.com/match/'+replay_id+'/0000000')
break
except Exception,e:
print 'invalid or unindexed match ID, please re-enter or go index it on honrec.com'
continue
while True:
user_input = raw_input('Data Filter ("gold","gold lost","exp"): ')
if user_input == 'exp':
filter_seek = 'experience'
filter_y = 'EXP_EARNED'
break
elif user_input == 'gold':
filter_seek = 'gold'
filter_y = 'GOLD_EARNED'
break
elif user_input == 'gold lost':
filter_seek = 'gold'
filter_y = 'GOLD_LOST'
break
#elif user_input == 'kill':
# filter_seek = None
# filter_y = 'KILL'
# break
else:
print 'Invalid filter'
continue
##DOWNLOAD##
response = urllib2.urlopen('http://www.honrec.com/match/'+replay_id+'/0000000')
html = response.read()
#onclick="download_replay('EUjjCa186/saves/replays/20121102/M104112519.honreplay')">Download Replay<
#print html
#data = re.search('(?<=download_replay\(\').*(?=\'\)\"\>Download Replay)', html)
data = re.search('(?<=download_replay\(\').*(?=\.honreplay\'\)\"\>Download Replay)', html)
#print data.group(0)
download = 'http://replaydl.heroesofnewerth.com/'+data.group(0)+'.zip'
#print download
#response2 = urllib2.urlopen (download)
try:
code = urllib2.urlopen(download)
except Exception,e:
print "The replay is too old or invalid, log not found."
else:
if not os.path.exists(data_dir):
os.makedirs(data_dir)
urllib.urlretrieve(download,data_dir+replay_id+'.z ip')
dest_dir = data_dir
zip_location = data_dir+replay_id+'.zip'
fzip = zipfile.ZipFile(zip_location, 'r')
members = [os.path.join(dest_dir, p) for p in fzip.namelist()]
fzip.extractall(path=dest_dir)
fzip.close()
##DOWNLOAD##
x_series = list()
y_series = list()
with open(data_dir+'m'+replay_id+'.log', 'r') as f:
read_data = f.read().decode(encoding='UTF-16',errors='ignore')
f.closed
match_id = re.search('(?<=TMM Match\s\#)\d*', read_data)
#print 'Match id: '+str(match_id.group(0))
entities = re.findall('player:\d name:".*"', read_data) #list all lines containing uniquely any player number and any player name
#print entities
for i in range(len(entities)):
#print i
c = str(entities[i])
f = open(data_dir+replay_id+'_'+str(i)+'.txt', 'w') #open log
#print c
f.write (str(c+'\n')) #write log
entity = str(re.findall('player:\d', entities[i])).strip('[u]\'') #strip list garbage #from every line containing uniquely any player number and any player name save the player name and the number
#print 'entity: '+str(entity)
#function gold per time
player = re.findall('\n.*'+entity+'.*\r\n', read_data) #from every line select the ones with unique player name and player number
#print player
#print 'player count: '+str(len(player))
add_gold = 0
gold = 0
for i in range(len(player)):
gold_earned = re.findall('(?<=\n)'+filter_y+'.*(?=\r\n)', player[i])#.strip('\n[u]\'') #still working with iteration + converting to str
time = re.findall('(?<=time:)\d*', str(gold_earned)) #find time
if filter_seek == None:
gold = [1 for x in list(time)] # in case there is no y value, the value is 1 per unit in time
else:
gold = re.findall('(?<='+filter_seek+':)\d*', str(gold_earned)) #find filter_y
gold = re.sub(r'[^\w]', ' ', str(gold))
gold = re.sub(r'(\s\s)*', '', str(gold))
time = re.sub(r'[^\w]', ' ', str(time))
time = re.sub(r'(\s\s)*', '', str(time))
if not (re.match(r'(^\s*$)', gold)):
add_gold = add_gold+int(gold)
y_series.append(add_gold) #graph
if not (re.match(r'(^\s*$)', gold)):
time = time
# gold_lost = re.findall('(?<=\n)GOLD_LOST.*(?=\r\n)', player[i])
# gold_lost_amount = re.findall('(?<=gold:)\d*', str(gold_lost))
# if not (re.match(r'(^\s*$)', gold_lost_amount)):
# substract_gold = substract_gold-int(gold_lost_amount)
x_series.append(int(time)/1000) #graph
output = (str(add_gold)+' '+str(time))
#print output
f.write (output+'\n') #write log
#plot data
#plt.axis('tight') #show all data
#plt.axis('image')
plt.plot(x_series, y_series, label=c, linewidth=2.0)
plt.rcParams.update({'legend.labelspacing':0.25})
y_series = list() #empty the list for another round
x_series = list() #empty the list for another round
#add_gold in labels and title
plt.xlabel("Time (seconds)")
plt.ylabel(user_input)
plt.title("Match ID: "+str(match_id.group(0)))
#add limits to the x and y axis
#plt.xlim(0, 6)
#plt.ylim(-5, 80)
#create legend
plt.legend(loc="upper left")
#save figure to png
plt.savefig(data_dir+replay_id+".png")
plt.show()
f.close()
#raise SystemExit