Пример #1
0
def get_corp_info(corp_id: int, *args) -> APICacheCorporationInfo:
    corp_cache: APICacheCorporationInfo = db.session.query(APICacheCorporationInfo) \
        .filter(APICacheCorporationInfo.id == corp_id).first()

    if corp_cache is None:
        corp_cache = APICacheCorporationInfo()
        corp_ep = CorporationEndpoint()
        corp_info: CorporationInfo = check_esi_response(corp_ep.get_corporation_info(corp_id), get_corp_info, args)

        set_from_corp_info(corp_cache, corp_info, corp_id)
        db.session.add(corp_cache)
        db.session.commit()
    elif corp_cache.name is None:
        corp_ep = CorporationEndpoint()
        corp_info: CorporationInfo = check_esi_response(corp_ep.get_corporation_info(corp_id), get_corp_info, args)
        set_from_corp_info(corp_cache, corp_info, corp_id)
        db.session.commit()
    else:
        now = datetime.now()
        if corp_cache.expire is None or corp_cache.expire < now:
            # expired, update it
            corp_ep = CorporationEndpoint()
            try:
                corp_info: CorporationInfo = check_esi_response(corp_ep.get_corporation_info(corp_id), get_corp_info, args)
            except ESIException:
                return corp_cache
            set_from_corp_info(corp_cache, corp_info, corp_id)
            db.session.commit()

    return corp_cache
Пример #2
0
def get_character_info_by_name(name: str,
                               *args) -> Optional[APICacheCharacterInfo]:
    """
    Get Info for a character by name
    :param name: character name to get the info for
    :return: APICacheCharacterInfo of the character or None if no character with this name can be found
    :throws ApiException if a problem with the api occured
    """
    character = db.session.query(APICacheCharacterInfo).filter(
        APICacheCharacterInfo.characterName == name).first()
    if character is None:
        search_ep = SearchEndpoint()
        search_info: SearchResponse = check_esi_response(
            search_ep.public_search(name, ['character']),
            get_character_info_by_name, args)
        if search_info.character_ids() is None or\
           len(search_info.character_ids()) < 1:
            return None
        return get_character_info(search_info.character_ids()[0])
    else:
        # this does expire checks and such for us
        info: APICacheCharacterInfo = get_character_info(character.id)
        # lets make sure with updated info the names still match
        if info.characterName.lower() != name.lower():
            # if they don't, try to find a char for it
            search_ep = SearchEndpoint()
            search_info: SearchResponse = check_esi_response(
                search_ep.public_search(name, ['character']),
                get_character_info_by_name, args)

            return get_character_info(search_info.character_ids()[0])

        return info
Пример #3
0
def get_alliance_info(alliance_id: int, *args) -> APICacheAllianceInfo:
    all_cache: APICacheAllianceInfo = db.session.query(APICacheAllianceInfo) \
        .filter(APICacheAllianceInfo.id == alliance_id).first()

    if all_cache is None:
        all_cache = APICacheAllianceInfo()
        all_ep = AllianceEndpoint()
        all_info: AllianceInfo = check_esi_response(
            all_ep.get_alliance_info(alliance_id), get_alliance_info, args)

        set_from_alliance_info(all_cache, all_info, alliance_id)
        db.session.add(all_cache)
        db.session.commit()
    elif all_cache.characterName is None:
        all_ep = AllianceEndpoint()
        all_info: AllianceInfo = check_esi_response(
            all_ep.get_alliance_info(alliance_id), get_alliance_info, args)
        set_from_alliance_info(all_cache, all_info, alliance_id)
        db.session.commit()
    else:
        now = datetime.now()
        if all_cache.expire is None or all_cache.expire < now:
            # expired, update it
            all_ep = AllianceEndpoint()
            all_info: AllianceInfo = check_esi_response(
                all_ep.get_alliance_info(alliance_id), get_alliance_info, args)
            set_from_alliance_info(all_cache, all_info, alliance_id)
            db.session.commit()

    return all_cache
Пример #4
0
def get_character_info(char_id: int, *args) -> APICacheCharacterInfo:
    """
    :throws ApiException if an error with the api occured
    """
    char_cache: APICacheCharacterInfo = db.session.query(APICacheCharacterInfo) \
        .filter(APICacheCharacterInfo.id == char_id).first()

    if char_cache is None:
        char_cache = APICacheCharacterInfo()
        char_ep = CharacterEndpoint()
        char_info: CharacterInfo = check_esi_response(
            char_ep.get_character_info(char_id), get_character_info, args)
        char_cache.id = char_id
        set_from_character_info(char_cache, char_info, char_id)
        db.session.add(char_cache)
        db.session.commit()
    elif char_cache.characterName is None:
        char_ep = CharacterEndpoint()
        char_info: CharacterInfo = check_esi_response(
            char_ep.get_character_info(char_id), get_character_info, args)
        set_from_character_info(char_cache, char_info, char_id)
        db.session.commit()
    else:
        now = datetime.now()
        if char_cache.expire is None or char_cache.expire < now:
            # expired, update it
            char_ep = CharacterEndpoint()
            char_info: CharacterInfo = check_esi_response(
                char_ep.get_character_info(char_id), get_character_info, args)
            set_from_character_info(char_cache, char_info, char_id)
            db.session.commit()

    return char_cache
