Guest User

Untitled

a guest
Jun 11th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. @blueprints_bp.route('/update_prices', methods=['POST'])
  2. def update_prices():
  3.     try:
  4.         access_token = session.get("token")
  5.         headers = {
  6.             'User-Agent': 'ManuOptimizer 1.0 [email protected]'  
  7.         }
  8.         if access_token:
  9.             headers['Authorization'] = f'Bearer {access_token}'
  10.  
  11.         from sqlalchemy import or_
  12.  
  13.         materials = Material.query.filter(
  14.             or_(
  15.                 Material.category != "Invention Materials",
  16.                 Material.category == None  # include uncategorized
  17.             )
  18.         ).all()
  19.  
  20.         blueprints = BlueprintModel.query.all()
  21.  
  22.         unique_type_ids = set()
  23.         material_jobs = []
  24.         blueprint_jobs = []
  25.  
  26.         # Stage material fetch jobs (always Jita)
  27.         for mat in materials:
  28.             if mat.type_id:
  29.                 unique_type_ids.add(mat.type_id)
  30.                 material_jobs.append((mat.type_id, None))
  31.  
  32.         # Stage blueprint fetch jobs
  33.         for bp in blueprints:
  34.             if not bp.type_id:
  35.                 logger.debug(f"bp.name raw repr: {repr(bp.name)}")
  36.                 logger.debug(f"bp.name bytes: {[hex(ord(c)) for c in bp.name]}")
  37.                 info = get_material_info([bp.name])
  38.                 logger.debug(f"Lookup result for '{bp.name}': {info}")
  39.                 bp.type_id = info[normalize_name(bp.name)]['type_id']
  40.             if bp.type_id:
  41.                 unique_type_ids.add(bp.type_id)
  42.                 if not bp.use_jita_sell and bp.station_id:
  43.                     blueprint_jobs.append((bp.type_id, bp.station_id))
  44.                 else:
  45.                     blueprint_jobs.append((bp.type_id, None))
  46.  
  47.         prices = {}
  48.  
  49.         def fetch_price(type_id, station_id):
  50.             if station_id:
  51.                 return type_id, get_station_sell_price(type_id, station_id, headers)
  52.             else:
  53.                 return type_id, get_lowest_jita_sell_price(type_id, headers)
  54.  
  55.         jobs = material_jobs + blueprint_jobs
  56.         with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
  57.             futures = {executor.submit(fetch_price, tid, sid): tid for tid, sid in jobs}
  58.             for future in concurrent.futures.as_completed(futures):
  59.                 tid, price = future.result()
  60.                 if price is not None:
  61.                     prices[tid] = price
  62.  
  63.         # Cache material prices for quick cost calculation
  64.         material_price_map = {}
  65.  
  66.         for mat in materials:
  67.             if mat.type_id in prices:
  68.                 mat.sell_price = prices[mat.type_id]
  69.                 material_price_map[mat.type_id] = mat.sell_price
  70.             else:
  71.                 logger.warning(f"No price found for material {mat.name}")
  72.  
  73.         for bp in blueprints:
  74.             if bp.type_id in prices:
  75.                 bp.sell_price = prices[bp.type_id]
  76.             else:
  77.                 logger.warning(f"No price found for blueprint {bp.name}")
  78.  
  79.             total_cost = 0.0
  80.             name_to_type_id = {mat.name: mat.type_id for mat in materials}
  81.  
  82.             for category, materials_dict in bp.materials.items():
  83.                 for mat_name, qty in materials_dict.items():
  84.                     type_id = name_to_type_id.get(mat_name)
  85.                     if type_id is not None:
  86.                         price = material_price_map.get(type_id)
  87.                     else:
  88.                         price = None
  89.                     if price:
  90.                         total_cost += price * qty
  91.                     else:
  92.                         logger.warning(f"No price for material {mat_name} (type_id: {type_id}) in blueprint {bp.name}")
  93.             bp.material_cost = round(total_cost, 2)
  94.  
  95.         db.session.commit()
  96.         return jsonify({"message": "Prices updated successfully"}), 200
  97.  
  98.     except Exception as e:
  99.         db.session.rollback()
  100.         logger.error("Error updating prices", exc_info=True)
  101.         return jsonify({"error": "An error occurred while updating prices"}), 500
  102.  
Advertisement
Add Comment
Please, Sign In to add comment