Example #1
0
def get_structure_id(structure_id, token_entity_id, safe=True):
    from django_eveonline_connector.models import EveStructure
    if str(structure_id) in cache:
        return cache.get(str(structure_id))

    if safe:  # not running safe can lead to ESI lockouts, structure resolution is busted
        structure = EveStructure.objects.filter(
            structure_id=structure_id).first()
        if structure:
            return structure.name
        else:
            return "Unknown Structure"
    else:
        try:
            token = EveToken.objects.get(
                evecharacter__external_id=token_entity_id)
        except EveToken.DoesNotExist:
            raise EveDataResolutionError(
                'Attempted to resolve a structure without a token')

        response = EveClient.call('get_universe_structures_structure_id',
                                  token=token,
                                  structure_id=structure_id)
        if response.status != 200:
            raise EveDataResolutionError(
                f"Failed to resolve structure {structure_id} using external ID {token_entity_id}. Reason: {response.data}"
            )

        if cache:
            cache.set(str(structure_id), response.data['name'])
            return response.data['name']
        else:
            return response.data['name']
Example #2
0
def get_eve_character_transactions(character_id, ignore_ids=[]):
    logger.info("Getting transactions for %s" % character_id)
    logger.debug("Transactions to ignore: %s" % ignore_ids)
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    logger.debug("Resolved ID %s to character %s" %
                 (character_id, token.evecharacter.name))
    # resolve and clean transaction list
    transactions_response = EveClient.call(
        'get_characters_character_id_wallet_transactions',
        token,
        character_id=character_id)

    # resolve ids
    ids_to_resolve = set()
    for transaction in transactions_response:
        ids_to_resolve.add(transaction['client_id'])
    resolved_ids = resolve_ids_with_types(ids_to_resolve)

    transactions = []
    for transaction in transactions_response:
        if transaction['transaction_id'] in ignore_ids:
            logger.debug("Skipping ignored transaction: %s" %
                         transaction['transaction_id'])
            continue
        transaction['client'] = resolved_ids[transaction['client_id']]['name']
        transaction['client_id'] = transaction['client_id']
        transaction['client_type'] = resolved_ids[
            transaction['client_id']]['type']
        transaction['type_name'] = resolve_type_id_to_type_name(
            transaction['type_id'])
        transactions.append(transaction)
    logger.debug("Total transactions: %s \n Returned Transactions: %s" %
                 (len(transactions_response), len(transactions)))
    return transactions
Example #3
0
def get_eve_character_skillpoints(character_id):
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    skillpoints_response = EveClient.call('get_characters_character_id_skills',
                                          token,
                                          character_id=character_id)

    return skillpoints_response['total_sp']
Example #4
0
def get_type_id(type_id):
    response = EveClient.call('get_universe_types_type_id',
                              type_id=type_id,
                              raise_exception=True)
    if response.status != 200:
        raise EveDataResolutionError(
            f"Failed to resolve type_id({type_id}) using ESI. Notify CCP.")
    return response.data
Example #5
0
def resolve_ids(ids):
    resolved_ids = EveClient.call('post_universe_names', token=None, ids=ids)
    response = {}
    for resolved_id in resolved_ids:
        external_id = resolved_id['id']
        external_name = resolved_id['name']
        if external_id not in response:
            response[external_id] = external_name
    return response
def get_entity_info(request):
    external_id = None
    if 'external_id' not in request.GET:
        return HttpResponse(status=400)
    else:
        external_id = request.GET['external_id']
    print(external_id)

    entity = resolve_id(external_id)

    if not entity:
        return HttpResponse(status=404)

    if entity['type'] == "character":
        response = EveClient.call(
            'get_characters_character_id', character_id=external_id).data
        return JsonResponse({
            "type": "character",
            "name": response['name'],
            "external_id": external_id,
        })
    elif entity['type'] == "corporation":
        response = EveClient.call(
            'get_corporations_corporation_id', corporation_id=external_id).data
        return JsonResponse({
            "type": "corporation",
            "name": response['name'],
            "ticker": response['ticker'],
            "external_id": external_id,
            "alliance_id": response['alliance_id'],
        })
    elif entity['type'] == "alliance":
        response = EveClient.call(
            'get_alliances_alliance_id', alliance_id=external_id).data
        return JsonResponse({
            "type": "alliance",
            "name": response['name'],
            "ticker": response['ticker'],
            "external_id": external_id,
        })
    else:
        return HttpResponse(status=500)
