def __init__(self): r = redis.StrictRedis(host='localhost', port=6379, db=1) self.cache = RedisCache(r) self.esi = EsiPysi( 'https://esi.evetech.net/_latest/swagger.json?datasource=tranquility', user_agent=config.email, cache=self.cache)
class ESI: def __init__(self): r = redis.StrictRedis(host='localhost', port=6379, db=1) self.cache = RedisCache(r) self.esi = EsiPysi( 'https://esi.evetech.net/_latest/swagger.json?datasource=tranquility', user_agent=config.email, cache=self.cache) #self.auth_esi = EsiAuth(config.client_id, config.secret_key) def search(self, scope, entity): entity_search = self.esi.get_operation("get_search") result = entity_search.json(categories=scope, search=entity, strict=True) return result def loose_search(self, scope, entity): entity_search = self.esi.get_operation("get_search") result = entity_search.json(categories=scope, search=entity, strict=False) return result def kill_lookup(self, kill_id, kill_hash): kill_call = self.esi.get_operation( "get_killmails_killmail_id_killmail_hash") result = kill_call.json(killmail_id=kill_id, killmail_hash=kill_hash) return result
def __init__(self, request, loop): access = request.session.get("access-token") refresh = request.session.get("refresh-token") self.auth = EsiAuth(settings.ESI_CLIENT, settings.ESI_SECRET, access, refresh, None) self.esi = EsiPysi("https://esi.evetech.net/_latest/swagger.json", loop=loop) self.char_id = None self.char_name = None self.corp_id = None self.alliance_id = None self.in_alliance = None
async def test_404_op(self): async with EsiPysi("https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test").session() as esi: op = esi.get_operation("get_universe_regions_region_id") try: await op.execute(region_id=9) self.fail("Should raise exception") except HTTPError as ex: self.assertEqual(ex.code, 404)
async def test_simple_op(self): cache = DictCache() async with EsiPysi( "https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test", cache=cache).session() as esi: op_id = "get_search" data = {"categories": "character", "search": "Flying Kiwi Sertan"} op = esi.get_operation(op_id) await op.execute(**data) self.assertTrue(cache.in_cache(op_id, data))
async def test_cached_list_op(self): cache = DictCache() async with EsiPysi( "https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test", cache=cache).session() as esi: op_id = "post_universe_names" data = {"ids": [30000142, 30002693]} op = esi.get_operation(op_id) await op.execute(**data) self.assertTrue(cache.in_cache(op_id, data))
async def test_cache_retrival(self): cache = DictCache() async with EsiPysi( "https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test", cache=cache).session() as esi: op_id = "get_search" data = {"categories": "character", "search": "Flying Kiwi Sertan"} op = esi.get_operation(op_id) result = await op.execute(**data) result_id = result.headers.get("x-esi-request-id") if result_id is None: self.fail("No result ID in headers") self.assertTrue(cache.in_cache(op_id, data)) result2 = await op.execute(**data) self.assertEqual(result_id, result2.headers.get("x-esi-request-id")) self.assertEqual(result2.json(), {'character': [95095106]})
def get_client(cls): esi_url = "https://esi.evetech.net/_latest/swagger.json?datasource=tranquility" ua = "Esipraisal - IGN: Flying Kiwi Sertan" return EsiPysi(esi_url, user_agent=ua)
class Esi(): log = logging.getLogger(__name__) def __init__(self, request, loop): access = request.session.get("access-token") refresh = request.session.get("refresh-token") self.auth = EsiAuth(settings.ESI_CLIENT, settings.ESI_SECRET, access, refresh, None) self.esi = EsiPysi("https://esi.evetech.net/_latest/swagger.json", loop=loop) self.char_id = None self.char_name = None self.corp_id = None self.alliance_id = None self.in_alliance = None def close(self): self.esi.close() async def __do_request(self, op, parameters={}): if op is None: self.log.error("No operation provided, did the ESI spec change?") return None if self.auth is not None: op.set_auth(self.auth) try: self.log.debug("Executing op \"{}\" with parameters: {}".format(op, parameters)) result = await op.execute(**parameters) except HTTPError as e: self.log.exception("An error occured with the ESI call \"{}\" with parameters: {}".format(op, parameters)) self.last_error = e except Exception: self.log.exception("An exception occured with a ESI call") pass else: self.log.debug("op \"{}\" with parameters: {} complete, result: {}".format(op, parameters, result.text)) return result.json() return None async def get_char_id(self): if self.char_id is not None: return self.char_id verify = await self.auth.verify() self.char_id = verify.get("CharacterID") self.char_name = verify.get("CharacterName") return self.char_id async def get_char_name(self): if self.char_name is not None: return self.char_name verify = await self.auth.verify() self.char_id = verify.get("CharacterID") self.char_name = verify.get("CharacterName") return self.char_id async def is_hr(self): char_id = await self.get_char_id() op = self.esi.get_operation("get_characters_character_id_roles") results = await self.__do_request(op, {"character_id":char_id}) if results is None: return False roles = results.get("roles", []) if "Personnel_Manager" in roles: return True return False async def get_corp_id(self): if self.corp_id is not None: return self.corp_id await self.load_affiliations() return self.corp_id async def get_alliance_id(self): if self.in_alliance is not None: return self.alliance_id await self.load_affiliations() return self.alliance_id async def is_in_alliance(self): if self.in_alliance is not None: return self.in_alliance await self.load_affiliations() return self.in_alliance async def load_affiliations(self): char_id = await self.get_char_id() op = self.esi.get_operation("post_characters_affiliation") results = await self.__do_request(op, {"characters":[char_id]}) if len(results) != 1: return result = results[0] self.alliance_id = result.get("alliance_id") self.corp_id = result.get("corporation_id") if self.corp_id is not None: if self.alliance_id is None: self.in_alliance = False else: self.in_alliance = True async def get_kill_info(self, kill_id, kill_hash): op = self.esi.get_operation("get_killmails_killmail_id_killmail_hash") results = await self.__do_request(op, {"killmail_id":kill_id,"killmail_hash":kill_hash}) if results is None: print("{} - {} is none".format(kill_id, kill_hash)) return None vic = results.get("victim", {}) char_id = vic.get("character_id") if char_id is None: print("char_id is none") return None ship_id = vic.get("ship_type_id") if ship_id is None: print("ship name is none") return None kill_time = results.get("killmail_time","") char_name = await self.get_character_name(char_id) ship_name = await self.get_type_name(ship_id) org_info = await self.get_org_info(char_id) details = {"id": kill_id, "hash":kill_hash,"victim_name":char_name,"victim_id":char_id,"victim_ship":ship_name, "victim_ship_id":ship_id,"victim_orgs":org_info,"kill_time":kill_time} return details async def get_character_name(self, character_id): op = self.esi.get_operation("get_characters_character_id") results = await self.__do_request(op, {"character_id":character_id}) if results is None: return None return results.get("name") async def get_type_name(self, type_id): op = self.esi.get_operation("get_universe_types_type_id") results = await self.__do_request(op, {"type_id":type_id}) if results is None: return None return results.get("name") async def get_org_info(self, character_id): op = self.esi.get_operation("post_characters_affiliation") results = await self.__do_request(op, {"characters":[character_id]}) if len(results) != 1: return result = results[0] alliance_id = result.get("alliance_id") corp_id = result.get("corporation_id") ids = [] ids.append(corp_id) if alliance_id is not None: ids.append(alliance_id) op = self.esi.get_operation("post_universe_names") results = await self.__do_request(op, {"ids":ids}) if results is None: return {} org_info = {} for result in results: if result.get("category") == "alliance": org_info["alliance_id"] = result.get("id") org_info["alliance_name"] = result.get("name") elif result.get("category") == "corporation": org_info["corporation_id"] = result.get("id") org_info["corporation_name"] = result.get("name") return org_info async def send_mail(self, recipient_id, recipient_name, body, subject): #Form JSON recipients = [] recipient_details = {} recipient_details["recipient_type"] = "character" recipient_details["recipient_id"] = recipient_id recipients.append(recipient_details) mail = {} mail["approved_cost"] = 0 mail["body"] = body mail["recipients"] = recipients mail["subject"] = subject char_id = await self.get_char_id() op = self.esi.get_operation("post_characters_character_id_mail") results = await self.__do_request(op, {"character_id":char_id, "mail":mail}) if results is None: return {"sent":False, "error":self.last_error} return {"sent":True}
#bot.py import os import discord from dotenv import load_dotenv from esipysi import EsiPysi from esipysi import EsiAuth import requests from esipysi import EsiAuth import asyncio import aiohttp import time from asgiref.sync import sync_to_async esi = EsiPysi( "https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Cailean Blackies") load_dotenv() token = os.getenv('DISCORD_TOKEN') guild = os.getenv('DISCORD_GUILD') headers = { 'Content-Type': 'application/json', 'Authorization': 'Basic MGM4YzI0MTYwY2QyNDFiMmI2N2EzYzRlM2I2NDkxNjU6RUZLRHZNVVpSeHZvYU14UHNIeXJwNEduZzU3Z0lOM2xrZ1dnYUlZYQ==', } data = '{"grant_type":"authorization_code", "code":"0Na3WjTPCWe6DMFFKZwYKlBGSTf7EzIlwYSqfLZuF_Vu6rKkXzRs9sgnI21OSicU"}' response = requests.post('https://login.eveonline.com/oauth/token',
async def test_simple_op(self): async with EsiPysi("https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test").session() as esi: op = esi.get_operation("get_search") result = await op.execute(categories="character", search="Flying Kiwi Sertan") self.assertEqual(result.json(), {'character': [95095106]})
async def test_headers(self): async with EsiPysi("https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test").session() as esi: op = esi.get_operation("get_search") result = await op.execute(categories="character", search="Flying Kiwi Sertan") self.assertIsNotNone(result.headers.get("x-esi-request-id"))
async def test_post_op(self): async with EsiPysi("https://esi.evetech.net/_latest/swagger.json?datasource=tranquility", user_agent="Eve Test").session() as esi: op = esi.get_operation("post_universe_names") await op.execute(ids=[30000142, 30002693])