"""Ultra fast warehouse stock getter using regex"""
def get_warehouse_stock(product_name):
"""Super fast warehouse stock finder using regex instead of XML parsing"""
try:
import re
import requests
# Fetch XML
warehouse_url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
response = requests.get(warehouse_url, verify=False, timeout=7)
if response.status_code != 200:
return None
xml_text = response.text
# Turkish normalization
turkish_map = {'ı': 'i', 'ğ': 'g', 'ü': 'u', 'ş': 's', 'ö': 'o', 'ç': 'c', 'İ': 'i', 'I': 'i'}
def normalize_turkish(text):
text = text.lower()
for tr_char, en_char in turkish_map.items():
text = text.replace(tr_char, en_char)
return text
# Normalize search
search_name = normalize_turkish(product_name.strip())
search_name = search_name.replace('(2026)', '').replace('(2025)', '').strip()
search_words = search_name.split()
# Separate size from product words
size_words = ['s', 'm', 'l', 'xl', 'xs', 'xxl', 'ml']
size_indicators = ['beden', 'size', 'boy']
variant_filter = [w for w in search_words if w in size_words]
product_words = [w for w in search_words if w not in size_words and w not in size_indicators]
print(f"DEBUG - Looking for: {' '.join(product_words)}")
print(f"DEBUG - Size filter: {variant_filter}")
# Build exact pattern for common products
search_pattern = ' '.join(product_words).upper()
# Handle specific products
if 'madone' in product_words and 'sl' in product_words and '6' in product_words and 'gen' in product_words and '8' in product_words:
search_pattern = 'MADONE SL 6 GEN 8'
elif 'madone' in product_words and 'slr' in product_words and '7' in product_words:
search_pattern = 'MADONE SLR 7 GEN 8'
else:
# Generic pattern
search_pattern = search_pattern.replace('İ', 'I')
# Find all Product blocks with this name
# Using lazy quantifier .*? for efficiency
product_pattern = f'.*?.*?'
matches = re.findall(product_pattern, xml_text, re.DOTALL)
print(f"DEBUG - Found {len(matches)} products matching '{search_pattern}'")
# Process matches
warehouse_stock_map = {}
for match in matches:
# Extract variant if we need to filter by size
should_process = True
if variant_filter:
variant_match = re.search(r'', match)
if variant_match:
variant = variant_match.group(1)
size_wanted = variant_filter[0].upper()
# Check if variant starts with desired size
if variant.startswith(f'{size_wanted}-'):
print(f"DEBUG - Found matching variant: {variant}")
should_process = True
else:
should_process = False
else:
should_process = False
if should_process:
# Extract all warehouses with stock
warehouse_pattern = r'.*?.*?(.*?).*?'
warehouses = re.findall(warehouse_pattern, match, re.DOTALL)
for wh_name, wh_stock in warehouses:
try:
stock = int(wh_stock.strip())
if stock > 0:
if wh_name in warehouse_stock_map:
warehouse_stock_map[wh_name] += stock
else:
warehouse_stock_map[wh_name] = stock
except:
pass
# If we found a variant match, stop looking
if variant_filter and should_process:
break
print(f"DEBUG - Warehouse stock: {warehouse_stock_map}")
# Format results
if warehouse_stock_map:
all_warehouse_info = []
for warehouse_name, total_stock in warehouse_stock_map.items():
# Make store names more readable
if "Caddebostan" in warehouse_name or "CADDEBOSTAN" in warehouse_name:
display_name = "Caddebostan mağazası"
elif "Ortaköy" in warehouse_name or "ORTAKÖY" in warehouse_name:
display_name = "Ortaköy mağazası"
elif "Sarıyer" in warehouse_name:
display_name = "Sarıyer mağazası"
elif "Alsancak" in warehouse_name or "ALSANCAK" in warehouse_name or "İzmir" in warehouse_name:
display_name = "İzmir Alsancak mağazası"
elif "BAHCEKOY" in warehouse_name or "Bahçeköy" in warehouse_name:
display_name = "Bahçeköy mağazası"
else:
display_name = warehouse_name
all_warehouse_info.append(f"{display_name}: Mevcut")
return all_warehouse_info
else:
return ["Hiçbir mağazada mevcut değil"]
except Exception as e:
print(f"Warehouse stock error: {e}")
return None