示例#1
0
def update_character(character_id):
    # Get the db objects we need
    db_char = Character.objects.get(id=character_id)

    # Grab public info from API
    api = ESI()
    char = api.get("/v4/characters/%s/" % db_char.id)

    db_char.name = char['name']
    db_char.corp = Corporation.get_or_create(char['corporation_id'])
    if "alliance_id" in char:
        db_char.alliance = Alliance.get_or_create(char['alliance_id'])
    else:
        db_char.alliance = None
    db_char.save()

    # Grab non-public info
    if db_char.token != None:
        api = ESI(db_char.token)

        # Wallet
        db_char.wallet = api.get("/v1/characters/$id/wallet/")

        # Location
        location = api.get("/v1/characters/$id/location/")
        db_char.system_id = location['solar_system_id']

        # Ship
        ship = api.get("/v1/characters/$id/ship/")
        db_char.ship_id = ship['ship_type_id']

        # Fatigue
        fatigue = api.get("/v1/characters/$id/fatigue/")
        if "jump_fatigue_expire_date" in fatigue:
            db_char.fatigue_expire_date = parse_api_date(fatigue['jump_fatigue_expire_date'])
            db_char.last_jump_date = parse_api_date(fatigue['last_jump_date'])

        db_char.save()

        # Skills
        skills = api.get("/v4/characters/$id/skills/")
        with transaction.atomic():
            Skill.objects.filter(character=db_char).delete()
            for skill in skills['skills']:
                Skill(
                    character=db_char,
                    type_id=skill['skill_id'],
                    trained_skill_level=skill['trained_skill_level'],
                    active_skill_level=skill['active_skill_level'],
                    skillpoints_in_skill=skill['skillpoints_in_skill']
                ).save()

        # Assets
        with transaction.atomic():
            Asset.objects.filter(character=db_char).delete()

            page = 1
            while True:
                assets = api.get("/v3/characters/$id/assets/", get_vars={'page': page})
                if len(assets) < 1:
                    break

                for asset in assets:
                    db_asset = Asset(
                        character=db_char,
                        id=asset['item_id'],
                        type_id=asset['type_id'],
                        flag=asset['location_flag'],
                        quantity=asset['quantity'],
                        raw_quantity=asset['quantity'],
                        singleton=asset['is_singleton'],
                    )

                    if asset['location_flag'] == "Hangar":
                        station = Station.get_or_create(asset['location_id'], api)
                        db_asset.system = station.system
                    else:
                        db_asset.parent_id = asset['location_id']
                    db_asset.save()

                page = page + 1

            # Fetch names for all ships/containers
            items = list(
                Asset.objects.filter(
                    Q(character=db_char),
                    Q(type__group__category_id=6) | Q(type__group__in=[12 , 340, 448])
                ).values_list(
                    'id',
                    flat=True
                )
            )

            asset_names = api.post("/v1/characters/$id/assets/names/", data=json.dumps(items))
            for asset in asset_names:
                db_asset = Asset.objects.get(id=asset['item_id'])
                db_asset.name = asset['name']
                db_asset.save()

            # Fix systems
            db_assets = Asset.objects.filter(
                character=db_char,
                system__isnull=True
            ).all()
            for db_asset in db_assets:
                try:
                    if db_asset.parent != None:
                        db_asset.system = db_asset.parent.system
                except Asset.DoesNotExist:
                    pass
                    #print db_asset.parent_id
                db_asset.save()

        print "Updated all info for character %s" % db_char.name