Example #7
0
def get_eve_character_skills(character_id):
    logger.info("Gathering token for %s" % character_id)
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    logger.info("Pulling skills of %s" % token.evecharacter.name)
    # resolve and clean skill list
    skills = EveClient.call(
        'get_characters_character_id_skills', token, character_id=character_id)['skills']
    for skill in skills:
        skill['skill_name'] = resolve_type_id_to_type_name(skill['skill_id'])
        skill['skill_type'] = resolve_type_id_to_group_name(skill['skill_id'])
    return skills
Example #8
0
def get_structure(structure_id, corporation_id):
    from django_eveonline_connector.models import EveCorporation
    corporation = EveCorporation.objects.get(external_id=corporation_id)
    response = EveClient.call('get_universe_structures_structure_id',
                              token=corporation.ceo.token,
                              structure_id=structure_id)
    if response.status != 200:
        raise EveDataResolutionError(
            f"Failed to resolve structure {structure_id} using external ID {corporation_id}. Reason: {response.data}"
        )

    return response
Example #9
0
def resolve_ids_with_types(ids):
    resolved_ids = EveClient.call('post_universe_names', token=None, ids=ids)
    response = {}
    for resolved_id in resolved_ids:
        external_id = resolved_id['id']
        external_name = resolved_id['name']
        external_type = resolved_id['category']
        if external_id not in response:
            response[external_id] = {
                "name": external_name,
                "type": external_type
            }
    return response
Example #10
0
def get_eve_character_clones(character_id):
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    # resolve and clean asset list
    clones_response = EveClient.call('get_characters_character_id_clones',
                                     token,
                                     character_id=character_id)
    structure_ids = []
    clones = []
    implant_list = {}
    # get structure ids and build implant list
    for clone in clones_response['jump_clones']:
        if clone['location_type'] == 'structure':
            structure_ids.append(clone['location_id'])
        for implant in clone['implants']:
            implant_list[implant] = {
                "item_id": implant,
                "item_name": resolve_type_id_to_type_name(implant)
            }

    logger.debug("Structure names to resolve: %s" % structure_ids)
    structure_names = resolve_structure_ids(token=token,
                                            structure_ids=structure_ids)
    logger.debug("Structure resolutions: %s" % structure_names)

    # build read-able clone list
    for clone in clones_response['jump_clones']:
        readable_clone = {
            'location': None,
            'implants': [],
        }

        # resolve locaton
        if clone['location_type'] == 'station':
            readable_clone['location'] = resolve_location_id_to_station(
                clone['location_id'])
        elif clone['location_type'] == 'structure':
            readable_clone['location'] = structure_names[clone['location_id']]
        else:
            readable_clone['location'] = "Unknown Location"

        # resolve implants
        for implant in clone['implants']:
            readable_clone['implants'].append(
                implant_list[implant]['item_name'])  # TODO: refactor

        clones.append(readable_clone)

    return clones
Example #11
0
def resolve_ids(ids):
    response = EveClient.call('post_universe_names', ids=ids)

    if response.status != 200:
        raise EveDataResolutionError(
            f"[{response.status} Failed to resolve universe names. {response.data}"
        )

    resolved_ids = response.data
    response = {}
    for resolved_id in resolved_ids:
        external_id = resolved_id['id']
        external_name = resolved_id['name']
        if external_id not in response:
            response[external_id] = external_name
    return response
Example #12
0
def get_contract_items(character_id, contract_id):
    logger.info("Getting contract items for %s" % contract_id)
    token = EveToken.objects.get(evecharacter__external_id=character_id)

    contracts_response = EveClient.call(
        'get_characters_character_id_contracts_contract_id_items',
        token,
        character_id=character_id,
        contract_id=contract_id)

    items = []
    for item in contracts_response:
        items.append(
            "%s %s" %
            (item['quantity'], resolve_type_id_to_type_name(item['type_id'])))

    return items
