Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @blueprints_bp.route('/update_prices', methods=['POST'])
- def update_prices():
- try:
- access_token = session.get("token")
- headers = {
- 'User-Agent': 'ManuOptimizer 1.0 [email protected]'
- }
- if access_token:
- headers['Authorization'] = f'Bearer {access_token}'
- from sqlalchemy import or_
- materials = Material.query.filter(
- or_(
- Material.category != "Invention Materials",
- Material.category == None # include uncategorized
- )
- ).all()
- blueprints = BlueprintModel.query.all()
- unique_type_ids = set()
- material_jobs = []
- blueprint_jobs = []
- # Stage material fetch jobs (always Jita)
- for mat in materials:
- if mat.type_id:
- unique_type_ids.add(mat.type_id)
- material_jobs.append((mat.type_id, None))
- # Stage blueprint fetch jobs
- for bp in blueprints:
- if not bp.type_id:
- logger.debug(f"bp.name raw repr: {repr(bp.name)}")
- logger.debug(f"bp.name bytes: {[hex(ord(c)) for c in bp.name]}")
- info = get_material_info([bp.name])
- logger.debug(f"Lookup result for '{bp.name}': {info}")
- bp.type_id = info[normalize_name(bp.name)]['type_id']
- if bp.type_id:
- unique_type_ids.add(bp.type_id)
- if not bp.use_jita_sell and bp.station_id:
- blueprint_jobs.append((bp.type_id, bp.station_id))
- else:
- blueprint_jobs.append((bp.type_id, None))
- prices = {}
- def fetch_price(type_id, station_id):
- if station_id:
- return type_id, get_station_sell_price(type_id, station_id, headers)
- else:
- return type_id, get_lowest_jita_sell_price(type_id, headers)
- jobs = material_jobs + blueprint_jobs
- with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
- futures = {executor.submit(fetch_price, tid, sid): tid for tid, sid in jobs}
- for future in concurrent.futures.as_completed(futures):
- tid, price = future.result()
- if price is not None:
- prices[tid] = price
- # Cache material prices for quick cost calculation
- material_price_map = {}
- for mat in materials:
- if mat.type_id in prices:
- mat.sell_price = prices[mat.type_id]
- material_price_map[mat.type_id] = mat.sell_price
- else:
- logger.warning(f"No price found for material {mat.name}")
- for bp in blueprints:
- if bp.type_id in prices:
- bp.sell_price = prices[bp.type_id]
- else:
- logger.warning(f"No price found for blueprint {bp.name}")
- total_cost = 0.0
- name_to_type_id = {mat.name: mat.type_id for mat in materials}
- for category, materials_dict in bp.materials.items():
- for mat_name, qty in materials_dict.items():
- type_id = name_to_type_id.get(mat_name)
- if type_id is not None:
- price = material_price_map.get(type_id)
- else:
- price = None
- if price:
- total_cost += price * qty
- else:
- logger.warning(f"No price for material {mat_name} (type_id: {type_id}) in blueprint {bp.name}")
- bp.material_cost = round(total_cost, 2)
- db.session.commit()
- return jsonify({"message": "Prices updated successfully"}), 200
- except Exception as e:
- db.session.rollback()
- logger.error("Error updating prices", exc_info=True)
- return jsonify({"error": "An error occurred while updating prices"}), 500
Advertisement
Add Comment
Please, Sign In to add comment