Example #1
0
def fetch_alliance_kills(id):
    alliance = Alliance.get_or_create(id)
    url = "https://zkillboard.com/api/allianceID/%s/page/%s/"
    _fetch_kills(alliance, url)
Example #2
0
def parse_redisq(json):
    package = ujson.loads(json)['package']
    killmail = package['killmail']
    victim = killmail['victim']
    zkb = package['zkb']

    # Check the KM doesn't already exist
    if Killmail.objects.filter(id=package['killID']).count() > 0:
        print("Kill ID %s already exists" % package['killID'])
        return

    # Populate killmail
    km = Killmail(id=package['killID'],
                  source_id=1,
                  date=parse_datetime(killmail['killmail_time']),
                  system_id=killmail['solar_system_id'],
                  ship_id=victim['ship_type_id'],
                  value=zkb['totalValue'],
                  damage=victim['damage_taken'],
                  attackers=len(killmail['attackers']))

    if "position" in victim:
        km.x = victim['position']['x']
        km.y = victim['position']['y']
        km.z = victim['position']['z']

    db_victim = Involved(kill=km,
                         attacker=False,
                         ship_id=victim['ship_type_id'],
                         damage=0)
    if "character_id" in victim:
        db_victim.character = Character.get_or_create(victim['character_id'])
    if "corporation_id" in victim:
        db_victim.corporation = Corporation.get_or_create(
            victim['corporation_id'])
    if "alliance_id" in victim:
        db_victim.alliance = Alliance.get_or_create(victim['alliance_id'])
    km.save()

    # Populate Involved
    attackers = [db_victim]
    for attacker in killmail['attackers']:
        a = Involved(kill=km, damage=attacker['damage_done'])

        if "final_blow" in attacker:
            a.final_blow = attacker['final_blow']
        if "character_id" in attacker:
            a.character = Character.get_or_create(attacker['character_id'])
        if "corporation_id" in attacker:
            a.corporation = Corporation.get_or_create(
                attacker['corporation_id'])
        if "alliance_id" in attacker:
            a.alliance = Alliance.get_or_create(attacker['alliance_id'])

        if "ship_type_id" in attacker:
            a.ship_id = attacker['ship_type_id']
        if "weapon_type_id" in attacker:
            a.weapon_id = attacker['weapon_type_id']
        #a.save()
        attackers.append(a)
    Involved.objects.bulk_create(attackers)

    # Populate Items
    items = []
    for item in victim['items']:
        i = Item(kill=km,
                 type_id=item['item_type_id'],
                 singleton=item['singleton'],
                 flag=item['flag'])

        if "quantity_dropped" in item:
            i.quantity = item['quantity_dropped']
        if "quantity_destroyed" in item:
            i.quantity = item['quantity_destroyed']
        #i.save()
        items.append(i)
    Item.objects.bulk_create(items)

    print("Added Kill ID %s on %s with %s involved from Redisq" %
          (km.id, km.date.strftime("%d/%m/%Y %H:%M"), len(attackers)))