Example #13
0
def resolve_ids_with_types(ids):
    response = {}
    for ids_segment in batch(list(ids), 999):
        request = EveClient.call('post_universe_names', ids=ids_segment)
        if request.status != 200:
            raise EveDataResolutionError(
                f"[{request.status}] Failed to resolve IDs: {ids_segment}\nResponse: {request.data}"
            )
        resolved_ids = request.data

        for resolved_id in resolved_ids:
            external_id = resolved_id['id']
            external_name = resolved_id['name']
            external_type = resolved_id['category']
            if external_id not in response:
                response[external_id] = {
                    "name": external_name,
                    "type": external_type
                }
    return response
Example #14
0
def get_eve_character_contacts(character_id):
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    # resolve and clean asset list
    contacts_response = EveClient.call('get_characters_character_id_contacts',
                                       token,
                                       character_id=character_id)
    unresolved_ids = []
    contacts = []
    # get list of ids
    for contact in contacts_response:
        unresolved_ids.append(contact['contact_id'])

    resolved_ids = resolve_ids(unresolved_ids)

    # match ids to contacts
    for contact in contacts_response:
        contacts.append({
            'name': resolved_ids[contact['contact_id']],
            'external_id': contact['contact_id'],
            'type': contact['contact_type'].upper(),
            'standing': contact['standing'],
        })
    return contacts
Example #15
0
def get_group_id(group_id):
    return EveClient.call('get_universe_groups_group_id',
                          group_id=group_id).data
Example #16
0
def get_category_id(category_id):
    return EveClient.call('get_universe_categories_category_id',
                          category_id=category_id).data
Example #17
0
def get_eve_character_net_worth(character_id):
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    wallet_response = EveClient.call('get_characters_character_id_wallet',
                                     token,
                                     character_id=character_id)
    return wallet_response
Example #18
0
def get_station_id(station_id):
    return EveClient.call('get_universe_stations_station_id',
                          station_id=station_id).data
Example #19
0
def resolve_names(names):
    resolved_ids = EveClient.call('post_universe_ids', token=None, names=names)
    return resolved_ids
Example #20
0
def resolve_names(names):
    resolved_ids = EveClient.call('post_universe_ids', names=names).data
    return resolved_ids
Example #21
0
def get_type_id(type_id):
    return EveClient.call('get_universe_types_type_id',
                          token=None,
                          type_id=type_id)
Example #22
0
def get_station_id(station_id):
    return EveClient.call('get_universe_stations_station_id',
                          token=None,
                          station_id=station_id)
Example #23
0
def get_group_id(group_id):
    return EveClient.call('get_universe_groups_group_id',
                          token=None,
                          group_id=group_id)
