-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesi.py
165 lines (124 loc) · 4.75 KB
/
esi.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
import json
from esipy import App, EsiClient
from esipy.cache import FileCache
from esipy.exceptions import APIException
import config
esiapp = App.create(config.ESI_SWAGGER_JSON)
esiclient = EsiClient(
# cache=RedisCache(Redis(config.REDIS_CACHE_HOST, port=config.REDIS_CACHE_PORT)),
cache=FileCache('.webcache'),
headers={'User-Agent': config.ESI_USER_AGENT}
)
def _get_esi(op, raw_body_only = False):
"""
Try to get a result from an esipy request response tuple
:param op: Request response tuple generated by esiapp.op
:return: parsed result data
:raises: `esipy.excpetions.APIException` if an ESI Exception is encountered
"""
res = esiclient.request(op, raw_body_only=raw_body_only)
if res.status == 200:
if raw_body_only:
return json.loads(res.raw.decode('utf-8'))
else:
return res.data
else:
raise APIException(op[0].url, res.status, json.loads(res.raw.decode('utf-8')))
def get_system(system_id):
"""
Get system details for a system id from ESI
:param system_id: ID for the required system
:return: Dictionary containing system details
"""
op = esiapp.op['get_universe_systems_system_id'](system_id=system_id)
return _get_esi(op)
def get_constellation(constellation_id):
"""
Get constellation details for a constellation id from ESI
:param constellation_id: ID for the required constellation
:return: Dictionary containing constellation details
"""
op = esiapp.op['get_universe_constellations_constellation_id'](constellation_id=constellation_id)
return _get_esi(op)
def get_region(region_id):
"""
Get region details for a region id from ESI
:param region_id: ID for the required region
:return: Dictionary containing region details
"""
op = esiapp.op['get_universe_regions_region_id'](region_id=region_id)
return _get_esi(op)
def get_character(character_id):
"""
Get character details for a character id from ESI
:param character_id: ID for the required character
:return: Dictionary containing character details
"""
op = esiapp.op['get_characters_character_id'](character_id=character_id)
return _get_esi(op)
def get_corporation(corporation_id):
"""
Get corporation details for a corporation id from ESI
:param corporation_id: ID for the required corporation
:return: Dictionary containing corporation details
"""
op = esiapp.op['get_corporations_corporation_id'](corporation_id=corporation_id)
return _get_esi(op)
def get_name(id):
"""
Get the associated Name for an ID from ESI. Can be a type, character, corporation, region, system, constellation
:param id: ID to be resolved
:return: Dictionary containing id and name
"""
op = esiapp.op['post_universe_names'](ids=[id])
data = _get_esi(op)
if len(data) > 0:
return data[0]
else:
raise ValueError("No results for ID {}".format(id))
def get_type(type_id):
"""
Get details for a Type ID
:param type_id: Type ID to be resolved
:return: Dictionary containing type details
"""
op = esiapp.op['get_universe_types_type_id'](type_id=type_id)
return _get_esi(op)
def get_faction_corp(id):
"""
Get the primary corporation associated with a Faction from ESI
:param id: Faction ID
:return: Dictionary containing corporation info
"""
op = esiapp.op['get_universe_factions']()
factions = _get_esi(op)
if factions:
# TODO: Handle invalid faction IDs
corp_id = [f.get('corporation_id',None) for f in factions if f['faction_id'] == id][0]
if corp_id:
corp = get_corporation(corp_id)
return {'corporation_id':corp_id, **corp}
else:
raise ValueError('No Corp for faction id {}'.format(id))
def get_system_region(system_id):
"""
Get the region for a system id via system -> constellation.
:param system_id: System ID to be resolved
:return: Dictionary containing region details.
"""
system = get_system(system_id)
constellation = get_constellation(system.get('constellation_id'))
region = get_region(constellation.get('region_id'))
return region
def check_jspace(system_id):
"""
Get the wormhole class for a given system via system -> constellation -> region
:param system_id: System ID to be checked
:return: String of the wormhole class or False for K-Space.
"""
region = get_system_region(system_id)
code = region.get('name', 'XX')[0:2]
return config.JSPACE_REGION_MAP.get(code, False)
def get_killmail(killmail_id, killmail_hash):
op = esiapp.op['get_killmails_killmail_id_killmail_hash'](killmail_id=killmail_id, killmail_hash=killmail_hash)
return _get_esi(op, raw_body_only=True)