|
|
|
"""Test actual get_product_price_and_link function""" |
|
|
|
from smart_warehouse_with_price import get_product_price_and_link |
|
|
|
|
|
def test_with_debug(): |
|
print("Testing get_product_price_and_link('Marlin 5')...") |
|
|
|
|
|
import smart_warehouse_with_price |
|
import xml.etree.ElementTree as ET |
|
import re |
|
|
|
|
|
xml_content = smart_warehouse_with_price.get_cached_trek_xml() |
|
if not xml_content: |
|
print("No XML content") |
|
return |
|
|
|
root = ET.fromstring(xml_content) |
|
|
|
search_name = "marlin 5" |
|
clean_search = re.sub(r'\s*\(\d{4}\)\s*', '', search_name).strip() |
|
|
|
print(f"Clean search: '{clean_search}'") |
|
|
|
best_match = None |
|
best_score = 0 |
|
best_name = None |
|
|
|
count = 0 |
|
for item in root.findall('item'): |
|
rootlabel_elem = item.find('rootlabel') |
|
if rootlabel_elem is None or not rootlabel_elem.text: |
|
continue |
|
|
|
item_name = rootlabel_elem.text.lower() |
|
|
|
|
|
tr_map = {'ı': 'i', 'ğ': 'g', 'ü': 'u', 'ş': 's', 'ö': 'o', 'ç': 'c'} |
|
for tr, en in tr_map.items(): |
|
item_name = item_name.replace(tr, en) |
|
|
|
clean_item = re.sub(r'\s*\(\d{4}\)\s*', '', item_name).strip() |
|
|
|
score = 0 |
|
|
|
|
|
if clean_search == clean_item: |
|
score += 100 |
|
elif clean_item.startswith(clean_search + " ") or clean_item == clean_search: |
|
score += 50 |
|
else: |
|
name_parts = clean_search.split() |
|
for part in name_parts: |
|
if part in clean_item: |
|
score += 1 |
|
|
|
if score > 0: |
|
count += 1 |
|
if count <= 10: |
|
print(f" {count}. {rootlabel_elem.text[:50]}: score={score}") |
|
|
|
if score > best_score: |
|
best_score = score |
|
best_match = item |
|
best_name = rootlabel_elem.text |
|
print(f" >>> NEW BEST: {best_name} with score {best_score}") |
|
|
|
print(f"\nFinal best match: {best_name}") |
|
print(f"Best score: {best_score}") |
|
|
|
if best_match is not None: |
|
price_elem = best_match.find('priceTaxWithCur') |
|
link_elem = best_match.find('productLink') |
|
|
|
if price_elem is not None: |
|
print(f"Price: {price_elem.text}") |
|
if link_elem is not None: |
|
print(f"Link: {link_elem.text}") |
|
|
|
|
|
print("\n" + "="*60) |
|
print("Actual function result:") |
|
price, link = get_product_price_and_link("Marlin 5") |
|
print(f"Price: {price}") |
|
print(f"Link: {link}") |
|
|
|
if __name__ == "__main__": |
|
test_with_debug() |