def update_adjusted_price(): """ Get market prices from ESI and update the database. Raises exception if database cannot be updated. Returns ------- Dict the prices that were updated. """ item_adjusted_price = {} count = 0 market_price = esiclient.request(get_markets_prices()) if market_price.status == 200: for item_price in market_price.data: count += 1 item_adjusted_price[item_price.type_id] = { 'item_id': item_price.type_id, 'price': item_price.adjusted_price or 0.00 } db.engine.execute("TRUNCATE TABLE %s" % ItemAdjustedPrice.__tablename__) db.engine.execute(ItemAdjustedPrice.__table__.insert(), list(item_adjusted_price.values())) db.session.commit() return item_adjusted_price return None
def task_update_adjusted_price(self): """ Task that update the adjusted prices from the API """ self.start() item_adjusted_price = [] count = 0 market_price = esiclient.request(get_markets_prices()) if market_price.status == 200: for item_price in market_price.data: count += 1 item_adjusted_price.append({ 'item_id': item_price.type_id, 'price': item_price.adjusted_price or 0.00 }) db.engine.execute("TRUNCATE TABLE %s" % ItemAdjustedPrice.__tablename__) db.engine.execute(ItemAdjustedPrice.__table__.insert(), item_adjusted_price) db.session.commit() self.end(TaskState.SUCCESS) else: self.end(TaskState.ERROR)
def task_industry_indexes(): """ Get the industry indexes list from API. """ all_indexes = esiclient.request(get_industry_systems()) insert_index_list = [] if all_indexes.status == 200: for index in all_indexes.data: solar_system = index.solar_system_id for activity_index in index.cost_indices: row = {} row['solarsystem_id'] = solar_system row['activity'] = IndustryIndex.activity_string_to_activity( activity_index.activity) row['cost_index'] = activity_index.cost_index insert_index_list.append(row) try: db.engine.execute("TRUNCATE TABLE %s" % IndustryIndex.__tablename__) db.engine.execute(IndustryIndex.__table__.insert(), insert_index_list) db.session.commit() except SQLAlchemyError: db.session.rollback() logger.exception("Error while updating indexes")
def task_update_industry_indexes(self): """ Get the industry indexes list from API. """ self.start() all_indexes = esiclient.request(get_industry_systems()) insert_index_list = [] if all_indexes.status == 200: for index in all_indexes.data: solar_system = index.solar_system_id for activity_index in index.cost_indices: row = {} row['solarsystem_id'] = solar_system row['activity'] = IndustryIndex.activity_string_to_activity( activity_index.activity) row['cost_index'] = activity_index.cost_index insert_index_list.append(row) db.engine.execute("TRUNCATE TABLE %s" % IndustryIndex.__tablename__) db.engine.execute(IndustryIndex.__table__.insert(), insert_index_list) db.session.commit() self.end(TaskState.SUCCESS) else: self.end(TaskState.ERROR)
def task_update_character_skills(character_id): """ Update the skills for a given character_id """ character = User.query.get(character_id) if character is None: return # get token token = get_token_update_esipy(character_id=character_id, scope=TokenScope.SCOPE_SKILL) # get current character skills from ESI character_skills = esiclient.request( get_characters_skills(character_id=character_id), ) if character_skills.status == 200: for skill_object in character_skills.data.skills: char_skill = character.skills.filter( Skill.skill_id == skill_object.skill_id).one_or_none() if char_skill: char_skill.level = skill_object.active_skill_level else: skill = Skill( character=character, skill_id=skill_object.skill_id, level=skill_object.active_skill_level, ) db.session.merge(skill) db.session.commit() update_token_state(token, character_skills.header['Expires'][0]) else: inc_fail_token_scope(token, character_skills.status)
def task_update_character_skills(self, character_id): """ Update the skills for a given character_id """ self.start() skill_number = 0 character = User.query.get(character_id) if character is None: return # get token token = self.get_token_update_esipy( character_id=character_id, scope=TokenScope.SCOPE_SKILL ) # get current character skills from ESI character_skills = esiclient.request( get_characters_skills(character_id=character_id), ) if character_skills.status == 200: for skill_object in character_skills.data.skills: char_skill = character.skills.filter( Skill.skill_id == skill_object.skill_id ).one_or_none() if char_skill: char_skill.level = skill_object.active_skill_level else: skill = Skill( character=character, skill_id=skill_object.skill_id, level=skill_object.active_skill_level, ) db.session.merge(skill) skill_number += 1 db.session.commit() else: self.inc_fail_token_scope(token, character_skills.status) self.end(TaskState.ERROR) return # update the token and the state token.request_try = 0 token.last_update = utcnow() token.cached_until = datetime( *parsedate(character_skills.header['Expires'][0])[:6] ).replace(tzinfo=pytz.utc) db.session.commit() self.end(TaskState.SUCCESS)
def task_update_character_skills(self, character_id): """ Update the skills for a given character_id """ self.start() skill_number = 0 character = User.query.get(character_id) if character is None: return # get token token = self.get_token_update_esipy(character_id=character_id, scope=TokenScope.SCOPE_SKILL) # get current character skills from ESI character_skills = esiclient.request( get_characters_skills(character_id=character_id), ) if character_skills.status == 200: for skill_object in character_skills.data.skills: char_skill = character.skills.filter( Skill.skill_id == skill_object.skill_id).one_or_none() if char_skill: char_skill.level = skill_object.active_skill_level else: skill = Skill( character=character, skill_id=skill_object.skill_id, level=skill_object.active_skill_level, ) db.session.merge(skill) skill_number += 1 db.session.commit() else: self.inc_fail_token_scope(token, character_skills.status) self.end(TaskState.ERROR) return # update the token and the state token.request_try = 0 token.last_update = utcnow() token.cached_until = datetime( *parsedate(character_skills.header['Expires'][0])[:6]).replace( tzinfo=pytz.utc) db.session.commit() self.end(TaskState.SUCCESS)
def is_server_online(): """ return true if server looks online, else otherwise """ res = esiclient.request(get_status()) return (res.status == 200 and 'vip' not in res.data)
def is_server_online(): """ return true if server looks online, else otherwise """ op = esiapp.op['get_status']() res = esiclient.request(op) return (res.status == 200 and 'vip' not in res.data)
def task_update_corporation_blueprints(character_id): """ Update the skills for a given character_id """ character = User.query.get(character_id) if character is None: return # get token token = get_token_update_esipy(character_id=character_id, scope=TokenScope.SCOPE_CORP_BLUEPRINTS) # check char roles op = get_characters_roles(character_id=character_id) res = esiclient.request(op) if (res.status != 200 or (res.status == 200 and 'Director' not in res.data.roles)): inc_fail_token_scope(token, res.status) return character.is_corp_director = True # check char corporation op = get_characters(character_id=character_id) res = esiclient.request(op) if res.status != 200 and character.corporation_id is None: inc_fail_token_scope(token, res.status) return character.corporation_id = res.data.corporation_id # get current blueprints bps = Blueprint.query.filter_by(character_id=character_id).filter_by( corporation=True).all() blueprints = {} for bp in bps: key = "%s-%d-%d-%d" % (bp.item_id, bp.original, bp.material_efficiency, bp.time_efficiency) # update run to 0, to have the real total run for bpc if not bp.original: bp.total_runs = 0 blueprints[key] = bp # set of known blueprints blueprint_init_list = set(blueprints.keys()) blueprint_updated_list = set() # get the first page to have the page number op_blueprint = get_corporations_blueprints( corporation_id=character.corporation_id, page=1) bp_one = esiclient.request(op_blueprint) if bp_one.status != 200: inc_fail_token_scope(token, bp_one.status_code) logger.error( 'Request failed [%s, %s, %d]: %s', op_blueprint[0].url, op_blueprint[0].query, bp_one.status, bp_one.raw, ) return # prepare all other pages total_page = bp_one.header['X-Pages'][0] operations = [] for page in range(2, total_page + 1): operations.append( get_corporations_blueprints( corporation_id=character.corporation_id, page=page)) # query all other pages and add the first page bp_list = esiclient.multi_request(operations) # parse the response and save everything for _, response in [(op_blueprint[0], bp_one)] + bp_list: for blueprint in response.data: original = (blueprint.quantity != -2) runs = blueprint.runs me = blueprint.material_efficiency te = blueprint.time_efficiency item_id = blueprint.type_id key = "%s-%d-%d-%d" % (item_id, original, me, te) if key not in blueprint_updated_list: blueprint_updated_list.add(key) if key not in blueprints: blueprints[key] = Blueprint( item_id=item_id, original=original, total_runs=runs, material_efficiency=me, time_efficiency=te, character_id=character_id, corporation=True, ) try: db.session.add(blueprints[key]) db.session.commit() except SQLAlchemyError: logger.exception( "Error while trying to add blueprint id: %d", item_id) continue if not original: blueprints[key].total_runs += runs # delete every blueprint that have not been updated for key in (blueprint_init_list - blueprint_updated_list): db.session.delete(blueprints[key]) try: db.session.commit() except SQLAlchemyError: logger.exception("Error while trying to delete unused blueprints") # update the token and the state update_token_state(token, bp_one.header['Expires'][0])
def task_update_character_blueprints(self, character_id): """ Update the skills for a given character_id """ self.start() character = User.query.get(character_id) if character is None: return # get token token = self.get_token_update_esipy(character_id=character_id, scope=TokenScope.SCOPE_CHAR_BLUEPRINTS) # get current blueprints bps = Blueprint.query.filter_by(character_id=character_id).filter_by( corporation=False).all() blueprints = {} for bp in bps: key = "%s-%d-%d-%d" % (bp.item_id, bp.original, bp.material_efficiency, bp.time_efficiency) # update run to 0, to have the real total run for bpc if not bp.original: bp.total_runs = 0 blueprints[key] = bp # set of known blueprints blueprint_init_list = set(blueprints.keys()) blueprint_updated_list = set() # get the first page to have the page number op_blueprint = get_characters_blueprints(character_id=character_id, page=1) bp_one = esiclient.request(op_blueprint) if bp_one.status != 200: logger.error('Request failed [%s, %s, %d]: %s' % ( op_blueprint[0].url, op_blueprint[0].query, bp_one.status, bp_one.raw, )) self.end(TaskState.ERROR) return # prepare all other pages total_page = bp_one.header['X-Pages'][0] operations = [] for page in range(2, total_page + 1): operations.append( get_characters_blueprints(character_id=character_id, page=page)) # query all other pages and add the first page bp_list = esiclient.multi_request(operations) # parse the response and save everything for req, response in [(op_blueprint[0], bp_one)] + bp_list: for blueprint in response.data: original = (blueprint.quantity != -2) runs = blueprint.runs me = blueprint.material_efficiency te = blueprint.time_efficiency item_id = blueprint.type_id key = "%s-%d-%d-%d" % (item_id, original, me, te) if key not in blueprint_updated_list: blueprint_updated_list.add(key) if key not in blueprints: blueprints[key] = Blueprint( item_id=item_id, original=original, total_runs=runs, material_efficiency=me, time_efficiency=te, character_id=character_id, ) db.session.add(blueprints[key]) continue if not original: blueprints[key].total_runs += runs # delete every blueprint that have not been updated for key in (blueprint_init_list - blueprint_updated_list): db.session.delete(blueprints[key]) # update the token and the state token.request_try = 0 token.last_update = utcnow() token.cached_until = datetime( *parsedate(bp_one.header['Expires'][0])[:6]).replace(tzinfo=pytz.utc) db.session.commit() self.end(TaskState.SUCCESS)