Trek Turkey commited on
Commit
794919b
·
1 Parent(s): 83753e3

sync: smart_warehouse_with_price.py iki botta esitlendi (birlesik surum)

Browse files

CLAUDE.md senkron kuralina ragmen dosyalar suruklenmisti. Birlesik surum
her iki taraftan iyi parcalari alir:
- Desktop'tan: '+' model normalizasyonu (Fuel+ / Domane+ vb, 2 yerde)
- WAB'dan: stale cache fallback + '<Products>' gecerlilik kontrolu
- Yeni: fetch 2 deneme / 5-8s timeout (endpoint artik snapshot tabanli,
eski 3x10-20s retry WhatsApp yanit suresi icin fazla yavasti)

Files changed (1) hide show
  1. smart_warehouse_with_price.py +38 -14
smart_warehouse_with_price.py CHANGED
@@ -194,10 +194,20 @@ def get_product_price_and_link(product_name, variant=None):
194
  for tr, en in tr_map.items():
195
  search_name_normalized = search_name_normalized.replace(tr, en)
196
  search_variant_normalized = search_variant_normalized.replace(tr, en)
197
-
198
  # Now lowercase
199
  search_name = search_name_normalized.lower()
200
  search_variant = search_variant_normalized.lower()
 
 
 
 
 
 
 
 
 
 
201
 
202
  best_match = None
203
  best_score = 0
@@ -285,18 +295,23 @@ def get_cached_warehouse_xml():
285
  cache_age = (current_time - cache['warehouse_xml']['time']) / 60 # in minutes
286
  return cache['warehouse_xml']['data']
287
 
288
- # Hizli timeout endpoint sik sik down. Cache hit yoksa bile 3s'den fazla bekleme.
289
- try:
290
- url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
291
- response = requests.get(url, verify=False, timeout=3)
292
- xml_text = response.text
293
- if xml_text and '<Products>' in xml_text:
294
- cache['warehouse_xml']['data'] = xml_text
295
- cache['warehouse_xml']['time'] = current_time
296
- return xml_text
297
- except Exception:
298
- pass
299
- # Stale cache fallback
 
 
 
 
 
300
  if cache['warehouse_xml']['data']:
301
  return cache['warehouse_xml']['data']
302
  return None
@@ -313,7 +328,16 @@ def get_warehouse_stock_smart_with_price(user_message, previous_result=None):
313
  ]
314
 
315
  clean_message = user_message.lower().strip()
316
-
 
 
 
 
 
 
 
 
 
317
  for phrase in live_support_phrases:
318
  if phrase in clean_message:
319
  return None # Ürün araması yapma, GPT'ye bırak
 
194
  for tr, en in tr_map.items():
195
  search_name_normalized = search_name_normalized.replace(tr, en)
196
  search_variant_normalized = search_variant_normalized.replace(tr, en)
197
+
198
  # Now lowercase
199
  search_name = search_name_normalized.lower()
200
  search_variant = search_variant_normalized.lower()
201
+
202
+ # KRITIK: "+" karakterini normalize et (fuel + lx -> fuel+ lx, fuel lx -> fuel+ lx)
203
+ # Kullanici "fuel + lx" veya "fuel lx" yazabilir, XML'de "FUEL+ LX" var
204
+ plus_models = ['fuel', 'domane', 'fx', 'ds', 'verve', 'townie', 'allant']
205
+ for model in plus_models:
206
+ # "fuel + lx" -> "fuel+ lx"
207
+ search_name = search_name.replace(f'{model} + ', f'{model}+ ')
208
+ # "fuel lx" -> "fuel+ lx" (sadece elektrikli model ise)
209
+ if f'{model} lx' in search_name or f'{model} 9' in search_name:
210
+ search_name = search_name.replace(f'{model} ', f'{model}+ ')
211
 
212
  best_match = None
213
  best_score = 0
 
295
  cache_age = (current_time - cache['warehouse_xml']['time']) / 60 # in minutes
296
  return cache['warehouse_xml']['data']
297
 
298
+ # Endpoint artik snapshot tabanli (bh-collector) tipik yanit <1s.
299
+ # 2 deneme (5s/8s): WhatsApp yanit suresini korur, gecici aksakligi tolere eder.
300
+ for attempt in range(2):
301
+ try:
302
+ url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
303
+ timeout_val = 5 + (attempt * 3)
304
+ response = requests.get(url, verify=False, timeout=timeout_val)
305
+ xml_text = response.text
306
+ # Gecerlilik kontrolu: hata sayfasini cache'leme
307
+ if xml_text and '<Products>' in xml_text:
308
+ cache['warehouse_xml']['data'] = xml_text
309
+ cache['warehouse_xml']['time'] = current_time
310
+ return xml_text
311
+ except Exception:
312
+ pass
313
+
314
+ # Stale cache fallback — bayat veri, bos cevaptan iyidir
315
  if cache['warehouse_xml']['data']:
316
  return cache['warehouse_xml']['data']
317
  return None
 
328
  ]
329
 
330
  clean_message = user_message.lower().strip()
331
+
332
+ # KRITIK: "+" karakterini normalize et (fuel + lx -> fuel+ lx, fuel lx -> fuel+ lx)
333
+ plus_models = ['fuel', 'domane', 'fx', 'ds', 'verve', 'townie', 'allant']
334
+ for model in plus_models:
335
+ # "fuel + lx" -> "fuel+ lx"
336
+ clean_message = clean_message.replace(f'{model} + ', f'{model}+ ')
337
+ # "fuel lx" -> "fuel+ lx" (elektrikli model)
338
+ if f'{model} lx' in clean_message:
339
+ clean_message = clean_message.replace(f'{model} lx', f'{model}+ lx')
340
+
341
  for phrase in live_support_phrases:
342
  if phrase in clean_message:
343
  return None # Ürün araması yapma, GPT'ye bırak