-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathq_assets.py
246 lines (219 loc) · 12.5 KB
/
q_assets.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
""" Q.Assets (desktop/mobile)
Prerequisites:
* Have a Python 3 environment available to you (possibly by using a
virtual environment: https://virtualenv.pypa.io/en/stable/).
* Run pip install -r requirements.txt --user with this directory.
or
Run pip install -r requirements.txt with this directory as your root.
* Copy q_industrialist_settings.py.template into q_industrialist_settings.py and
mood for your needs.
* Create an SSO application at developers.eveonline.com with the scopes
from g_client_scope list declared in q_industrialist_settings.py and the
callback URL "https://localhost/callback/".
Note: never use localhost as a callback in released applications.
To run this example, make sure you have completed the prerequisites and then
run the following command from this directory as the root:
$ chcp 65001 & @rem on Windows only!
$ python eve_sde_tools.py --cache_dir=~/.q_industrialist
$ python q_assets.py --pilot="Qandra Si" --pilot="Your Name" --online --cache_dir=~/.q_industrialist
Requires application scopes:
* esi-assets.read_corporation_assets.v1 - Requires role(s): Director
* esi-universe.read_structures.v1 - Requires: access token
"""
import sys
import json
import requests
import eve_esi_interface as esi
import eve_esi_tools
import eve_sde_tools
import console_app
import render_html_assets
import q_industrialist_settings
from __init__ import __version__
# type_id - тип требуемых данных
# * None : корневой, т.е. данные по текущей локации маршрута циносети
# * <number> : значение type_id, поиск которого осуществляется (солнечная система, или топляк)
# при type_id > 0 поиск осуществляется вверх по дереву
# при type_id < 0 поиск осуществляется вниз по дереву
def get_cyno_solar_system_details(location_id, corp_assets_tree, subtype=None):
if not (str(location_id) in corp_assets_tree):
return None
loc_dict = corp_assets_tree[str(location_id)]
if subtype is None:
if "location_id" in loc_dict: # иногда ESI присылает содержимое контейнеров, которые исчезают из ангаров, кораблей и звёздных систем
solar_system_id = get_cyno_solar_system_details(
loc_dict["location_id"],
corp_assets_tree,
-5 # Solar System (поиск вниз по дереву)
)
else:
solar_system_id = None
badger_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 648) # Badger
venture_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 32880) # Venture
liquid_ozone_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 16273) # Liquid Ozone
indus_cyno_gen_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 52694) # Industrial Cynosural Field Generator
exp_cargohold_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 1317) # Expanded Cargohold I
cargohold_rigs_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 31117) # Small Cargohold Optimization I
nitrogen_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 17888) # Nitrogen Isotopes
hydrogen_isotope_ids = get_cyno_solar_system_details(location_id, corp_assets_tree, 17889) # Hydrogen Isotopes
return {"solar_system": solar_system_id,
"badger": badger_ids,
"venture": venture_ids,
"liquid_ozone": liquid_ozone_ids,
"indus_cyno_gen": indus_cyno_gen_ids,
"exp_cargohold": exp_cargohold_ids,
"cargohold_rigs": cargohold_rigs_ids,
"nitrogen_isotope": nitrogen_isotope_ids,
"hydrogen_isotope": hydrogen_isotope_ids}
else:
type_id = loc_dict["type_id"] if "type_id" in loc_dict else None
if not (type_id is None) and (type_id == abs(subtype)): # нашли
return location_id # выдаём как item_id
if subtype < 0:
if "location_id" in loc_dict:
return get_cyno_solar_system_details(loc_dict["location_id"], corp_assets_tree, subtype)
else:
return None
else: # subtype > 0
result = []
items = loc_dict["items"] if "items" in loc_dict else None
if not (items is None):
for i in items:
item_ids = get_cyno_solar_system_details(i, corp_assets_tree, subtype)
if not (item_ids is None):
if isinstance(item_ids, list):
result.extend(item_ids)
else:
result.append(item_ids)
if len(result) > 0:
return result
else:
return None
return None
def main():
# работа с параметрами командной строки, получение настроек запуска программы, как то: работа в offline-режиме,
# имя пилота ранее зарегистрированного и для которого имеется аутентификационный токен, регистрация нового и т.д.
argv_prms = console_app.get_argv_prms()
# настройка Eve Online ESI Swagger interface
auth = esi.EveESIAuth(
'{}/auth_cache'.format(argv_prms["workspace_cache_files_dir"]),
debug=True)
client = esi.EveESIClient(
auth,
q_industrialist_settings.g_client_id,
keep_alive=True,
debug=argv_prms["verbose_mode"],
logger=True,
user_agent='Q.Industrialist v{ver}'.format(ver=__version__),
restrict_tls13=q_industrialist_settings.g_client_restrict_tls13)
interface = esi.EveOnlineInterface(
client,
q_industrialist_settings.g_client_scope,
cache_dir='{}/esi_cache'.format(argv_prms["workspace_cache_files_dir"]),
offline_mode=argv_prms["offline_mode"])
authz = interface.authenticate(argv_prms["character_names"][0])
character_id = authz["character_id"]
character_name = authz["character_name"]
# Public information about a character
character_data = interface.get_esi_data(
"characters/{}/".format(character_id),
fully_trust_cache=True)
# Public information about a corporation
corporation_data = interface.get_esi_data(
"corporations/{}/".format(character_data["corporation_id"]),
fully_trust_cache=True)
corporation_id = character_data["corporation_id"]
corporation_name = corporation_data["name"]
print("\n{} is from '{}' corporation".format(character_name, corporation_name))
sys.stdout.flush()
sde_type_ids = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "typeIDs")
sde_inv_names = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "invNames")
sde_inv_items = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "invItems")
sde_market_groups = eve_sde_tools.read_converted(argv_prms["workspace_cache_files_dir"], "marketGroups")
# Requires role(s): Director
corp_assets_data = interface.get_esi_paged_data(
"corporations/{}/assets/".format(corporation_id))
print("\n'{}' corporation has {} assets".format(corporation_name, len(corp_assets_data)))
sys.stdout.flush()
# Получение названий контейнеров, станций, и т.п. - всё что переименовывается ingame
corp_ass_named_ids = eve_esi_tools.get_assets_named_ids(corp_assets_data)
# Requires role(s): Director
corp_ass_names_data = interface.get_esi_piece_data(
"corporations/{}/assets/names/".format(corporation_id),
corp_ass_named_ids)
print("\n'{}' corporation has {} custom asset's names".format(corporation_name, len(corp_ass_names_data)))
sys.stdout.flush()
del corp_ass_named_ids
# Поиск тех станций, которые не принадлежат корпорации (на них имеется офис, но самой станции в ассетах нет)
foreign_structures_data = {}
foreign_structures_ids = eve_esi_tools.get_foreign_structures_ids(corp_assets_data)
foreign_structures_forbidden_ids = []
if len(foreign_structures_ids) > 0:
# Requires: access token
for structure_id in foreign_structures_ids:
try:
universe_structure_data = interface.get_esi_data(
"universe/structures/{}/".format(structure_id),
fully_trust_cache=True)
foreign_structures_data.update({str(structure_id): universe_structure_data})
except requests.exceptions.HTTPError as err:
status_code = err.response.status_code
if status_code == 403: # это нормально, что часть структур со временем могут оказаться Forbidden
foreign_structures_forbidden_ids.append(structure_id)
else:
raise
except:
print(sys.exc_info())
raise
print("\n'{}' corporation has offices in {} foreign stations".format(corporation_name, len(foreign_structures_data)))
if len(foreign_structures_forbidden_ids) > 0:
print("\n'{}' corporation has offices in {} forbidden stations : {}".format(corporation_name, len(foreign_structures_forbidden_ids), foreign_structures_forbidden_ids))
sys.stdout.flush()
try:
# Public information about market prices
eve_market_prices_data = interface.get_esi_paged_data("markets/prices/")
print("\nEVE market has {} prices".format(len(eve_market_prices_data) if not (eve_market_prices_data is None) else 0))
sys.stdout.flush()
except requests.exceptions.HTTPError as err:
status_code = err.response.status_code
if status_code == 404: # 2020.12.03 поломался доступ к ценам маркета (ССР-шники "внесли правки")
eve_market_prices_data = []
else:
raise
except:
print(sys.exc_info())
raise
# # Public information with list of public structures
# universe_structures_data = eve_esi_interface.get_esi_data(
# access_token,
# "universe/structures/")
# print("\nFound {} public structures in universe".format(len(universe_structures_data)))
# sys.stdout.flush()
# Построение дерева ассетов, с узлави в роли станций и систем, и листьями в роли хранящихся
# элементов, в виде:
# { location1: {items:[item1,item2,...],type_id,location_id},
# location2: {items:[item3],type_id} }
corp_assets_tree = eve_esi_tools.get_assets_tree(corp_assets_data, foreign_structures_data, sde_inv_items, virtual_hierarchy_by_corpsag=True)
eve_esi_tools.dump_debug_into_file(argv_prms["workspace_cache_files_dir"], "corp_assets_tree", corp_assets_tree)
# Построение дерева asset-ов:
print("\nBuilding assets tree report...")
sys.stdout.flush()
render_html_assets.dump_assets_tree_into_report(
# путь, где будет сохранён отчёт
argv_prms["workspace_cache_files_dir"],
# sde данные, загруженные из .converted_xxx.json файлов
sde_type_ids,
sde_inv_names,
sde_inv_items,
sde_market_groups,
# esi данные, загруженные с серверов CCP
corp_assets_data,
corp_ass_names_data,
foreign_structures_data,
eve_market_prices_data,
# данные, полученные в результате анализа и перекомпоновки входных списков
corp_assets_tree)
# Вывод в лог уведомления, что всё завершилось (для отслеживания с помощью tail)
print("\nDone")
if __name__ == "__main__":
main()