File size: 6,056 Bytes
56aafb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
"""

Ürün Resimlerini Sohbette Gösterme Sistemi

"""

def round_price(price_str):
    """Fiyatı yuvarlama formülüne göre yuvarla"""
    try:
        # TL ve diğer karakterleri temizle
        price_clean = price_str.replace(' TL', '').replace(',', '.')
        price_float = float(price_clean)
        
        # Fiyat 200000 üzerindeyse en yakın 5000'lik basamağa yuvarla
        if price_float > 200000:
            return str(round(price_float / 5000) * 5000)
        # Fiyat 30000 üzerindeyse en yakın 1000'lik basamağa yuvarla
        elif price_float > 30000:
            return str(round(price_float / 1000) * 1000)
        # Fiyat 10000 üzerindeyse en yakın 100'lük basamağa yuvarla
        elif price_float > 10000:
            return str(round(price_float / 100) * 100)
        # Diğer durumlarda en yakın 10'luk basamağa yuvarla
        else:
            return str(round(price_float / 10) * 10)
    except (ValueError, TypeError):
        return price_str

def format_message_with_images(message):
    """Mesajdaki resim URL'lerini HTML formatına çevir"""
    if "Ürün resmi:" not in message:
        return message
    
    lines = message.split('\n')
    formatted_lines = []
    
    for line in lines:
        if line.startswith("Ürün resmi:"):
            image_url = line.replace("Ürün resmi:", "").strip()
            if image_url:
                # HTML img tag'i oluştur
                img_html = f"""

<div style="margin: 10px 0;">

    <img src="{image_url}" 

         alt="Ürün Resmi" 

         style="max-width: 300px; max-height: 200px; border-radius: 8px; border: 1px solid #ddd; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"

         onerror="this.style.display='none'">

</div>"""
                formatted_lines.append(img_html)
            else:
                formatted_lines.append(line)
        else:
            formatted_lines.append(line)
    
    return '\n'.join(formatted_lines)

def create_product_gallery(products_with_images):
    """Birden fazla ürün için galeri oluştur"""
    if not products_with_images:
        return ""
    
    gallery_html = """

<div style="display: flex; flex-wrap: wrap; gap: 15px; margin: 15px 0;">

"""
    
    for product in products_with_images:
        name = product.get('name', 'Bilinmeyen')
        price = product.get('price', 'Fiyat yok')
        image_url = product.get('image_url', '')
        product_url = product.get('product_url', '')
        
        if image_url:
            card_html = f"""

<div style="border: 1px solid #ddd; border-radius: 8px; padding: 10px; max-width: 200px; text-align: center; background: white; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">

    <img src="{image_url}" 

         alt="{name}" 

         style="max-width: 180px; max-height: 120px; border-radius: 4px; margin-bottom: 8px;"

         onerror="this.style.display='none'">

    <div style="font-size: 12px; font-weight: bold; margin-bottom: 4px;">{name}</div>

    <div style="font-size: 11px; color: #666; margin-bottom: 8px;">{price}</div>

    {f'<a href="{product_url}" target="_blank" style="font-size: 10px; color: #007bff; text-decoration: none;">Detaylı Bilgi</a>' if product_url else ''}

</div>"""
            gallery_html += card_html
    
    gallery_html += "</div>"
    return gallery_html

def extract_product_info_for_gallery(message):
    """Mesajdan ürün bilgilerini çıkarıp galeri formatına çevir"""
    if "karşılaştır" in message.lower() or "öneri" in message.lower():
        # Bu durumda galeri formatı kullan
        lines = message.split('\n')
        products = []
        
        current_product = {}
        for line in lines:
            line = line.strip()
            if line.startswith('•') and any(keyword in line.lower() for keyword in ['marlin', 'émonda', 'madone', 'domane', 'fuel', 'powerfly', 'fx']):
                # Yeni ürün başladı
                if current_product:
                    products.append(current_product)
                
                # Ürün adı ve fiyatı parse et
                parts = line.split(' - ')
                name = parts[0].replace('•', '').strip()
                price_raw = parts[1] if len(parts) > 1 else 'Fiyat yok'
                
                # Fiyatı yuvarlama formülüne göre yuvarla
                if price_raw != 'Fiyat yok':
                    price = round_price(price_raw) + ' TL'
                else:
                    price = price_raw
                
                current_product = {
                    'name': name,
                    'price': price,
                    'image_url': '',
                    'product_url': ''
                }
            elif "Ürün resmi:" in line and current_product:
                current_product['image_url'] = line.replace("Ürün resmi:", "").strip()
            elif "Ürün linki:" in line and current_product:
                current_product['product_url'] = line.replace("Ürün linki:", "").strip()
        
        # Son ürünü ekle
        if current_product:
            products.append(current_product)
        
        if products:
            gallery = create_product_gallery(products)
            # Orijinal mesajdaki resim linklerini temizle
            cleaned_message = message
            for line in message.split('\n'):
                if line.startswith("Ürün resmi:") or line.startswith("Ürün linki:"):
                    cleaned_message = cleaned_message.replace(line, "")
            
            return cleaned_message.strip() + "\n\n" + gallery
    
    return format_message_with_images(message)

def should_use_gallery_format(message):
    """Mesajın galeri formatı kullanması gerekip gerekmediğini kontrol et"""
    gallery_keywords = ["karşılaştır", "öneri", "seçenek", "alternatif", "bütçe"]
    return any(keyword in message.lower() for keyword in gallery_keywords)