|
"""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 |
|
|
|
|
|
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_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 |
|
|
|
|
|
search_name = normalize_turkish(product_name.strip()) |
|
search_name = search_name.replace('(2026)', '').replace('(2025)', '').strip() |
|
search_words = search_name.split() |
|
|
|
|
|
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}") |
|
|
|
|
|
search_pattern = ' '.join(product_words).upper() |
|
|
|
|
|
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: |
|
|
|
search_pattern = search_pattern.replace('İ', 'I') |
|
|
|
|
|
|
|
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}'") |
|
|
|
|
|
warehouse_stock_map = {} |
|
|
|
for match in matches: |
|
|
|
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() |
|
|
|
|
|
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: |
|
|
|
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 variant_filter and should_process: |
|
break |
|
|
|
print(f"DEBUG - Warehouse stock: {warehouse_stock_map}") |
|
|
|
|
|
if warehouse_stock_map: |
|
all_warehouse_info = [] |
|
for warehouse_name, total_stock in warehouse_stock_map.items(): |
|
|
|
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 |