示例#1
0
def fetch_character_info(id):
    api = ESI()
    char = api.get("/v4/characters/%s/" % id)
    db_char = Character.objects.get(id=id)
    db_char.name = char['name']
    db_char.save()
    print "Fetched info for %s:%s" % (db_char.id, db_char.name)
示例#2
0
def fetch_alliance_info(id):
    api = ESI()
    alliance = api.get("/v3/alliances/%s/" % id)
    db_alliance = Alliance.objects.get(id=id)
    db_alliance.name = alliance['name']
    db_alliance.ticker = alliance['ticker']
    db_alliance.save()
    print "Fetched info for %s:%s" % (db_alliance.id, db_alliance.name)
示例#3
0
def fetch_corp_info(id):
    api = ESI()
    corp = api.get("/v4/corporations/%s/" % id)
    db_corp = Corporation.objects.get(id=id)
    db_corp.name = corp['name']
    db_corp.ticker = corp['ticker']
    db_corp.save()
    print "Fetched info for %s:%s" % (db_corp.id, db_corp.name)
示例#4
0
    def get_or_create(id):
        from capritools2.esi import ESI

        faction = Faction.objects.filter(id=id)
        if faction.count() == 0:
            api = ESI()
            factions = api.get("/v2/universe/factions/")
            faction = filter(lambda x: x['faction_id'] == id, factions)[0]
            faction = Faction(id=faction['faction_id'], name=faction['name'])
            faction.save()
            return faction
        else:
            return faction[0]
示例#5
0
def fleet_live_fetch_affiliations(ids, fleet_id):
    api = ESI()
    affiliations = api.post("/characters/affiliation/", data=json.dumps(ids))

    for affiliation in affiliations:
        db_member = Fleet_Member.objects.get(
            character_id=affiliation['character_id'], fleet_id=fleet_id)
        db_member.corporation = Corporation.get_or_create_async(
            affiliation['corporation_id'])
        if "alliance_id" in affiliation:
            db_member.alliance = Alliance.get_or_create_async(
                affiliation['alliance_id'])
        db_member.save()
示例#6
0
    def get_or_create(id, name=None):
        from capritools2.esi import ESI

        if name != None:
            alliance = Alliance(id=id, name=name)
            alliance.save()
            return alliance

        alliance = Alliance.objects.filter(id=id)
        if alliance.count() == 0:
            api = ESI()
            r = api.get("/alliances/%s/" % id)
            alliance = Alliance(id=id,
                                name=r['alliance_name'],
                                ticker=r['ticker'])
            alliance.save()
            return alliance
        else:
            return alliance[0]
示例#7
0
    def get_or_create(id, name=None):
        from capritools2.esi import ESI

        corporation = Corporation.objects.filter(id=id)
        if corporation.count() == 0:
            if name != None:
                corporation = Corporation(id=id, name=name)
                corporation.save()
                return corporation

            api = ESI()
            r = api.get("/v4/corporations/%s/" % id)
            corporation = Corporation(id=id,
                                      name=r['name'],
                                      ticker=r['ticker'])
            corporation.save()
            return corporation
        else:
            return corporation[0]
示例#8
0
class LocalScanParser:
    scan = None
    api = ESI()

    def __init__(self):
        eveapi.set_user_agent("eveapi.py/1.3")
        self.xmlapi = eveapi.EVEAPIConnection()


    def parse(self, input):
        # Create initial anchor key
        self.scan = LocalScan(key=random_key(7))
        self.scan.save()

        # Hit the API for characterIDs
        affiliations = Set()
        charAffiliations = []
        names=input.split("\r\n")

        for chunk in [names[x:x+100] for x in xrange(0, len(names), 100)]:
            ids = self.api.post(
                "/v1/universe/ids/",
                data=json.dumps(
                    chunk
                )
            )

            ids = map(lambda x: x['id'], ids['characters'])
            charAffiliations = charAffiliations + self.api.post("/v1/characters/affiliation/", data=json.dumps(ids))

        # Parse results
        added = 0
        if charAffiliations == None:
            self.scan.delete()
            return False

        with transaction.atomic():
            for i, affiliation in enumerate(charAffiliations):
                # Populate and retrieve objects in the DB
                char = LocalScanChar(scan=self.scan)
                char.corporation = Corporation.get_or_create(affiliation['corporation_id'])
                if "alliance_id" in affiliation:
                    char.alliance = Alliance.get_or_create(affiliation['alliance_id'])
                    affiliations.add((affiliation['corporation_id'], affiliation['alliance_id']))
                if "faction_id" in affiliation:
                    char.faction = Faction.get_or_create(affiliation['faction_id'])

                added += 1
                char.save()

            for corp_id, alliance_id in affiliations:
                db_affiliation = Affiliation(
                    scan=self.scan,
                    corporation_id=corp_id,
                    alliance_id=alliance_id
                )
                db_affiliation.save()

            if added > 0:
                return True
            else:
                self.scan.save()
                return False
