File size: 4,469 Bytes
15c7d5c
 
 
 
 
 
dc7e417
 
15c7d5c
8b25124
15c7d5c
dc7e417
15c7d5c
 
 
 
8b25124
15c7d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc7e417
15c7d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc7e417
 
 
15c7d5c
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
from typing import Dict, List
import requests
from bs4 import BeautifulSoup
import aiohttp
import asyncio
import json
from sentence_transformers import SentenceTransformer
import numpy as np
import re

class DynamicRecommender:
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
        }
        self.model = SentenceTransformer('all-mpnet-base-v2')
        
    async def search_amazon(self, query: str) -> List[Dict]:
        """Search Amazon for products"""
        search_url = f"https://www.amazon.in/s?k={query}"
        async with aiohttp.ClientSession() as session:
            async with session.get(search_url, headers=self.headers) as response:
                if response.status == 200:
                    html = await response.text()
                    return self._parse_amazon_results(html)
        return []
    
    async def search_flipkart(self, query: str) -> List[Dict]:
        """Search Flipkart for products"""
        search_url = f"https://www.flipkart.com/search?q={query}"
        async with aiohttp.ClientSession() as session:
            async with session.get(search_url, headers=self.headers) as response:
                if response.status == 200:
                    html = await response.text()
                    return self._parse_flipkart_results(html)
        return []

    def _parse_amazon_results(self, html: str) -> List[Dict]:
        soup = BeautifulSoup(html, 'html.parser')
        products = []
        for item in soup.select('.s-result-item'):
            try:
                name = item.select_one('.a-text-normal')
                price = item.select_one('.a-price-whole')
                if name and price:
                    products.append({
                        'name': name.text.strip(),
                        'price': price.text.strip(),
                        'source': 'Amazon',
                        'url': 'https://amazon.in' + item.select_one('a')['href']
                    })
            except Exception:
                continue
        return products[:5]

    def _parse_flipkart_results(self, html: str) -> List[Dict]:
        soup = BeautifulSoup(html, 'html.parser')
        products = []
        for item in soup.select('._1AtVbE'):
            try:
                name = item.select_one('._4rR01T')
                price = item.select_one('._30jeq3')
                if name and price:
                    products.append({
                        'name': name.text.strip(),
                        'price': price.text.strip(),
                        'source': 'Flipkart',
                        'url': 'https://flipkart.com' + item.select_one('a')['href']
                    })
            except Exception:
                continue
        return products[:5]

    def _extract_keywords(self, text: str) -> List[str]:
        """Extract relevant search keywords from input"""
        age_match = re.search(r'age\s+(\d+)', text.lower())
        age = age_match.group(1) if age_match else None

        interests = []
        if 'software' in text.lower() or 'engineer' in text.lower():
            interests.extend(['programming books', 'tech gadgets'])
        if 'books' in text.lower():
            interests.append('books')
        if 'successful' in text.lower():
            interests.extend(['self help books', 'business books'])

        return [f"{interest} for {age} year old" if age else interest for interest in interests]

    async def get_recommendations(self, text: str) -> Dict:
        """Get personalized recommendations"""
        try:
            keywords = self._extract_keywords(text)
            all_products = []
            
            for keyword in keywords:
                amazon_products = await self.search_amazon(keyword)
                flipkart_products = await self.search_flipkart(keyword)
                all_products.extend(amazon_products + flipkart_products)
            
            # Remove duplicates and sort by relevance
            seen = set()
            unique_products = []
            for product in all_products:
                if product['name'] not in seen:
                    seen.add(product['name'])
                    unique_products.append(product)
            
            return unique_products[:5]
            
        except Exception as e:
            print(f"Error in recommendations: {str(e)}")
            return []