"""Ultra simple and fast warehouse stock finder""" def get_warehouse_stock(product_name): """Find warehouse stock FAST - no XML parsing, just regex""" try: import re import requests # Get XML url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php' response = requests.get(url, verify=False, timeout=7) xml_text = response.text # Turkish normalize def normalize(text): tr_map = {'ı': 'i', 'ğ': 'g', 'ü': 'u', 'ş': 's', 'ö': 'o', 'ç': 'c', 'İ': 'i', 'I': 'i'} text = text.lower() for tr, en in tr_map.items(): text = text.replace(tr, en) return text # Parse query query = normalize(product_name.strip()).replace('(2026)', '').replace('(2025)', '').strip() words = query.split() # Find size sizes = ['s', 'm', 'l', 'xl', 'xs', 'xxl', 'ml'] size = next((w for w in words if w in sizes), None) product_words = [w for w in words if w not in sizes and w not in ['beden', 'size', 'boy']] # Build search pattern if 'madone' in product_words and 'sl' in product_words and '6' in product_words: pattern = 'MADONE SL 6 GEN 8' else: pattern = ' '.join(product_words).upper() print(f"DEBUG - Searching: {pattern}, Size: {size}") # Search for product + variant combo if size: # Direct search for product with specific size size_pattern = f'{size.upper()}-' # Find all occurrences of the product name import re product_regex = f'.*?.*?.*?' match = re.search(product_regex, xml_text, re.DOTALL) if match: product_block = match.group(0) print(f"DEBUG - Found product with {size_pattern} variant") # Extract warehouses warehouse_info = [] warehouse_regex = r'.*?.*?(.*?).*?' warehouses = re.findall(warehouse_regex, product_block, re.DOTALL) for wh_name, wh_stock in warehouses: try: stock = int(wh_stock.strip()) if stock > 0: # Format name if "CADDEBOSTAN" in wh_name: display = "Caddebostan mağazası" elif "ORTAKÖY" in wh_name: display = "Ortaköy mağazası" elif "ALSANCAK" in wh_name: display = "İzmir Alsancak mağazası" elif "BAHCEKOY" in wh_name or "BAHÇEKÖY" in wh_name: display = "Bahçeköy mağazası" else: display = wh_name warehouse_info.append(f"{display}: Mevcut") except: pass return warehouse_info if warehouse_info else ["Hiçbir mağazada mevcut değil"] else: print(f"DEBUG - No {size_pattern} variant found for {pattern}") return ["Hiçbir mağazada mevcut değil"] else: # No size filter - get all stock return ["Beden bilgisi belirtilmedi"] except Exception as e: print(f"Error: {e}") return None