示例#9
0
def fleet_live_update(fleet_id):
    db_fleet = Fleet.objects.get(id=fleet_id)
    api = ESI(db_fleet.token, db_fleet.id)
    api.cache_time = 0

    # Update Fleet info
    old_members = set(map(lambda x: x.character, db_fleet.members.all()))
    fleet = api.get("/v1/fleets/$fleet/")
    if fleet == None:
        db_fleet.active = False
        db_fleet.save()
        return

    db_fleet.motd = fleet['motd']
    db_fleet.voice_enabled = fleet['is_voice_enabled']
    db_fleet.registered = fleet['is_registered']
    db_fleet.free_move = fleet['is_free_move']
    db_fleet.save()

    # Update fleet structure
    wings = api.get("/v1/fleets/$fleet/wings/")
    db_fleet.wings.exclude(id__in=map(lambda x: x['id'], wings)).delete()
    for wing in wings:
        db_wing = Fleet_Wing.get_or_create(wing['id'], db_fleet, wing['name'])
        db_wing.name = wing['name']
        db_wing.save()

        # Squads
        db_wing.squads.exclude(
            id__in=map(lambda x: x['id'], wing['squads'])).delete()
        for squad in wing['squads']:
            db_squad = Fleet_Squad.get_or_create(squad['id'], db_fleet,
                                                 db_wing, squad['name'])
            db_squad.wing = db_wing
            db_squad.name = squad['name']
            db_squad.save()

    # Update members
    members = api.get("/v1/fleets/$fleet/members/")
    affiliations = []
    new_members = set(
        map(lambda x: Character.get_or_create_async(x['character_id']),
            members))
    db_fleet.members.exclude(character__in=new_members).delete()
    for member in members:
        try:
            db_member = db_fleet.members.get(
                character_id=member['character_id'])
        except Exception:
            db_member = Fleet_Member(fleet=db_fleet,
                                     character=Character.get_or_create_async(
                                         id=member['character_id']))

        # Fleet Position
        if member['wing_id'] > 0:
            db_member.wing = Fleet_Wing.objects.get(id=member['wing_id'])
        else:
            db_member.wing = None
        if member['squad_id'] > 0:
            db_member.squad = Fleet_Squad.objects.get(id=member['squad_id'])
        else:
            db_member.squad = None
        db_member.boss = member['role_name'].endswith("(Boss)")
        db_member.role = member['role']
        db_member.role_name = member['role_name']

        # Character
        if db_member.corporation == None:
            affiliations.append(db_member.character_id)

        # Meta
        if db_member.system != None:
            if db_member.system_id != member['solar_system_id']:
                Fleet_Jump(fleet=db_fleet,
                           character=db_member.character,
                           from_system=db_member.system,
                           to_system_id=member['solar_system_id']).save()
        db_member.system_id = member['solar_system_id']

        if db_member.ship != None:
            if db_member.ship_id != member['ship_type_id']:
                Fleet_ShipChange(fleet=db_fleet,
                                 character=db_member.character,
                                 from_ship=db_member.ship,
                                 to_ship_id=member['ship_type_id']).save()
        db_member.ship_id = member['ship_type_id']

        if db_member.takes_fleet_warp != None:
            if db_member.takes_fleet_warp != member['takes_fleet_warp']:
                if member['takes_fleet_warp']:
                    Fleet_MemberEvent(fleet=db_fleet,
                                      character=db_member.character,
                                      event="accept_fleetwarp").save()
                else:
                    Fleet_MemberEvent(fleet=db_fleet,
                                      character=db_member.character,
                                      event="exempt_fleetwarp").save()
            db_member.takes_fleet_warp = member['takes_fleet_warp']

        db_member.save()

    # Create join/leave fleet events
    for character in old_members - new_members:
        Fleet_MemberEvent(fleet=db_fleet, character=character,
                          event="leave").save()

    for character in new_members - old_members:
        Fleet_MemberEvent(fleet=db_fleet, character=character,
                          event="join").save()

    # Get the character affiliations async
    if len(affiliations) > 0:
        fleet_live_fetch_affiliations.delay(affiliations, fleet_id)

    print "Updated fleet %s" % fleet_id