示例#2
0
def update_character(character_id):
    # Get the db objects we need
    db_char = Character.objects.get(id=character_id)

    # Grab public info from API
    api = ESI()
    char = api.get("/v4/characters/%s/" % db_char.id)

    db_char.name = char['name']
    db_char.corp = Corporation.get_or_create(char['corporation_id'])
    if "alliance_id" in char:
        db_char.alliance = Alliance.get_or_create(char['alliance_id'])
    else:
        db_char.alliance = None
    db_char.save()

    # Grab non-public info
    if db_char.token != None:
        api = ESI(db_char.token)

        # Check refresh token to see if it's still valid
        if api._refresh_access_token() == False:
            # Check if we've had 24 within the last 72hrs failures. This runs hourly so that means EVE would need to be dead for a full day.
            cache_key = "fail_history_character_%s" % db_char.id
            fail_history = cache.get(cache_key, [])
            if len(fail_history) > 24:
                token = db_char.token

                user = db_char.owner

                db_char.owner = None
                db_char.token = None
                db_char.save()

                token.delete()

                Webhook.send("character_expired",
                             embeds.character_expired(user, db_char))

                fail_history = []
            else:
                fail_history.append(now())

            cache.set(cache_key, fail_history, 259200)
            return

        # Wallet
        db_char.wallet = api.get("/v1/characters/$id/wallet/")

        # Location
        location = api.get("/v1/characters/$id/location/")
        db_char.system_id = location['solar_system_id']

        # Ship
        ship = api.get("/v1/characters/$id/ship/")
        db_char.ship_id = ship['ship_type_id']

        # Fatigue
        fatigue = api.get("/v1/characters/$id/fatigue/")
        if "jump_fatigue_expire_date" in fatigue:
            db_char.fatigue_expire_date = parse_api_date(
                fatigue['jump_fatigue_expire_date'])
            db_char.last_jump_date = parse_api_date(fatigue['last_jump_date'])

        # Roles
        with transaction.atomic():
            roles = api.get("/v2/characters/$id/roles/")
            db_char.roles.all().delete()

            if "roles" in roles:
                for role in roles['roles']:
                    Role(character=db_char, name=role).save()

        db_char.save()

        # Skills
        skills = api.get("/v4/characters/$id/skills/")
        with transaction.atomic():
            Skill.objects.filter(character=db_char).delete()
            for skill in skills['skills']:
                Skill(
                    character=db_char,
                    type_id=skill['skill_id'],
                    trained_skill_level=skill['trained_skill_level'],
                    active_skill_level=skill['active_skill_level'],
                    skillpoints_in_skill=skill['skillpoints_in_skill']).save()

        # Assets
        with transaction.atomic():
            Asset.objects.filter(character=db_char).delete()

            page = 1
            while True:
                assets = api.get("/v3/characters/$id/assets/",
                                 get_vars={'page': page})
                if len(assets) < 1:
                    break

                for asset in assets:
                    db_asset = Asset(
                        character=db_char,
                        id=asset['item_id'],
                        type_id=asset['type_id'],
                        flag=asset['location_flag'],
                        quantity=asset['quantity'],
                        raw_quantity=asset['quantity'],
                        singleton=asset['is_singleton'],
                    )

                    if asset['location_flag'] == "Hangar":
                        station = Station.get_or_create(
                            asset['location_id'], api)
                        db_asset.system = station.system
                    else:
                        db_asset.parent_id = asset['location_id']
                    db_asset.save()

                page = page + 1

            # Fetch names for all ships/containers
            items = list(
                Asset.objects.filter(
                    Q(character=db_char),
                    Q(type__group__category_id=6)
                    | Q(type__group__in=[12, 340, 448])).values_list(
                        'id', flat=True))

            asset_names = api.post("/v1/characters/$id/assets/names/",
                                   data=json.dumps(items))
            for asset in asset_names:
                db_asset = Asset.objects.get(id=asset['item_id'])
                db_asset.name = asset['name']
                db_asset.save()

            # Fix systems
            db_assets = Asset.objects.filter(character=db_char,
                                             system__isnull=True).all()
            for db_asset in db_assets:
                try:
                    if db_asset.parent != None:
                        db_asset.system = db_asset.parent.system
                except Asset.DoesNotExist:
                    pass
                    #print db_asset.parent_id
                db_asset.save()

        # Implants
        with transaction.atomic():
            implants = api.get("/v1/characters/$id/implants/")
            db_char.implants.all().delete()
            for implant in implants:
                Implant(character=db_char, type_id=implant).save()

        # Clones
        info_sync = db_char.skills.filter(type_id=33399).first()
        if info_sync != None:
            info_sync = timedelta(hours=info_sync.trained_skill_level)
        else:
            info_sync = timedelta(hours=0)

        clones = api.get("/v3/characters/$id/clones/")
        db_char.home = Station.get_or_create(
            clones['home_location']['location_id'], api)
        if "last_clone_jump_date" in clones:
            db_char.clone_jump_ready = parse_api_date(
                clones['last_clone_jump_date']) - info_sync + timedelta(
                    hours=24)
        db_char.save()

        db_char.clones.all().delete()
        for clone in clones['jump_clones']:
            if "name" in clone:
                name = clone['name']
            else:
                name = ""

            db_clone = Clone(id=clone['jump_clone_id'],
                             character=db_char,
                             name=name,
                             location=Station.get_or_create(
                                 clone['location_id'], api))
            db_clone.save()

            for implant in clone['implants']:
                CloneImplant(clone=db_clone, type_id=implant).save()

        print "Updated all info for character %s" % db_char.name