Example #3
0
def parse_esi(json=None, keyhash=None, attempts=0, source_id=3):
    if json != None:
        package = ujson.loads(json)
        victim = package['victim']
        if Killmail.objects.filter(id=package['killmail_id']).count() > 0:
            print("Kill ID %s already exists" % package['killmail_id'])
            return
    else:
        if Killmail.objects.filter(id=keyhash[0]).count() > 0:
            print("Kill ID %s already exists" % keyhash[0])
            return

        package = requests.get(
            "https://esi.tech.ccp.is/latest/killmails/%s/%s/" %
            (keyhash[0], keyhash[1]))
        if package.status_code != 200:
            if attempts < 5:
                sleep(1)
                return parse_esi(keyhash=keyhash, attempts=attempts + 1)
            else:
                print("Error fetching kill ID %s from ESI" % keyhash[0])
                return None
        package = package.json()
        victim = package['victim']

    # Pre-load char/corp/alliance sets so we can fill the database
    chars = set()
    corps = set()
    alliances = set()

    if "character_id" in victim:
        chars.add(victim['character_id'])
    if "corporation_id" in victim:
        corps.add(victim['corporation_id'])
    if "alliance_id" in victim:
        alliances.add(victim['alliance_id'])

    for attacker in package['attackers']:
        if "character_id" in attacker:
            chars.add(attacker['character_id'])
        if "corporation_id" in attacker:
            corps.add(attacker['corporation_id'])
        if "alliance_id" in attacker:
            alliances.add(attacker['alliance_id'])

    # Now call get_or_create to preload them in the database
    for char in chars - set(
            Character.objects.filter(id__in=chars).values_list('id',
                                                               flat=True)):
        Character.get_or_create(char)
    for corp in corps - set(
            Corporation.objects.filter(id__in=corps).values_list('id',
                                                                 flat=True)):
        Corporation.get_or_create(corp)
    for alliance in alliances - set(
            Alliance.objects.filter(id__in=alliances).values_list('id',
                                                                  flat=True)):
        Alliance.get_or_create(alliance)

    # Populate killmail and data within transaction
    with transaction.atomic():
        km = Killmail(id=package['killmail_id'],
                      source_id=source_id,
                      date=parse_datetime(package['killmail_time']),
                      system_id=package['solar_system_id'],
                      ship_id=victim['ship_type_id'],
                      value=0,
                      damage=victim['damage_taken'],
                      attackers=len(package['attackers']))

        if "position" in victim:
            if "x" in victim['position']:
                km.x = victim['position']['x']
                km.y = victim['position']['y']
                km.z = victim['position']['z']

        db_victim = Involved(kill=km,
                             attacker=False,
                             ship_id=victim['ship_type_id'],
                             damage=0)
        if "character_id" in victim:
            db_victim.character = Character.get_or_create(
                victim['character_id'])
        if "corporation_id" in victim:
            db_victim.corporation = Corporation.get_or_create(
                victim['corporation_id'])
        if "alliance_id" in victim:
            db_victim.alliance = Alliance.get_or_create(victim['alliance_id'])

        km.save()

        # Populate attackers
        attackers = [db_victim]
        for attacker in package['attackers']:
            a = Involved(kill=km, damage=attacker['damage_done'])

            if "final_blow" in attacker:
                a.final_blow = attacker['final_blow']
            if "character_id" in attacker:
                a.character_id = attacker['character_id']
            if "corporation_id" in attacker:
                a.corporation_id = attacker['corporation_id']
            if "alliance_id" in attacker:
                a.alliance_id = attacker['alliance_id']

            if "ship_type_id" in attacker:
                a.ship_id = attacker['ship_type_id']
            if "weapon_type_id" in attacker:
                a.weapon_id = attacker['weapon_type_id']
            #a.save()
            attackers.append(a)
        Involved.objects.bulk_create(attackers)

        # Populate Items
        items = []
        for item in victim['items']:
            i = Item(kill=km,
                     type_id=item['item_type_id'],
                     singleton=item['singleton'],
                     flag=item['flag'])

            if "quantity_dropped" in item:
                i.quantity = item['quantity_dropped']
            if "quantity_destroyed" in item:
                i.quantity = item['quantity_destroyed']
            #i.save()
            items.append(i)
        Item.objects.bulk_create(items)

        # Calculate the cost of the ship
        km.value = km.items.annotate(value=ExpressionWrapper(
            F('type__sell') *
            F('quantity'), output_field=FloatField())).aggregate(
                total_value=Sum('value'))['total_value']
        if km.value == None:
            km.value = float(km.ship.sell)
        else:
            km.value = km.value + float(km.ship.sell)
        km.save()

    print("Added Kill ID %s on %s with %s involved from ESI" %
          (km.id, km.date.strftime("%d/%m/%Y %H:%M"), len(attackers)))

    return km