Пример #5
0
def get_character_info_by_name(name: str,
                               *args) -> Optional[APICacheCharacterInfo]:
    """
    Get Info for a character by name
    :param name: character name to get the info for
    :return: APICacheCharacterInfo of the character or None if no character with this name can be found
    :throws ApiException if a problem with the api occured
    """
    char_cache = db.session.query(APICacheCharacterInfo)\
        .filter(APICacheCharacterInfo.characterName == name).first()

    if char_cache is not None:
        __run_update_check(char_cache, char_cache.id)

        # lets make sure with updated info the names still match
        if char_cache.characterName.lower() == name.lower():
            return char_cache

    search_ep = SearchEndpoint()
    search_info: SearchResponse = check_esi_response(
        search_ep.public_search(name, ['character']),
        get_character_info_by_name, args)
    if search_info.character_ids() is None or\
        len(search_info.character_ids()) < 1:
        return None
    return get_character_info(search_info.character_ids()[0])
Пример #6
0
def get_char_or_corp_or_alliance_id_by_name(name: str, *args) -> Optional[int]:
    search_ep = SearchEndpoint()
    search_results: SearchResponse = check_esi_response(
        search_ep.public_search(name, ['character', 'corporation', 'alliance']),
        get_char_or_corp_or_alliance_id_by_name, args)
    ids = search_results.ids(['character', 'corporation', 'alliance'])
    if len(ids) < 1:
        return None
    return ids[0]
Пример #7
0
def get_corp_info(corp_id: int, *args) -> APICacheCorporationInfo:
    """
    :throws ApiException if something went wrong during Api calls
            and we can't recover to cache
    """
    corp_cache: APICacheCorporationInfo = db.session.query(APICacheCorporationInfo) \
        .filter(APICacheCorporationInfo.id == corp_id).first()

    if corp_cache is None:
        corp_cache = APICacheCorporationInfo()
        corp_ep = CorporationEndpoint()
        corp_info: CorporationInfo = check_esi_response(
            corp_ep.get_corporation_info(corp_id), get_corp_info, args)

        set_from_corp_info(corp_cache, corp_info, corp_id)
        db.session.add(corp_cache)
        db.session.commit()
    elif corp_cache.name is None:
        corp_ep = CorporationEndpoint()
        corp_info: CorporationInfo = check_esi_response(
            corp_ep.get_corporation_info(corp_id), get_corp_info, args)
        set_from_corp_info(corp_cache, corp_info, corp_id)
        db.session.commit()
    else:
        now = datetime.now()
        if corp_cache.expire is None or corp_cache.expire < now:
            # expired, update it
            corp_ep = CorporationEndpoint()
            try:
                corp_info: CorporationInfo = check_esi_response(
                    corp_ep.get_corporation_info(corp_id), get_corp_info, args)
            except ESIException:
                return corp_cache
            set_from_corp_info(corp_cache, corp_info, corp_id)
            db.session.commit()

    return corp_cache
Пример #8
0
def __get_names_for_ids(ep: UniverseEndpoint, id_list: List[int], count=1,
                        *args):
    resp: ResolveIdsResponse = ep.resolve_ids(id_list)

    if resp.is_error():
        if count <= 5:
            return __get_names_for_ids(id_list, count+1, args)
        # this will just throw an ApiException
        return check_esi_response(resp,
                                  get_names_for_ids, args)

    data = dict()
    for item in resp.data:
        data[item.id] = item
    return data
Пример #9
0
def __run_update_check(self: APICacheCharacterInfo, char_id: int, *args):
    """
    :throws ApiException if an error with the api occured
    """
    if self.expire is None or self.expire < datetime.now():
        char_ep = CharacterEndpoint()
        info: CharacterInfo = check_esi_response(
            char_ep.get_character_info(char_id), __run_update_check, args)
        self.id = char_id
        self.allianceID = info.get_alliance_id()
        self.characterName = info.get_name()
        self.corporationID = info.get_corp_id()
        self.characterBirthday = info.get_birthday()
        self.raceID = info.get_race_id()
        self.expire = info.expires()
        db.session.commit()
Пример #10
0
def get_char_or_corp_or_alliance_id_by_name(
        name: str, *args) -> Tuple[Optional[int], Optional[str]]:
    """
    Tuple of id and "character", "corporation", "alliance"
    or None, None
    :throws ApiException if something goes wrong on the api
    """
    search_ep = SearchEndpoint()
    search_results: SearchResponse = check_esi_response(
        search_ep.public_search(name,
                                ['character', 'corporation', 'alliance']),
        get_char_or_corp_or_alliance_id_by_name, args)

    ids = search_results.ids(['character'])
    if len(ids) > 0:
        return ids[0], 'character'
    ids = search_results.ids(['corporation'])
    if len(ids) > 0:
        return ids[0], 'corporation'
    ids = search_results.ids(['alliance'])
    if len(ids) > 0:
        return ids[0], 'alliance'
    return None, None