| 3232 |
varun.gupt |
1 |
'''
|
|
|
2 |
Created on 26-Aug-2011
|
|
|
3 |
|
|
|
4 |
@author: Varun Gupta
|
|
|
5 |
'''
|
| 3350 |
varun.gupt |
6 |
import json, sys, os
|
| 3232 |
varun.gupt |
7 |
|
| 3350 |
varun.gupt |
8 |
cmd_folder = os.path.dirname(os.path.abspath(os.environ["HOME"] + "/code/trunk/PyProj/src/shop2020/"))
|
|
|
9 |
if cmd_folder not in sys.path:
|
|
|
10 |
sys.path.insert(0, cmd_folder)
|
|
|
11 |
|
|
|
12 |
from shop2020.clients.CatalogClient import CatalogClient
|
|
|
13 |
|
| 3453 |
varun.gupt |
14 |
CHARACTER_ENCODING = 'ISO-8859-1'
|
|
|
15 |
|
|
|
16 |
class BrandAndModelExtracter:
|
|
|
17 |
|
|
|
18 |
def __init__(self):
|
|
|
19 |
|
|
|
20 |
try:
|
|
|
21 |
client = CatalogClient().get_client()
|
|
|
22 |
self.brands = client.getAllBrandsByCategory(10001)
|
|
|
23 |
except Exception:
|
|
|
24 |
self.brands = ['Micromax', 'BlackBerry', 'Blackberry', 'Motorola', 'Alcatel', 'Sony Ericsson', 'Apple', \
|
|
|
25 |
'Spice', 'Nokia', 'HTC', 'Samsung', 'LG', 'Dell', 'Karbonn', 'Lava']
|
|
|
26 |
|
|
|
27 |
self.brands.append('Blackberry') #To resolve issue of 'BlackBerry' and 'Blackberry'
|
|
|
28 |
|
|
|
29 |
def extract(self, full_name):
|
| 4039 |
varun.gupt |
30 |
full_name = full_name.strip()
|
| 3453 |
varun.gupt |
31 |
|
|
|
32 |
for brand in self.brands:
|
|
|
33 |
if full_name.startswith(brand): return (brand, full_name.replace(brand, '').strip())
|
|
|
34 |
|
|
|
35 |
return ("", full_name)
|
|
|
36 |
|
| 4198 |
varun.gupt |
37 |
def getURLSource(url):
|
|
|
38 |
try:
|
|
|
39 |
return str(url.split('.')[1].strip())
|
|
|
40 |
except Exception:
|
|
|
41 |
return None
|
|
|
42 |
|
| 3232 |
varun.gupt |
43 |
def isValidRule(rule):
|
|
|
44 |
try:
|
|
|
45 |
if rule is None:
|
|
|
46 |
return False
|
|
|
47 |
|
|
|
48 |
elif rule['url'] is None:
|
|
|
49 |
return False
|
|
|
50 |
|
|
|
51 |
elif rule['source'] is None:
|
|
|
52 |
return False
|
|
|
53 |
|
|
|
54 |
else:
|
|
|
55 |
return True
|
|
|
56 |
|
|
|
57 |
except KeyError:
|
|
|
58 |
return False
|
|
|
59 |
|
|
|
60 |
def getItemsWithTopScore(items):
|
|
|
61 |
filterd_items = []
|
|
|
62 |
top_score = -1.0
|
|
|
63 |
|
|
|
64 |
for item in items:
|
|
|
65 |
if item['score'] >= top_score:
|
|
|
66 |
filterd_items.append(item)
|
|
|
67 |
top_score = item['score']
|
|
|
68 |
else:
|
|
|
69 |
return filterd_items
|
|
|
70 |
|
|
|
71 |
return filterd_items
|
|
|
72 |
|
|
|
73 |
def isPriceSame(items):
|
|
|
74 |
for i in range(0, items.__len__() - 1):
|
|
|
75 |
if items[i]['price'] != items[i + 1]['price']: return False
|
|
|
76 |
|
| 3313 |
varun.gupt |
77 |
return True
|
|
|
78 |
|
|
|
79 |
def getProductClusters(products):
|
|
|
80 |
'''
|
|
|
81 |
Receives a list of products (returned from search results) &
|
|
|
82 |
returns a clustered dictionary, where products are grouped by
|
|
|
83 |
the 'source'
|
|
|
84 |
'''
|
| 4198 |
varun.gupt |
85 |
clustered_results = {'adexmart': [], 'flipkart': [], 'homeshop18': [], 'infibeam': [], 'letsbuy': []}
|
| 3313 |
varun.gupt |
86 |
|
|
|
87 |
for product in products:
|
|
|
88 |
clustered_results[product['source']].append(product)
|
|
|
89 |
|
|
|
90 |
return clustered_results
|
|
|
91 |
|
|
|
92 |
def getFilteredClustersWithTopScores(product_clusters):
|
|
|
93 |
filtered_cluster = {}
|
|
|
94 |
|
|
|
95 |
for source, products in product_clusters.iteritems():
|
|
|
96 |
filtered_cluster[source] = getItemsWithTopScore(products)
|
|
|
97 |
|
|
|
98 |
return filtered_cluster
|
|
|
99 |
|
|
|
100 |
def removePriceFormatting(price_string):
|
| 4198 |
varun.gupt |
101 |
return price_string.replace('Rs.', '').replace('Rs', '').replace(',', '').replace(' ', '').strip().split('.')[0]
|
| 3313 |
varun.gupt |
102 |
|
| 4198 |
varun.gupt |
103 |
def getSearchURL(source, name):
|
|
|
104 |
|
|
|
105 |
search_urls = {
|
|
|
106 |
'flipkart': 'http://www.flipkart.com/search-mobiles?query=$$&from=all&searchGroup=mobiles',
|
|
|
107 |
'homeshop18': 'http://www.homeshop18.com/$$/search:$$/categoryid:3024',
|
|
|
108 |
'adexmart': 'http://adexmart.com/search.php?orderby=position&orderway=desc&search_query=$$',
|
|
|
109 |
'infibeam': 'http://www.infibeam.com/Mobiles/search?q=$$',
|
|
|
110 |
'letsbuy': 'http://www.letsbuy.com/advanced_search_result.php?cPath=254&keywords=$$'
|
|
|
111 |
}
|
|
|
112 |
return search_urls[source].replace('$$', name)
|
|
|
113 |
|
|
|
114 |
def getDisplayInfo(filtered_cluster, product_name):
|
|
|
115 |
display_info = {'adexmart': {}, 'flipkart': {}, 'homeshop18': {}, 'infibeam': {}, 'letsbuy': {}}
|
| 3313 |
varun.gupt |
116 |
|
|
|
117 |
for source, products in filtered_cluster.iteritems():
|
|
|
118 |
|
|
|
119 |
if len(products) > 0:
|
|
|
120 |
if isPriceSame(products):
|
|
|
121 |
display_info[source]['price'] = products[0]['price']
|
|
|
122 |
display_info[source]['data'] = None
|
|
|
123 |
display_info[source]['url'] = products[0]['url']
|
|
|
124 |
display_info[source]['text'] = removePriceFormatting(products[0]['price'])
|
| 3453 |
varun.gupt |
125 |
display_info[source]['title'] = products[0]['name']
|
| 3313 |
varun.gupt |
126 |
else:
|
|
|
127 |
display_info[source]['price'] = None
|
|
|
128 |
display_info[source]['data'] = json.dumps(products)
|
|
|
129 |
display_info[source]['url'] = None
|
|
|
130 |
display_info[source]['text'] = 'Conflict'
|
|
|
131 |
else:
|
|
|
132 |
display_info[source]['price'] = None
|
|
|
133 |
display_info[source]['data'] = None
|
| 4198 |
varun.gupt |
134 |
display_info[source]['url'] = getSearchURL(source, product_name)
|
| 3313 |
varun.gupt |
135 |
display_info[source]['text'] = 'Not Found'
|
|
|
136 |
|
| 3453 |
varun.gupt |
137 |
return display_info
|
|
|
138 |
|
|
|
139 |
def getSynonyms():
|
| 4198 |
varun.gupt |
140 |
file_path = '/tmp/price-comp-dashboard/synonyms.json'
|
| 3453 |
varun.gupt |
141 |
file = open(file_path, 'r')
|
|
|
142 |
synonyms_json = file.read()
|
|
|
143 |
|
|
|
144 |
synonyms = {}
|
|
|
145 |
for key, value in json.loads(synonyms_json).iteritems():
|
|
|
146 |
list_synonyms = []
|
|
|
147 |
|
|
|
148 |
if 'MODEL_NAME' in value:
|
|
|
149 |
list_synonyms.extend(value['MODEL_NAME'])
|
|
|
150 |
|
|
|
151 |
if 'MODEL_NUMBER' in value:
|
|
|
152 |
list_synonyms.extend(value['MODEL_NUMBER'])
|
|
|
153 |
|
|
|
154 |
synonyms[int(key)] = list_synonyms
|
|
|
155 |
|
|
|
156 |
return synonyms
|
|
|
157 |
|
|
|
158 |
if __name__ == '__main__':
|
|
|
159 |
extracter = BrandAndModelExtracter()
|
|
|
160 |
# print extracter.extract('Nokia X5-01 (Pink)')
|
|
|
161 |
print getSynonyms()
|