def _parse_price(self, price_value) -> Optional[fl

← العودة
Blog Post

عنوان المحادثة: def _parse_price(self, price_value) -> Optional[float]: """تحوي...

التاريخ: 05.01.2026

التصنيف: 🛒 التجارة الإلكترونية والتسويق

إجمالي الرسائل: 4 | ياسر: 0 | M: 4

المحادثة الكاملة - 05.01.2026
M
def _parse_price(self, price_value) -> Optional[float]: """تحويل السعر لرقم""" if price_value is None: return None try: price_str = str(price_value).replace(",", "").replace("٬", "").strip() price_num = float(price_str) return price_num if price_num > 0 else None except (ValueError, TypeError, AttributeError): return None def _extract_budget_from_text(self, text: str) -> Optional[float]: """استخراج الميزانية من النص العربي أو الإنجليزي""" if not text: return None arabic_nums = {'٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9'} for ar, en in arabic_nums.items(): text = text.replace(ar, en) patterns = [ r'(\d+)\s*(?:ألف|الف)\s*(?:ريال)?', r'(\d+),?(\d{3})\s*(?:ريال)?', r'(\d+)000\s*(?:ريال)?', r'(\d+)k', ] for pattern in patterns: match = re.search(pattern, text, re.IGNORECASE) if match: groups = match.groups() if len(groups) == 2 and groups[1]: return float(groups[0] + groups[1]) else: num = float(groups[0]) if num return num * 1000 return num match = re.search(r'(\d+)', text) if match: num = float(match.group(1)) if num return num * 1000 return num return None async def get_available_cars_with_images(self, budget_min: float = None, budget_max: float = None) -> List[Dict]: """جلب السيارات المتاحة مع الصور - مفلترة بالسعر الصحيح""" try: result = await aldaman_client.get_cars( price_from=budget_min, price_to=budget_max, per_page=50 ) all_cars = result.get("data", []) MIN_VALID_PRICE = 1000 valid_cars = [] seen_names = set() for car in all_cars: status = car.get("status", "").lower() if status not in ["available", "متاح", "متاحة"]: continue price = self._parse_price(car.get("discount_price")) or \ self._parse_price(car.get("original_price")) or \ self._parse_price(car.get("price")) if not price or price continue if budget_max and price > budget_max: continue if budget_min and price continue car_name = car.get("name", "") if car_name in seen_names: continue seen_names.add(car_name) car["_calculated_price"] = price valid_cars.append(car) valid_cars.sort(key=lambda x: x.get("_calculated_price", 0)) if budget_max and len(valid_cars) > 6: selected_cars = [] total = len(valid_cars) indices = [ 0, total // 4, total // 2, (total * 3) // 4, total - 2 if total > 2 else total - 1, total - 1, ] seen_indices = set() for idx in indices: if idx not in seen_indices and 0 selected_cars.append(valid_cars[idx]) seen_indices.add(idx) for i in range(total - 1, -1, -1): if len(selected_cars) >= 6:
05.01.2026 17:02
M
def _parse_price(self, price_value) -> Optional[float]: """تحويل السعر لرقم""" if price_value is None: return None try: price_str = str(price_value).replace(",", "").replace("٬", "").strip() price_num = float(price_str) return price_num if price_num > 0 else None except (ValueError, TypeError, AttributeError): return None def _extract_budget_from_text(self, text: str) -> Optional[float]: """استخراج الميزانية من النص العربي أو الإنجليزي""" if not text: return None arabic_nums = {'٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9'} for ar, en in arabic_nums.items(): text = text.replace(ar, en) patterns = [ r'(\d+)\s*(?:ألف|الف)\s*(?:ريال)?', r'(\d+),?(\d{3})\s*(?:ريال)?', r'(\d+)000\s*(?:ريال)?', r'(\d+)k', ] for pattern in patterns: match = re.search(pattern, text, re.IGNORECASE) if match: groups = match.groups() if len(groups) == 2 and groups[1]: return float(groups[0] + groups[1]) else: num = float(groups[0]) if num return num * 1000 return num match = re.search(r'(\d+)', text) if match: num = float(match.group(1)) if num return num * 1000 return num return None async def get_available_cars_with_images(self, budget_min: float = None, budget_max: float = None) -> List[Dict]: """جلب السيارات المتاحة مع الصور - مفلترة بالسعر الصحيح""" try: result = await aldaman_client.get_cars( price_from=budget_min, price_to=budget_max, per_page=50 ) all_cars = result.get("data", []) MIN_VALID_PRICE = 1000 valid_cars = [] seen_names = set() for car in all_cars: status = car.get("status", "").lower() if status not in ["available", "متاح", "متاحة"]: continue price = self._parse_price(car.get("discount_price")) or \ self._parse_price(car.get("original_price")) or \ self._parse_price(car.get("price")) if not price or price continue if budget_max and price > budget_max: continue if budget_min and price continue car_name = car.get("name", "") if car_name in seen_names: continue seen_names.add(car_name) car["_calculated_price"] = price valid_cars.append(car) valid_cars.sort(key=lambda x: x.get("_calculated_price", 0)) if budget_max and len(valid_cars) > 6: selected_cars = [] total = len(valid_cars) indices = [ 0, total // 4, total // 2, (total * 3) // 4, total - 2 if total > 2 else total - 1, total - 1, ] seen_indices = set() for idx in indices: if idx not in seen_indices and 0 selected_cars.append(valid_cars[idx]) seen_indices.add(idx) for i in range(total - 1, -1, -1): if len(selected_cars) >= 6:
05.01.2026 17:02
M
def _parse_price(self, price_value) -> Optional[float]: """تحويل السعر لرقم""" if price_value is None: return None try: price_str = str(price_value).replace(",", "").replace("٬", "").strip() price_num = float(price_str) return price_num if price_num > 0 else None except (ValueError, TypeError, AttributeError): return None def _extract_budget_from_text(self, text: str) -> Optional[float]: """استخراج الميزانية من النص العربي أو الإنجليزي""" if not text: return None arabic_nums = {'٠': '0', '١': '1', '٢': '2', '٣': '3', '٤': '4', '٥': '5', '٦': '6', '٧': '7', '٨': '8', '٩': '9'} for ar, en in arabic_nums.items(): text = text.replace(ar, en) patterns = [ r'(\d+)\s*(?:ألف|الف)\s*(?:ريال)?', r'(\d+),?(\d{3})\s*(?:ريال)?', r'(\d+)000\s*(?:ريال)?', r'(\d+)k', ] for pattern in patterns: match = re.search(pattern, text, re.IGNORECASE) if match: groups = match.groups() if len(groups) == 2 and groups[1]: return float(groups[0] + groups[1]) else: num = float(groups[0]) if num return num * 1000 return num match = re.search(r'(\d+)', text) if match: num = float(match.group(1)) if num return num * 1000 return num return None async def get_available_cars_with_images(self, budget_min: float = None, budget_max: float = None) -> List[Dict]: """جلب السيارات المتاحة مع الصور - مفلترة بالسعر الصحيح""" try: result = await aldaman_client.get_cars( price_from=budget_min, price_to=budget_max, per_page=50 ) all_cars = result.get("data", []) MIN_VALID_PRICE = 1000 valid_cars = [] seen_names = set() for car in all_cars: status = car.get("status", "").lower() if status not in ["available", "متاح", "متاحة"]: continue price = self._parse_price(car.get("discount_price")) or \ self._parse_price(car.get("original_price")) or \ self._parse_price(car.get("price")) if not price or price continue if budget_max and price > budget_max: continue if budget_min and price continue car_name = car.get("name", "") if car_name in seen_names: continue seen_names.add(car_name) car["_calculated_price"] = price valid_cars.append(car) valid_cars.sort(key=lambda x: x.get("_calculated_price", 0)) if budget_max and len(valid_cars) > 6: selected_cars = [] total = len(valid_cars) indices = [ 0, total // 4, total // 2, (total * 3) // 4, total - 2 if total > 2 else total - 1, total - 1, ] seen_indices = set() for idx in indices: if idx not in seen_indices and 0 selected_cars.append(valid_cars[idx]) seen_indices.add(idx) for i in range(total - 1, -1, -1): if len(selected_cars) >= 6:
05.01.2026 17:02
M
async def get_car_image(self, car_name: str) -> Optional[Dict]: """جلب صورة سيارة بالاسم""" car = self.find_car_by_name(car_name) if car and car.get("image_url"): return { "url": car.get("image_url"), "title": car.get("name"), "price": car.get("_calculated_price") or car.get("discount_price") or car.get("original_price"), "car_id": car.get("id"), } return None async def get_welcome_message(self) -> Dict[str, Any]: """رسالة الترحيب""" return { "message": "أهلاً وسهلاً بك في أعمال الدمعان للسيارات! 🚗✨\n\nيسعدني مساعدتك في اختيار سيارة أحلامك! 😊\n\nبدايةً، هل الحجز لك شخصياً أم لشركة؟", "options": [ {"id": "individual", "label": "👤 لي شخصياً (أفراد)", "value": "أفراد"}, {"id": "company", "label": "🏢 لشركة أو مؤسسة", "value": "شركات"} ], "next_step": "select_customer_type", "images": [] }# Singletonai_chat_service = AIChatService()'@Set-Content -Path "c:\Users\miraz\OneDrive\Desktop\cars\aldamancars\backend\app\services\ai_chat.py" -Value $content -Encoding utf8Read [](file:///c%3A/Users/miraz/OneDrive/Desktop/cars/aldamancars/backend/app/services/ai_chat.py#1-1), lines 1 to 150Using "Apply Patch"Made changes.
05.01.2026 17:02
← العودة إلى الرئيسية