-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
472 lines (390 loc) · 19.4 KB
/
main.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import collections
try:
from collections import abc
collections.MutableMapping = abc.MutableMapping
collections.Mapping = abc.Mapping
except:
pass
import cfg
import items
import ships
import esipy
from esipy import EsiApp
from esipy import EsiClient
from esipy import EsiSecurity
import operator
from time import sleep
import datetime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread import Cell
app = EsiApp().get_latest_swagger
# Item Id format {Name: item_id}
item_ids = dict()
location_cache = {}
security = EsiSecurity(
redirect_uri='http://localhost:5000/callback',
client_id=cfg.client_id,
secret_key= cfg.secret,
headers={'User-Agent': cfg.agent},
)
esi_client = EsiClient(
retry_requests=True,
headers={'User-Agent': cfg.agent},
security=security
)
security.update_token({
'access_token': '', # leave this empty
'expires_in': -1, # seconds until expiry, so we force refresh anyway
'refresh_token': cfg.refresh_token
})
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
# sheet = client.open("Staging Stocks").get_worksheet(1)
# Extract and print all of the values
# list_of_hashes = sheet.get_all_records()
# print(list_of_hashes)
def get_refresh_token():
print(security.get_auth_uri(state='1234567890', scopes=['esi-markets.structure_markets.v1',
'esi-contracts.read_corporation_contracts.v1',
'esi-universe.read_structures.v1']))
print(security.auth(cfg.auth_code))
def _convert_swagger_dt(dt) -> datetime.datetime:
"""Converts a pyswagger timestamp.
Args:
dt: pyswagger timestamp
Returns:
Python stdlib datetime object
"""
return datetime.datetime.strptime(dt.to_json(), '%Y-%m-%dT%H:%M:%S+00:00')
def check_location(location_id, system_id):
if location_id in location_cache.keys():
return location_cache[location_id] == system_id
if 60000000 < location_id < 64000000:
op = app.op['get_universe_stations_station_id'](
station_id=location_id
)
contents = esi_client.request(op)
station_system = contents.data['system_id']
location_cache[location_id] = station_system
return station_system == system_id
else:
op = app.op['get_universe_structures_structure_id'](
structure_id=location_id
)
contents = esi_client.request(op)
print(contents.data)
structure_system = contents.data['solar_system_id']
location_cache[location_id] = structure_system
return structure_system == system_id
def get_item_ids():
types = open('invTypes.csv', 'r', encoding='utf-8')
lines = types.readlines()
for line in lines:
parts = line.split(",")
if len(parts) > 2:
item_ids[parts[2]] = parts[0]
types.close()
def generate_report(file_name, station_ids, ship_list, item_list, sheet_index, corporation_id, region_id, contracts=True):
staging_ships = dict() # [match, hull match, parts, hull_id, max fittable from market, target quantity]
staging_charges = dict() # [name, local count, local price, jita volume, jita price, target quantity]
staging_parts = dict() # [name, local count, local price, jita volume, jita price, target quantity]
contract_owners = dict()
# Fetch items
for item in item_list:
staging_charges[int(item_ids[item[0]])] = [item[0], 0, 0, 0, 0, item[1]]
# Parse ships
for ship in ship_list:
fitting = open("ships/" + ship[0] + ".txt", 'r', encoding='utf-8')
ship_name = ship[0].strip("[]")
lines = fitting.readlines()
ship_id = int(item_ids[ship_name.split(",")[0]])
staging_ships[ship_name] = [0, 0, {ship_id: 1}, ship_id, -1, ship[1]]
staging_parts[int(item_ids[ship_name.split(",")[0]])] = [ship_name.split(",")[0], 0, 0, 0, 0, 0]
for line in lines[1:]:
if line.strip() in item_ids.keys():
module_id = int(item_ids[line.strip()])
staging_ships[ship_name][2][module_id] = staging_ships[ship_name][2].get(module_id, 0) + 1
staging_parts[module_id] = [line.strip(), 0, 0, 0, 0, 0]
elif line.strip().rsplit(',', 1)[0] in item_ids.keys():
parts = line.strip().rsplit(',', 1)
module_id = int(item_ids[parts[0]])
# charge_id = int(item_ids[parts[1].lstrip(' ')])
# print(charge_id)
staging_ships[ship_name][2][module_id] = staging_ships[ship_name][2].get(module_id, 0) + 1
# staging_ships[ship_name][2][charge_id] = staging_ships[ship_name][2].get(charge_id, 0) + 1
staging_parts[module_id] = [parts[0], 0, 0, 0, 0, 0]
# staging_charges[charge_id] = [parts[1].lstrip(' '), 0, 0, 0, 0]
elif line.strip().rsplit(' ', 1)[0] in item_ids.keys():
item_name = line.strip().rsplit(' ', 1)[0]
count = line.strip().rsplit(' ', 1)[1].strip("x")
charge_id = int(item_ids[item_name])
staging_charges[charge_id] = [item_name, 0, 0, 0, 0, 0]
staging_ships[ship_name][2][charge_id] = staging_ships[ship_name][2].get(charge_id, 0) + int(count)
fitting.close()
# print(staging_parts)
# Get orders in citadel with given items
for station_id in station_ids:
market_results = []
op = app.op['get_markets_structures_structure_id'](
structure_id=station_id,
page=1,
)
res = esi_client.head(op)
if res.status == 200:
number_of_pages = res.header['X-Pages'][0]
# now we know how many pages we want, let's prepare all the requests
operations = []
for page in range(1, number_of_pages+1):
operations.append(
app.op['get_markets_structures_structure_id'](
structure_id=station_id,
page=page,
)
)
market_results = esi_client.multi_request(operations)
for pair in market_results:
for result in pair[1].data:
if not result.get("is_buy_order") and result.get("type_id") in staging_charges.keys():
staging_charges[result.get("type_id")][1] += result.get("volume_remain")
current_price = staging_charges[result.get("type_id")][2]
if current_price == 0 or current_price > result.get("price"):
staging_charges[result.get("type_id")][2] = result.get("price")
if not result.get("is_buy_order") and result.get("type_id") in staging_parts.keys():
staging_parts[result.get("type_id")][1] += result.get("volume_remain")
current_price = staging_parts[result.get("type_id")][2]
if current_price == 0 or current_price > result.get("price"):
staging_parts[result.get("type_id")][2] = result.get("price")
# print(staging_parts)
# Fetch Jita prices and volumes for comparison
market_results = []
op = app.op['get_markets_region_id_orders'](
region_id=10000002,
order_type="sell",
page=1,
)
res = esi_client.head(op)
if res.status == 200:
number_of_pages = res.header['X-Pages'][0]
# print(number_of_pages)
# now we know how many pages we want, let's prepare all the requests
operations = []
for page in range(1, number_of_pages + 1):
operations.append(
app.op['get_markets_region_id_orders'](
region_id=10000002,
order_type="sell",
page=page,
)
)
market_results = esi_client.multi_request(operations)
# print(market_results)
for pair in market_results:
for result in pair[1].data:
# print(result)
if result.get("type_id") in staging_charges.keys():
staging_charges[result.get("type_id")][3] += result.get("volume_remain")
current_price = staging_charges[result.get("type_id")][4]
if current_price == 0 or current_price > result.get("price"):
staging_charges[result.get("type_id")][4] = result.get("price")
if result.get("type_id") in staging_parts.keys():
staging_parts[result.get("type_id")][3] += result.get("volume_remain")
current_price = staging_parts[result.get("type_id")][4]
if current_price == 0 or current_price > result.get("price"):
staging_parts[result.get("type_id")][4] = result.get("price")
# Determine how many ships can be fit from what's on market
for ship in staging_ships.keys():
for item in staging_ships[ship][2].keys():
if item in staging_parts.keys() and staging_parts[item][1] > 0:
if staging_ships[ship][4] == -1 or \
staging_parts[item][1] / staging_ships[ship][2][item] < staging_ships[ship][4]:
staging_ships[ship][4] = int(staging_parts[item][1] / staging_ships[ship][2][item])
elif item in staging_charges.keys() and staging_charges[item][1] > 0:
if staging_ships[ship][4] == -1 or \
staging_charges[item][1] / staging_ships[ship][2][item] < staging_ships[ship][4]:
staging_ships[ship][4] = int(staging_charges[item][1] / staging_ships[ship][2][item])
else:
staging_ships[ship][4] = 0
# Determine target number of modules and charges based on target quantities
for ship in staging_ships.keys():
for item in staging_ships[ship][2].keys():
if item in staging_parts.keys():
staging_parts[item][5] = staging_parts[item][5] + (staging_ships[ship][2][item] * staging_ships[ship][5])
if item in staging_charges.keys():
staging_charges[item][5] = staging_charges[item][5] + (staging_ships[ship][2][item] * staging_ships[ship][5])
if contracts:
# Get corp/alliance contracts with ships
contract_results = []
op = app.op['get_corporations_corporation_id_contracts'](
corporation_id=corporation_id,
page=1,
)
res = esi_client.head(op)
if res.status == 200:
number_of_pages = res.header['X-Pages'][0]
# now we know how many pages we want, let's prepare all the requests
operations = []
for page in range(1, number_of_pages+1):
operations.append(
app.op['get_corporations_corporation_id_contracts'](
corporation_id=corporation_id,
page=page,
)
)
contract_results = esi_client.multi_request(operations)
# print(contract_results)
for pair in contract_results:
for result in pair[1].data:
if result.get("status") == "outstanding" and result.get("type") == "item_exchange" \
and result.get("start_location_id") in station_ids and \
_convert_swagger_dt(result.get("date_expired")) > datetime.datetime.utcnow():
sleep(.5)
op = app.op['get_corporations_corporation_id_contracts_contract_id_items'](
contract_id=result.get("contract_id"),
corporation_id=corporation_id,
)
# print(result)
contract_owners[result.get("contract_id")] = [result.get("title"), result.get("issuer_id")]
while(True):
try:
contents = esi_client.request(op)
except:
sleep(3)
continue
break
contract_types = list()
for item in contents.data:
contract_types.append(item.get("type_id"))
for ship in staging_ships.keys():
if all(item in contract_types for item in staging_ships[ship][2].keys()):
staging_ships[ship][0] += 1
elif staging_ships[ship][3] in contract_types:
staging_ships[ship][1] += 1
# Get public contracts with ships
contract_results = []
op = app.op['get_contracts_public_region_id'](
region_id=region_id,
page=1,
)
res = esi_client.head(op)
if res.status == 200:
number_of_pages = res.header['X-Pages'][0]
# now we know how many pages we want, let's prepare all the requests
operations = []
for page in range(1, number_of_pages + 1):
operations.append(
app.op['get_contracts_public_region_id'](
region_id=region_id,
page=page,
)
)
contract_results = esi_client.multi_request(operations)
# print(contract_results)
for pair in contract_results:
for result in pair[1].data:
if result.get("type") == "item_exchange" \
and result.get("start_location_id") in station_ids and \
_convert_swagger_dt(result.get("date_expired")) > datetime.datetime.utcnow():
sleep(.5)
op = app.op['get_contracts_public_items_contract_id'](
contract_id=result.get("contract_id"),
)
# print(result)
contract_owners[result.get("contract_id")] = [result.get("title"), result.get("issuer_id")]
while (True):
try:
contents = esi_client.request(op)
except:
sleep(3)
continue
break
contract_types = list()
for item in contents.data:
contract_types.append(item.get("type_id"))
for ship in staging_ships.keys():
if all(item in contract_types for item in staging_ships[ship][2].keys()):
staging_ships[ship][0] += 1
elif staging_ships[ship][3] in contract_types:
staging_ships[ship][1] += 1
# print(staging_ships)
# Write to outfile and google sheets
out_file = open("output/" + file_name, 'w')
# Ships
sheet = client.open("Staging Stocks").get_worksheet(sheet_index)
sheet.update_cell(1, 8, str(datetime.datetime.utcnow()))
cell_list = []
row = 2
out_file.write("\n\n\nShip Name,Number Found,Hull Match Only, Fits On Market\n")
for key in staging_ships:
out_file.write(key.strip(",") + "," + str(staging_ships[key][0]) + "," + str(staging_ships[key][1]) +
str(staging_ships[key][4]) + "\n")
cell_list.append(Cell(row=row, col=1, value=key))
cell_list.append(Cell(row=row, col=2, value=int(staging_ships[key][0])))
cell_list.append(Cell(row=row, col=3, value=int(staging_ships[key][1])))
cell_list.append(Cell(row=row, col=4, value=int(staging_ships[key][4])))
cell_list.append(Cell(row=row, col=5, value=int(staging_ships[key][5])))
row += 1
sheet.update_cells(cell_list)
# Charges
sheet = client.open("Staging Stocks").get_worksheet(sheet_index + 1)
sheet.update_cell(1, 11, str(datetime.datetime.utcnow()))
cell_list = []
row = 2
out_file.write("Item Name,Local Volume,Local Price,Jita Volume,Jita Price\n")
for key in sorted(staging_charges.keys()):
out_file.write(str(staging_charges[key][0]) + "," + str(staging_charges[key][1]) + str(staging_charges[key][2])
+ str(staging_charges[key][3]) + str(staging_charges[key][4]) + "\n")
cell_list.append(Cell(row=row, col=1, value=str(staging_charges[key][0])))
cell_list.append(Cell(row=row, col=2, value=int(staging_charges[key][1])))
cell_list.append(Cell(row=row, col=3, value=int(staging_charges[key][2])))
cell_list.append(Cell(row=row, col=4, value=int(staging_charges[key][3])))
cell_list.append(Cell(row=row, col=5, value=int(staging_charges[key][4])))
cell_list.append(Cell(row=row, col=7, value=int(staging_charges[key][5])))
row += 1
sheet.update_cells(cell_list)
# Parts
sheet = client.open("Staging Stocks").get_worksheet(sheet_index + 2)
sheet.update_cell(1, 11, str(datetime.datetime.utcnow()))
cell_list = []
row = 2
out_file.write("Item Name,Local Volume,Local Price,Jita Volume,Jita Price\n")
for key in sorted(staging_parts.keys()):
out_file.write(str(staging_parts[key][0]) + "," + str(staging_parts[key][1]) + str(staging_parts[key][2])
+ str(staging_parts[key][3]) + str(staging_parts[key][4]) + "\n")
cell_list.append(Cell(row=row, col=1, value=str(staging_parts[key][0])))
cell_list.append(Cell(row=row, col=2, value=int(staging_parts[key][1])))
cell_list.append(Cell(row=row, col=3, value=int(staging_parts[key][2])))
cell_list.append(Cell(row=row, col=4, value=int(staging_parts[key][3])))
cell_list.append(Cell(row=row, col=5, value=int(staging_parts[key][4])))
cell_list.append(Cell(row=row, col=7, value=int(staging_parts[key][5])))
row += 1
sheet.update_cells(cell_list)
out_file.write("\n\n\nContract ID,Contract Name,Contract Owner\n")
for key in contract_owners:
try:
out_file.write(str(key) + "," + str(contract_owners[key][0]) + "," + str(contract_owners[key][1]) + "\n")
except:
out_file.write(str(key) + "," + "BAD NAME" + "," + str(contract_owners[key][1]) + "\n")
out_file.close()
# print(contract_owners)
def main():
# get_refresh_token()
print("Connection established")
get_item_ids()
print("Item ids fetched")
# generate_report("elanoda.csv", [1040246076254], ships.elanoda, items.elanoda, 4, 1018389948, 10000016)
# generate_report("enaluri.csv", [60015068], ships.enaluri, items.enaluri, 2, 1018389948, 10000069)
# generate_report("UMI_KK.csv", [1036351551330], ships.preds, items.preds, 0, 1018389948, 10000010)
generate_report("3T7_M8.csv", [1043621617719], ships._3T7_M8, items._3T7_M8, 0, 1018389948, 10000035, contracts=False)
# generate_report("F4R2_Q.csv", [1044008398262], ships.F4R2_Q, items.F4R2_Q, 3, 1018389948, 10000014)
# generate_report("5ZXX_K.csv", [1038708751029, 1039071618828], ships._5ZXX_K, items._5ZXX_K, 4, 1018389948, 10000023)
# print(check_location(60012580, 30002005))
# print(check_location(1037022287754, 30002005))
# print(check_location(1038708751029, 30002005))
# print(location_cache)
if __name__ == "__main__":
main()