-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesv.py
More file actions
294 lines (231 loc) · 11.3 KB
/
esv.py
File metadata and controls
294 lines (231 loc) · 11.3 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
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
289
290
291
292
293
294
from datetime import datetime
from flask import Flask, render_template, flash, request, redirect, url_for, jsonify
from flaskext.mysql import MySQL
from preston_new import Preston
import time
import yaml
application = Flask(__name__)
mysql = MySQL()
config = yaml.safe_load(open('config.conf', 'r'))
application.config['SECRET_KEY'] = config['SECRET_KEY']
application.config['MYSQL_DATABASE_USER'] = config['MYSQL_CONFIG']['MYSQL_DATABASE_USER']
application.config['MYSQL_DATABASE_PASSWORD'] = config['MYSQL_CONFIG']['MYSQL_DATABASE_PASSWORD']
application.config['MYSQL_DATABASE_DB'] = config['MYSQL_CONFIG']['MYSQL_DATABASE_DB']
application.config['MYSQL_DATABASE_HOST'] = config['MYSQL_CONFIG']['MYSQL_DATABASE_HOST']
mysql.init_app(application)
@application.route('/')
def index():
return redirect('/esv')
@application.route('/esv')
@application.route('/esv/')
def landing():
preston = Preston(
user_agent=config['EVE_OAUTH_USER_AGENT'],
client_id=config['EVE_OAUTH_CLIENT_ID'],
client_secret=config['EVE_OAUTH_SECRET'],
callback_url=config['EVE_OAUTH_CALLBACK'],
scope=config['EVE_OAUTH_SCOPE']
)
return render_template('dist/view.html',
show_crest=True,
crest_url=preston.get_authorize_url(),
network_time=0,
parse_time=0,
base_url=config['BASE_URL'])
@application.route('/esv/view')
@application.route('/esv/view/<refresh_token>')
def view_pilot(refresh_token=None):
try:
if refresh_token is None:
# check response
if 'error' in request.path:
flash("There was an error in EVE's response", 'error')
return redirect(url_for('landing'))
try:
preston = Preston(
user_agent=config['EVE_OAUTH_USER_AGENT'],
client_id=config['EVE_OAUTH_CLIENT_ID'],
client_secret=config['EVE_OAUTH_SECRET'],
callback_url=config['EVE_OAUTH_CALLBACK'],
scope=config['EVE_OAUTH_SCOPE']
)
auth = preston.authenticate(request.args['code'])
except Exception as e:
print('SSO callback exception: ' + str(e))
flash('There was an error signing you in.', 'error')
return redirect(url_for('landing'))
return redirect(url_for('view_pilot', refresh_token=auth.refresh_token))
else:
preston = Preston(
user_agent=config['EVE_OAUTH_USER_AGENT'],
client_id=config['EVE_OAUTH_CLIENT_ID'],
client_secret=config['EVE_OAUTH_SECRET'],
callback_url=config['EVE_OAUTH_CALLBACK'],
scope=config['EVE_OAUTH_SCOPE'],
refresh_token=refresh_token
)
pilot_info = preston.whoami()
pilot_name = pilot_info['CharacterName']
pilot_id = pilot_info['CharacterID']
except Exception as e:
flash('There was an error parsing skills: ' + str(e), 'error')
return redirect(url_for('landing'))
return render_template('dist/view.html',
show_crest=False,
pilot_name=pilot_name,
pilot_id=pilot_id,
base_url=config['BASE_URL'],
refresh_token=refresh_token)
@application.route('/esv/get_skills/<refresh_token>')
def get_skills(refresh_token):
try:
conn = mysql.connect()
cursor = conn.cursor()
except Exception as e:
msg = "MySQL Error"
return get_json_response(e, msg)
try:
t0 = time.time()
preston = Preston(
user_agent=config['EVE_OAUTH_USER_AGENT'],
client_id=config['EVE_OAUTH_CLIENT_ID'],
client_secret=config['EVE_OAUTH_SECRET'],
callback_url=config['EVE_OAUTH_CALLBACK'],
scope=config['EVE_OAUTH_SCOPE'],
refresh_token=refresh_token
)
pilot_info = preston.whoami()
character_id = pilot_info['CharacterID']
except Exception as e:
msg = "Authentication Error"
return get_json_response(e, msg)
try:
# First, hit ESI for all the info we need
skills_response = preston.get_op('get_characters_character_id_skills', character_id=character_id)
skillqueue_response = preston.get_op('get_characters_character_id_skillqueue', character_id=character_id)
implants_response = preston.get_op('get_characters_character_id_implants', character_id=character_id)
attributes_response = preston.get_op('get_characters_character_id_attributes', character_id=character_id)
t1 = time.time()
network_time = t1 - t0
t0 = time.time()
skills = skills_response['skills']
skill_ids = [skill.get('skill_id') for skill in skills]
query = "SELECT typeID, typeName FROM invTypes WHERE invTypes.typeID IN ({})" \
.format(','.join(str(i) for i in skill_ids))
cursor.execute(query)
skill_names = dict(cursor.fetchall())
query = "SELECT groupName, GROUP_CONCAT(DISTINCT typeID) " \
"FROM invTypes " \
"LEFT JOIN invGroups ON invGroups.groupID = invTypes.groupID " \
"WHERE invTypes.groupID IN " \
"(SELECT groupID from invGroups where categoryID = {}) " \
"GROUP BY groupName".format(config['CATEGORY_SKILLS'])
cursor.execute(query)
skill_groups = dict(cursor.fetchall())
skill_groups = {skill_group: skill_ids_csv.split(',') for skill_group, skill_ids_csv in skill_groups.items()}
for implant_id in implants_response:
# For each of the implants installed, we need to bump our character's attributes to figure out the SP/hour
query = "SELECT invTypes.typeID, " \
"dgmTypeAttributes.attributeID, " \
"dgmTypeAttributes.valueInt, " \
"dgmAttributeTypes.attributeName " \
"FROM invtypes " \
"LEFT JOIN dgmtypeattributes ON invtypes.typeID = dgmtypeattributes.typeID " \
"LEFT JOIN dgmAttributeTypes ON dgmAttributeTypes.attributeID = dgmTypeAttributes.attributeID " \
"WHERE dgmTypeAttributes.attributeID BETWEEN 175 AND 179 " \
"AND invtypes.typeID = {}".format(implant_id)
cursor.execute(query)
attribute_modifiers = cursor.fetchall()
for attribute in attribute_modifiers:
attribute_key = attribute[3][:-5] # Chop off the Bonus
attributes_response[attribute_key] += attribute[2]
t1 = time.time()
query_time = t1 - t0
except Exception as e:
msg = "Error fetching skills"
return get_json_response(e, msg)
try:
t0 = time.time()
skills_stats = {}
skills_dict = {}
skills_stats['Totals'] = {}
skills_stats['Totals']['num_skills'] = len(skills)
skills_stats['Totals']['total_sp'] = 0
# Parse skills into dicts for the html response
for group, child_skills in skill_groups.items():
child_skill_ints = [int(i) for i in child_skills]
for skill in skills:
if skill['skill_id'] in child_skill_ints:
# Prevents dict errors by setting child items to {} first
if group not in skills_dict:
skills_dict[group] = {}
skills_stats[group] = {}
skills_stats[group]['skills_in_group'] = 0
skills_stats[group]['sp_in_group'] = 0
# Add skill and metadata to dicts
skill_id = skill['skill_id']
skill_name = skill_names[skill_id]
skill_level_trained = skill['active_skill_level']
skills_dict[group][skill_name] = skill_level_trained
skills_stats[group]['skills_in_group'] += 1
skills_stats[group]['sp_in_group'] += skill['skillpoints_in_skill']
skills_stats['Totals']['total_sp'] += skill['skillpoints_in_skill']
except Exception as e:
msg = "Error parsing skills"
return get_json_response(e, msg)
try:
skill_count = len(skillqueue_response)
current_skill_fmt = {}
total_time = utcnow = datetime.utcnow()
if skill_count > 0:
fmt = '%Y-%m-%dT%H:%M:%SZ'
current_skill = skillqueue_response[0]
utcnow_fmt = datetime.strftime(utcnow, fmt)
level_start_sp = current_skill['level_start_sp']
level_end_sp = current_skill['level_end_sp']
training_start_sp = current_skill['training_start_sp']
completed_pct = ((training_start_sp - level_start_sp) / (level_end_sp - level_start_sp)) * 100
if current_skill['finish_date']:
while current_skill['finish_date'] < utcnow_fmt:
current_skill = skillqueue_response[1]
skill_count = len(skillqueue_response) - 1
skillqueue_response.pop(0)
finish_datetime = datetime.strptime(current_skill['finish_date'], fmt)
start_datetime = datetime.strptime(current_skill['start_date'], fmt)
current_skill_fmt['finish_datetime'] = datetime.strftime(finish_datetime, '%Y-%m-%d %H:%M:%S')
completed_pct = (utcnow - start_datetime) / (finish_datetime - start_datetime) * 100
sp_per_hour = (level_end_sp - level_start_sp) / (finish_datetime - start_datetime).total_seconds() / 3600
skill_name = skill_names[current_skill['skill_id']]
current_skill_fmt['completed_pct'] = completed_pct
current_skill_fmt['skill_name'] = skill_name
current_skill_fmt['skill_level'] = current_skill['finished_level']
for skill in skillqueue_response:
total_time += datetime.strptime(skill['finish_date'], fmt) - max(
datetime.strptime(skill['start_date'], fmt),
utcnow
)
total_time = str(total_time).split('.')[0]
t1 = time.time()
parse_time = t1 - t0
except Exception as e:
msg = "Error parsing skill queue"
return get_json_response(e, msg)
return jsonify({
'success': True,
'payload': render_template('dist/ajax.html',
skills=skills_dict,
skills_stats=skills_stats,
network_time=network_time,
parse_time=parse_time,
base_url=config['BASE_URL'],
current_skill=current_skill_fmt,
skill_count=skill_count,
total_time=total_time)
})
def get_json_response(e, msg):
return jsonify({
'message': "{}: {}".format(msg, e),
'success': False
})
if __name__ == "__main__":
application.run(host='localhost', debug=True, ssl_context=("localhost.crt", "localhost.key"))