File size: 5,826 Bytes
c8f893c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
"""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'<Product>.*?<ProductName><!\\[CDATA\\[{re.escape(search_pattern)}\\]\\]></ProductName>.*?</Product>'
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'<ProductVariant><!\\[CDATA\\[(.*?)\\]\\]></ProductVariant>', 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'<Warehouse>.*?<Name><!\\[CDATA\\[(.*?)\\]\\]></Name>.*?<Stock>(.*?)</Stock>.*?</Warehouse>'
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 |