Example #24
0
def get_eve_character_contracts(character_id, contract_ids_to_ignore=[]):
    logger.info("Getting contracts for %s" % character_id)
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    logger.debug("Resolved ID %s to character %s" %
                 (character_id, token.evecharacter.name))
    # resolve and clean asset list
    contracts_response = EveClient.call(
        'get_characters_character_id_contracts',
        token,
        character_id=character_id)
    logger.debug("Response: %s" % contracts_response[0])
    logger.debug("Obtaining IDs found in contracts of %s" %
                 token.evecharacter.name)
    ids_to_resolve = set()
    for contract in contracts_response:
        if 'assignee_id' in contract:
            ids_to_resolve.add(contract['assignee_id'])
        if 'acceptor_id' in contract:
            ids_to_resolve.add(contract['acceptor_id'])
        if 'issuer_id' in contract:
            ids_to_resolve.add(contract['issuer_id'])

    logger.debug("Resolving IDs to names: %s" % ids_to_resolve)
    resolved_ids = resolve_ids_with_types(list(ids_to_resolve))
    logger.debug("Resolved IDs: %s" % resolved_ids)

    contracts = []
    logger.info("Building contract list of %s from ESI response" %
                token.evecharacter.name)
    for contract in contracts_response:
        if contract['contract_id'] in contract_ids_to_ignore:
            continue
        resolved_contract = {
            "contract_id": contract['contract_id'],
            "issued_by": resolved_ids[contract['issuer_id']]['name'],
            "issued_by_type": resolved_ids[contract['issuer_id']]['type'],
            "date_created": contract['date_issued'],
            "contract_type": contract['type'],
            "contract_status": contract['status'].replace("_", " ").upper()
        }

        if 'assignee_id' in contract:
            if contract['assignee_id'] != 0:
                resolved_contract['issued_to'] = resolved_ids[
                    contract['assignee_id']]['name']
                resolved_contract['issued_to_type'] = resolved_ids[
                    contract['assignee_id']]['type']
            else:
                resolved_contract['issued_to'] = None
                resolved_contract['issued_to_type'] = None

        if 'acceptor_id' in contract:
            if contract['acceptor_id'] != 0:
                resolved_contract['accepted_by'] = resolved_ids[
                    contract['acceptor_id']]['name']
                resolved_contract['accepted_by_type'] = resolved_ids[
                    contract['acceptor_id']]['type']
            else:
                resolved_contract['accepted_by'] = None
                resolved_contract['accepted_by_type'] = None

        if 'price' in contract:
            resolved_contract['contract_value'] = contract['price']
        elif 'reward' in contract:
            resolved_contract['contract_value'] = contract['reward']
        else:
            resolved_contract['contract_value'] = 0

        resolved_contract['items'] = get_contract_items(
            character_id, contract['contract_id'])

        contracts.append(resolved_contract)

    return contracts
Example #25
0
def get_eve_character_assets(character_id):
    logger.info("Gathering token for %s" % character_id)
    token = EveToken.objects.get(evecharacter__external_id=character_id)
    logger.info("Pulling assets of %s" % token.evecharacter.name)
    # resolve and clean asset list
    assets = EveClient.call('get_characters_character_id_assets',
                            token,
                            character_id=character_id)
    # purge items not in hangars or asset safety
    logger.debug("Removing assets not in Hangar or AssetSafety")
    assets = [
        asset for asset in assets
        if not asset['location_flag'] not in ['AssetSafety', 'Hangar']
    ]
    # purge bad asset categories
    logger.debug("Purging bad assets categories: %s" % BAD_ASSET_CATEGORIES)
    assets = [
        asset for asset in assets if resolve_type_id_to_category_id(
            asset['type_id']) not in BAD_ASSET_CATEGORIES
    ]
    structure_ids = set()
    for asset in assets:
        # add item name
        asset['item_name'] = resolve_type_id_to_type_name(asset['type_id'])
        # clean excess fields
        if 'is_blueprint_copy' in asset:
            asset.pop('is_blueprint_copy')
        asset.pop('is_singleton')
        asset.pop('type_id')
        asset.pop('item_id')
        asset.pop('location_flag')
        # get list of structure IDs
        if asset['location_type'] == 'other':
            structure_ids.add(asset['location_id'])

    # get structure ids
    structure_names = {}
    logger.debug("Resolving structure names for structure IDs: %s" %
                 structure_ids)
    for structure_id in structure_ids:
        structure_response = EveClient.call(
            'get_universe_structures_structure_id',
            token,
            structure_id=structure_id)
        if 'error' in structure_response:
            structure_names[structure_id] = "Restricted Structure"
        else:
            structure_names[structure_id] = structure_response.name

    logger.debug("Resolved structures: %s" % structure_names)

    # clean location names
    for asset in assets:
        try:
            if asset['location_type'] == 'station':
                asset['location'] = resolve_location_id_to_station(
                    asset['location_id'])
                asset.pop('location_type')
                asset.pop('location_id')
            elif asset['location_type'] == 'other':
                asset['location'] = structure_names[asset['location_id']]
                asset.pop('location_type')
                asset.pop('location_id')
        except Exception as e:
            logger.error("Failed to resolve location for asset: %s" % str(e))

    return assets