Subversion Repositories SmartDukaan

Rev

Rev 3232 | Rev 3350 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3232 varun.gupt 1
'''
2
Created on 26-Aug-2011
3
 
4
@author: Varun Gupta
5
'''
3313 varun.gupt 6
import json
3232 varun.gupt 7
 
8
def isValidRule(rule):
9
    try:
10
        if rule is None:
11
            return False
12
 
13
        elif rule['url'] is None:
14
            return False
15
 
16
        elif rule['source'] is None:
17
            return False
18
 
19
        else:
20
            return True
21
 
22
    except KeyError:
23
        return False
24
 
25
def extractBrandAndName(full_name):
3313 varun.gupt 26
    brands = ('Micromax', 'BlackBerry', 'Blackberry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \
3232 varun.gupt 27
              'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell')
28
 
29
    for brand in brands:
30
        if full_name.startswith(brand):  return (brand, full_name.replace(brand, '').strip())
31
 
32
    return ("", full_name)
33
 
34
def getItemsWithTopScore(items):
35
    filterd_items = []
36
    top_score = -1.0
37
 
38
    for item in items:
39
        if item['score'] >= top_score:
40
            filterd_items.append(item)
41
            top_score = item['score'] 
42
        else:
43
            return filterd_items
44
 
45
    return filterd_items
46
 
47
def isPriceSame(items):
48
    for i in range(0, items.__len__() - 1):
49
        if items[i]['price'] != items[i + 1]['price']:    return False
50
 
3313 varun.gupt 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