Subversion Repositories SmartDukaan

Rev

Rev 3232 | Rev 3350 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3232 Rev 3313
Line 1... Line 1...
1
'''
1
'''
2
Created on 26-Aug-2011
2
Created on 26-Aug-2011
3
 
3
 
4
@author: Varun Gupta
4
@author: Varun Gupta
5
'''
5
'''
-
 
6
import json
6
 
7
 
7
def isValidRule(rule):
8
def isValidRule(rule):
8
    try:
9
    try:
9
        if rule is None:
10
        if rule is None:
10
            return False
11
            return False
Line 20... Line 21...
20
    
21
    
21
    except KeyError:
22
    except KeyError:
22
        return False
23
        return False
23
    
24
    
24
def extractBrandAndName(full_name):
25
def extractBrandAndName(full_name):
25
    brands = ('Micromax', 'BlackBerry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \
26
    brands = ('Micromax', 'BlackBerry', 'Blackberry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \
26
              'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell')
27
              'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell')
27
    
28
    
28
    for brand in brands:
29
    for brand in brands:
29
        if full_name.startswith(brand):  return (brand, full_name.replace(brand, '').strip())
30
        if full_name.startswith(brand):  return (brand, full_name.replace(brand, '').strip())
30
    
31
    
Line 45... Line 46...
45
 
46
 
46
def isPriceSame(items):
47
def isPriceSame(items):
47
    for i in range(0, items.__len__() - 1):
48
    for i in range(0, items.__len__() - 1):
48
        if items[i]['price'] != items[i + 1]['price']:    return False
49
        if items[i]['price'] != items[i + 1]['price']:    return False
49
    
50
    
50
    return True 
-
 
51
51
    return True
-
 
52
 
-
 
53
def getProductClusters(products):
-
 
54
    '''
-
 
55
    Receives a list of products (returned from search results) &
-
 
56
    returns a clustered dictionary, where products are grouped by
-
 
57
    the 'source'
-
 
58
    '''
-
 
59
    clustered_results = {'flipkart': [], 'homeshop18': [], 'infibeam': [], 'letsbuy': []}
-
 
60
    
-
 
61
    for product in products:
-
 
62
        clustered_results[product['source']].append(product)
-
 
63
    
-
 
64
    return clustered_results
-
 
65
 
-
 
66
def getFilteredClustersWithTopScores(product_clusters):
-
 
67
    filtered_cluster = {}
-
 
68
    
-
 
69
    for source, products in product_clusters.iteritems():
-
 
70
        filtered_cluster[source] = getItemsWithTopScore(products)
-
 
71
    
-
 
72
    return filtered_cluster
-
 
73
 
-
 
74
def removePriceFormatting(price_string):
-
 
75
    return price_string.replace('Rs.', '').replace(',', '').strip()
-
 
76
    
-
 
77
def getDisplayInfo(filtered_cluster):
-
 
78
    display_info = {'flipkart': {}, 'homeshop18': {}, 'infibeam': {}, 'letsbuy': {}}
-
 
79
    
-
 
80
    for source, products in filtered_cluster.iteritems():
-
 
81
        
-
 
82
        if len(products) > 0:
-
 
83
            if isPriceSame(products):
-
 
84
                display_info[source]['price'] = products[0]['price']
-
 
85
                display_info[source]['data'] = None
-
 
86
                display_info[source]['url'] = products[0]['url']
-
 
87
                display_info[source]['text'] = removePriceFormatting(products[0]['price'])
-
 
88
            else:
-
 
89
                display_info[source]['price'] = None
-
 
90
                display_info[source]['data'] = json.dumps(products)
-
 
91
                display_info[source]['url'] = None
-
 
92
                display_info[source]['text'] = 'Conflict'
-
 
93
        else:
-
 
94
            display_info[source]['price'] = None
-
 
95
            display_info[source]['data'] = None
-
 
96
            display_info[source]['url'] = None
-
 
97
            display_info[source]['text'] = 'Not Found'
-
 
98
    
-
 
99
    return display_info
-
 
100
52
101