| 22307 |
amit.gupta |
1 |
from elixir import *
|
|
|
2 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
3 |
from shop2020.model.v1.catalog.impl.DataService import Item, Tag_Listing, \
|
|
|
4 |
Tag_Ranking, PrivateDeals
|
|
|
5 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
|
|
6 |
from sqlalchemy.sql.expression import or_, desc
|
|
|
7 |
from sqlalchemy.sql.functions import now
|
|
|
8 |
import json
|
|
|
9 |
import optparse
|
|
|
10 |
import os
|
|
|
11 |
import pymongo
|
|
|
12 |
import pysolr
|
|
|
13 |
import shutil
|
|
|
14 |
import traceback
|
| 27375 |
amit.gupta |
15 |
import urllib2
|
| 24028 |
amit.gupta |
16 |
from dtr.utils.utils import to_java_date
|
| 27375 |
amit.gupta |
17 |
import urllib2
|
|
|
18 |
import StringIO
|
|
|
19 |
# import pymongo
|
|
|
20 |
# import pysolr
|
| 22307 |
amit.gupta |
21 |
|
| 26753 |
amit.gupta |
22 |
CATEGORY_MASTER = [10006, 10010, 14202, 14203]
|
| 22307 |
amit.gupta |
23 |
parser = optparse.OptionParser()
|
| 22308 |
amit.gupta |
24 |
parser.add_option("-d", "--d", dest="dbHost",
|
| 31595 |
amit.gupta |
25 |
default="127.0.0.1",
|
|
|
26 |
type="string", help="The HOST where the mysql server is running",
|
|
|
27 |
metavar="DBHOST")
|
| 22307 |
amit.gupta |
28 |
parser.add_option("-s", "--s", dest="solrPath",
|
| 31595 |
amit.gupta |
29 |
default="http://localhost:8984/solr/demo",
|
|
|
30 |
type="string", help="Complete solr path",
|
|
|
31 |
metavar="SOLRHOST")
|
| 22308 |
amit.gupta |
32 |
parser.add_option("-m", "--m", dest="mongoHost",
|
| 31595 |
amit.gupta |
33 |
default="localhost",
|
|
|
34 |
type="string", help="Complete solr path",
|
|
|
35 |
metavar="HOST")
|
| 22307 |
amit.gupta |
36 |
|
|
|
37 |
(options, args) = parser.parse_args()
|
|
|
38 |
|
| 27375 |
amit.gupta |
39 |
con = None
|
| 22307 |
amit.gupta |
40 |
|
| 22310 |
amit.gupta |
41 |
DataService.initialize(db_hostname=options.dbHost)
|
| 26576 |
amit.gupta |
42 |
solr = pysolr.Solr(options.solrPath, always_commit=True, timeout=10)
|
| 22307 |
amit.gupta |
43 |
|
| 27375 |
amit.gupta |
44 |
|
| 22308 |
amit.gupta |
45 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
46 |
global con
|
|
|
47 |
if con is None:
|
| 27375 |
amit.gupta |
48 |
print "Establishing connection %s host and port %d" % (host, port)
|
| 22308 |
amit.gupta |
49 |
try:
|
|
|
50 |
con = pymongo.MongoClient(host, port)
|
|
|
51 |
except Exception, e:
|
|
|
52 |
print e
|
|
|
53 |
return None
|
|
|
54 |
return con
|
| 22307 |
amit.gupta |
55 |
|
| 27375 |
amit.gupta |
56 |
|
| 22307 |
amit.gupta |
57 |
class __SkuInfo:
|
| 31595 |
amit.gupta |
58 |
|
| 27375 |
amit.gupta |
59 |
def __init__(self, id, ids, brand, model_name, category_id, subCategoryId, thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
|
| 22307 |
amit.gupta |
60 |
category, subCategory):
|
| 27375 |
amit.gupta |
61 |
# Skubundle id
|
| 22307 |
amit.gupta |
62 |
self.id = id
|
| 27375 |
amit.gupta |
63 |
# _id of skus
|
| 22307 |
amit.gupta |
64 |
self.ids = ids
|
|
|
65 |
self.brand = brand
|
|
|
66 |
self.model_name = model_name
|
|
|
67 |
self.category_id = category_id
|
|
|
68 |
self.subCategoryId = subCategoryId
|
| 31595 |
amit.gupta |
69 |
self.thumbnail = thumbnail
|
| 27375 |
amit.gupta |
70 |
self.title = title
|
| 22307 |
amit.gupta |
71 |
self.brand_synonyms = brand_synonyms
|
|
|
72 |
self.model_name_synonyms = model_name_synonyms
|
|
|
73 |
self.source_product_names = source_product_names
|
|
|
74 |
self.category = category
|
|
|
75 |
self.subCategory = subCategory
|
| 31595 |
amit.gupta |
76 |
|
| 22307 |
amit.gupta |
77 |
def toJSON(self):
|
| 27375 |
amit.gupta |
78 |
return json.dumps(self, default=lambda o: o.__dict__,
|
| 31595 |
amit.gupta |
79 |
sort_keys=True, indent=4)
|
| 27375 |
amit.gupta |
80 |
|
| 31595 |
amit.gupta |
81 |
|
| 22307 |
amit.gupta |
82 |
class __Catalog:
|
| 31595 |
amit.gupta |
83 |
|
| 22307 |
amit.gupta |
84 |
def __init__(self, id, rank, title, items):
|
| 27375 |
amit.gupta |
85 |
# Skubundle id
|
| 22307 |
amit.gupta |
86 |
self.id = id
|
|
|
87 |
self.rank = rank
|
|
|
88 |
self.title = title
|
|
|
89 |
self._childDocuments_ = items
|
| 27375 |
amit.gupta |
90 |
|
| 22307 |
amit.gupta |
91 |
def toJSON(self):
|
|
|
92 |
return json.dumps(self, default=lambda o: o.__dict__)
|
| 27375 |
amit.gupta |
93 |
|
| 31595 |
amit.gupta |
94 |
|
| 22307 |
amit.gupta |
95 |
class __Item:
|
| 27375 |
amit.gupta |
96 |
|
| 22307 |
amit.gupta |
97 |
def __init__(self, id, color, tags):
|
|
|
98 |
self.id = id
|
|
|
99 |
self.color = color
|
|
|
100 |
self._childDocuments_ = tags
|
| 27375 |
amit.gupta |
101 |
|
| 22307 |
amit.gupta |
102 |
def toJSON(self):
|
|
|
103 |
return json.dumps(self, default=lambda o: o.__dict__)
|
| 27375 |
amit.gupta |
104 |
|
|
|
105 |
|
| 22307 |
amit.gupta |
106 |
class __Tag:
|
| 27375 |
amit.gupta |
107 |
|
| 22307 |
amit.gupta |
108 |
def __init__(self, id, sellingPrice, mop):
|
|
|
109 |
self.id = id
|
|
|
110 |
self.sellingPrice = sellingPrice
|
|
|
111 |
self.mop = mop
|
| 31595 |
amit.gupta |
112 |
|
| 27375 |
amit.gupta |
113 |
# solr = pysolr.Solr(options.solrPath, timeout=10)
|
| 22307 |
amit.gupta |
114 |
|
|
|
115 |
|
|
|
116 |
def todict(obj, classkey=None):
|
|
|
117 |
if isinstance(obj, dict):
|
|
|
118 |
data = {}
|
|
|
119 |
for (k, v) in obj.items():
|
|
|
120 |
data[k] = todict(v, classkey)
|
|
|
121 |
return data
|
|
|
122 |
elif hasattr(obj, "_ast"):
|
|
|
123 |
return todict(obj._ast())
|
|
|
124 |
elif hasattr(obj, "__iter__"):
|
|
|
125 |
return [todict(v, classkey) for v in obj]
|
|
|
126 |
elif hasattr(obj, "__dict__"):
|
| 31595 |
amit.gupta |
127 |
data = dict([(key, todict(value, classkey))
|
|
|
128 |
for key, value in obj.__dict__.iteritems()
|
|
|
129 |
if not callable(value) and not key.startswith('_')])
|
| 22307 |
amit.gupta |
130 |
if classkey is not None and hasattr(obj, "__class__"):
|
|
|
131 |
data[classkey] = obj.__class__.__name__
|
|
|
132 |
return data
|
|
|
133 |
else:
|
|
|
134 |
return obj
|
| 27375 |
amit.gupta |
135 |
|
| 31595 |
amit.gupta |
136 |
|
| 27375 |
amit.gupta |
137 |
def ungzipResponse(r):
|
|
|
138 |
headers = r.info()
|
|
|
139 |
if headers.get('Content-Encoding') == 'gzip':
|
|
|
140 |
url_f = StringIO.StringIO(r.read())
|
|
|
141 |
gz = gzip.GzipFile(fileobj=url_f)
|
|
|
142 |
html = gz.read()
|
|
|
143 |
gz.close()
|
|
|
144 |
return html
|
|
|
145 |
return r.read()
|
| 22307 |
amit.gupta |
146 |
|
| 27375 |
amit.gupta |
147 |
|
|
|
148 |
def getAvailabilityJSON():
|
|
|
149 |
url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=warehousecisnew.xml&project=FOCOR&project_password=focor&target_format=JSON"
|
|
|
150 |
req = urllib2.Request(url, headers={})
|
|
|
151 |
response = urllib2.urlopen(req)
|
|
|
152 |
response_data = ungzipResponse(response)
|
|
|
153 |
response.close()
|
|
|
154 |
return json.loads(response_data)['data']
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
def getPendingPOJSON():
|
|
|
158 |
url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=UnfulfilledPOItemsNew.xml&project=FOCOR&project_password=focor&target_format=JSON"
|
|
|
159 |
req = urllib2.Request(url, headers={})
|
|
|
160 |
response = urllib2.urlopen(req)
|
|
|
161 |
response_data = ungzipResponse(response)
|
|
|
162 |
response.close()
|
|
|
163 |
return json.loads(response_data)['data']
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
def getItemMap():
|
|
|
167 |
rawAvailabilityList = getAvailabilityJSON()
|
|
|
168 |
rawPendingPoList = getPendingPOJSON()
|
|
|
169 |
availabilityItemMap = {}
|
|
|
170 |
# {"Warehouseid":"8468","Warehousefrom":"7573","Itemid":"29591","Availability":"8","Reserved":"0","Netavailability":"8"}
|
|
|
171 |
for rawAvailability in rawAvailabilityList:
|
|
|
172 |
if not availabilityItemMap.has_key(rawAvailability['Itemid']):
|
|
|
173 |
availabilityItemMap[rawAvailability['Itemid']] = {}
|
|
|
174 |
if not availabilityItemMap[rawAvailability['Itemid']].has_key(rawAvailability['Warehouseid']):
|
|
|
175 |
availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']] = {"netAvailability":0, "netPo":0}
|
| 31595 |
amit.gupta |
176 |
|
| 27375 |
amit.gupta |
177 |
availabilityItemMap[rawAvailability['Itemid']][rawAvailability['Warehouseid']]['netAvailability'] += int(rawAvailability['Netavailability'])
|
| 31595 |
amit.gupta |
178 |
|
| 27375 |
amit.gupta |
179 |
for rawPendingPo in rawPendingPoList:
|
|
|
180 |
if not availabilityItemMap.has_key(rawPendingPo['Itemid']):
|
|
|
181 |
availabilityItemMap[rawPendingPo['Itemid']] = {}
|
|
|
182 |
if not availabilityItemMap[rawPendingPo['Itemid']].has_key(rawPendingPo['Warehouseid']):
|
|
|
183 |
availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']] = {"netAvailability":0, "netPo":0}
|
|
|
184 |
availabilityItemMap[rawPendingPo['Itemid']][rawPendingPo['Warehouseid']]['netPo'] += int(rawPendingPo['Unfulfilled'])
|
| 31595 |
amit.gupta |
185 |
|
| 27375 |
amit.gupta |
186 |
return availabilityItemMap
|
|
|
187 |
|
|
|
188 |
|
| 22307 |
amit.gupta |
189 |
def populateTagItems():
|
| 27375 |
amit.gupta |
190 |
availabilityItemMap = getItemMap()
|
| 24109 |
amit.gupta |
191 |
tagRankingList = session.query(Tag_Ranking).order_by(desc(Tag_Ranking.rankPoints)).all()
|
|
|
192 |
rankingList = []
|
|
|
193 |
featureMap = {}
|
|
|
194 |
for tagRanking in tagRankingList:
|
|
|
195 |
rankingList.append(tagRanking.catalogItemId)
|
| 27375 |
amit.gupta |
196 |
featureMap[tagRanking.catalogItemId] = tagRanking.feature
|
| 22307 |
amit.gupta |
197 |
catalogMap = {}
|
| 27375 |
amit.gupta |
198 |
# stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()
|
|
|
199 |
# query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)
|
|
|
200 |
tuples = session.query(Tag_Listing, Item).join((Item, Item.id == Tag_Listing.item_id)).filter(or_(Item.status == status.ACTIVE, Item.status == status.PAUSED_BY_RISK, Item.status == status.PARTIALLY_ACTIVE))
|
|
|
201 |
projection = {'defaultImageUrl':1}
|
| 31595 |
amit.gupta |
202 |
excludeBrands = ["Live Demo", "Dummy", "FOC HANDSET"]
|
| 22307 |
amit.gupta |
203 |
for tag, item in tuples:
|
| 29666 |
amit.gupta |
204 |
if item.brand in excludeBrands:
|
| 24071 |
amit.gupta |
205 |
continue
|
| 25131 |
amit.gupta |
206 |
if not catalogMap.has_key(item.catalog_item_id):
|
| 22307 |
amit.gupta |
207 |
catalogObj = {}
|
| 24118 |
amit.gupta |
208 |
catalogObj['feature'] = ""
|
| 27375 |
amit.gupta |
209 |
catalogObj['title'] = " ".join(filter(None, [item.brand, item.model_name, item.model_number]))
|
| 24053 |
amit.gupta |
210 |
catalogObj['brand'] = (item.brand)
|
|
|
211 |
if item.brand == 'Huawei' or item.brand == 'Honor':
|
|
|
212 |
catalogObj['brand'] = ('Huawei', 'Honor')
|
| 26750 |
amit.gupta |
213 |
if item.brand == 'Mi' or item.brand == 'Xiaomi' or item.brand == 'Redmi':
|
|
|
214 |
catalogObj['brand'] = ('Mi', 'Xiaomi', 'Redmi')
|
| 22307 |
amit.gupta |
215 |
catalogObj['identifier'] = item.catalog_item_id
|
|
|
216 |
catalogObj['items'] = {}
|
|
|
217 |
filterMap = {"_id":item.catalog_item_id}
|
| 27375 |
amit.gupta |
218 |
# Dont include it catalog not available
|
| 22307 |
amit.gupta |
219 |
try:
|
| 23802 |
amit.gupta |
220 |
catalogObj['imageUrl'] = get_mongo_connection(options.mongoHost).CONTENT.siteContent.find_one(filterMap, projection)['defaultImageUrl']
|
| 22317 |
amit.gupta |
221 |
print catalogObj['imageUrl']
|
| 22307 |
amit.gupta |
222 |
except:
|
| 23354 |
amit.gupta |
223 |
try:
|
| 27375 |
amit.gupta |
224 |
catalogObj['imageUrl'] = 'https://images.smartdukaan.com/uploads/campaigns/' + str(item.catalog_item_id) + '.jpg'
|
| 23354 |
amit.gupta |
225 |
except:
|
|
|
226 |
traceback.print_exc()
|
| 31595 |
amit.gupta |
227 |
try:
|
| 24109 |
amit.gupta |
228 |
catalogObj['rank'] = rankingList.index(item.catalog_item_id)
|
| 22307 |
amit.gupta |
229 |
except:
|
| 27375 |
amit.gupta |
230 |
# A very big number
|
| 24109 |
amit.gupta |
231 |
traceback.print_exc()
|
| 23354 |
amit.gupta |
232 |
catalogObj['rank'] = 50000000
|
| 24118 |
amit.gupta |
233 |
if featureMap.has_key(item.catalog_item_id) and featureMap.get(item.catalog_item_id) is not None:
|
| 31595 |
amit.gupta |
234 |
catalogObj['feature'] = featureMap.get(item.catalog_item_id)
|
| 27375 |
amit.gupta |
235 |
# Mobile and tablets are showcased in same category
|
| 26757 |
amit.gupta |
236 |
catalogObj['categoryId'] = item.category if item.category in CATEGORY_MASTER else 6
|
| 27375 |
amit.gupta |
237 |
# Temporay change need to be removed
|
|
|
238 |
if item.category == 10006:
|
| 26757 |
amit.gupta |
239 |
catalogObj['categoryId'] = 3
|
| 26753 |
amit.gupta |
240 |
catalogObj['subCategoryId'] = item.category
|
| 24028 |
amit.gupta |
241 |
catalogObj['create_timestamp'] = to_java_date(tag.create_timestamp)
|
| 25130 |
amit.gupta |
242 |
catalogMap[item.catalog_item_id] = catalogObj
|
| 27375 |
amit.gupta |
243 |
catalogObj['avColor'] = {}
|
| 22307 |
amit.gupta |
244 |
|
| 23808 |
amit.gupta |
245 |
catalogObj = catalogMap.get(item.catalog_item_id)
|
| 27375 |
amit.gupta |
246 |
if availabilityItemMap.has_key(str(item.id)):
|
|
|
247 |
for warehouseId, avMap in availabilityItemMap[str(item.id)].iteritems():
|
|
|
248 |
print warehouseId, avMap, item.color, item.brand, item.model_name, item.model_number, item.catalog_item_id
|
|
|
249 |
if not catalogObj['avColor'].has_key(int(warehouseId)):
|
|
|
250 |
catalogObj['avColor'][int(warehouseId)] = 0
|
| 31595 |
amit.gupta |
251 |
|
| 27375 |
amit.gupta |
252 |
color = catalogObj['avColor'][int(warehouseId)]
|
|
|
253 |
if avMap['netAvailability'] > 0:
|
|
|
254 |
color = 2
|
|
|
255 |
elif avMap['netAvailability'] + avMap['netPo']:
|
|
|
256 |
color = max(color, 1)
|
|
|
257 |
else:
|
|
|
258 |
color = max(color, 0)
|
|
|
259 |
catalogObj['avColor'][int(warehouseId)] = color
|
| 23808 |
amit.gupta |
260 |
|
| 26886 |
amit.gupta |
261 |
if not catalogObj['items'].has_key(item.id):
|
| 24104 |
amit.gupta |
262 |
catalogObj['items'][item.id] = {'color': item.color.replace("f_", ""), 'tagPricing':[]}
|
| 26572 |
amit.gupta |
263 |
itemMap = catalogObj['items'].get(item.id)
|
|
|
264 |
itemMap['tagPricing'].append(tag)
|
| 31595 |
amit.gupta |
265 |
|
| 22307 |
amit.gupta |
266 |
catalogObjs = []
|
|
|
267 |
for catalogId, catalogMap in catalogMap.iteritems():
|
| 27375 |
amit.gupta |
268 |
print "catalogMap", catalogMap
|
| 30218 |
amit.gupta |
269 |
active = False
|
| 22307 |
amit.gupta |
270 |
itemsMap = catalogMap['items']
|
|
|
271 |
itemObjs = []
|
| 31559 |
tejbeer |
272 |
mop = 0
|
| 22307 |
amit.gupta |
273 |
for itemId, itemMap in itemsMap.iteritems():
|
| 31086 |
amit.gupta |
274 |
tags = itemMap.pop('tagPricing')
|
|
|
275 |
|
| 22307 |
amit.gupta |
276 |
for tag in tags:
|
| 30218 |
amit.gupta |
277 |
active = active or tag.active
|
| 27375 |
amit.gupta |
278 |
itemObj = {'id':('itemtag-%s-%s' % (itemId, tag.tag_id)), 'color_s':itemMap['color'], 'itemId_i': itemId, 'tagId_i':tag.tag_id,
|
| 26888 |
amit.gupta |
279 |
'mrp_f':tag.mrp, 'mop_f': tag.mop, 'sellingPrice_f': tag.selling_price, 'active_b' : tag.active, 'hot_deal_b': tag.hot_deals}
|
| 31559 |
tejbeer |
280 |
mop = tag.mop
|
| 22307 |
amit.gupta |
281 |
itemObjs.append(itemObj)
|
| 26885 |
amit.gupta |
282 |
catalogObj = {'id':'catalog' + str(catalogId), 'rank_i':catalogMap['rank'], 'title_s': catalogMap['title'],
|
| 26882 |
amit.gupta |
283 |
'_childDocuments_':itemObjs,
|
| 27375 |
amit.gupta |
284 |
'child_b' : len(itemObjs) > 0,
|
|
|
285 |
'catalogId_i':catalogId,
|
|
|
286 |
'imageUrl_s': catalogMap['imageUrl'].replace("saholic", "smartdukaan"),
|
| 26572 |
amit.gupta |
287 |
'feature_s':catalogMap['feature'],
|
| 27375 |
amit.gupta |
288 |
'brand_ss': catalogMap['brand'], 'create_s':catalogMap['create_timestamp'],
|
|
|
289 |
'categoryId_i':catalogMap['categoryId'],
|
| 28197 |
amit.gupta |
290 |
'subCategoryId_i':catalogMap['subCategoryId'],
|
| 27375 |
amit.gupta |
291 |
'w7573_i':0,
|
|
|
292 |
'w7678_i':0,
|
|
|
293 |
'w7720_i':0,
|
|
|
294 |
'w8468_i':0,
|
| 30079 |
amit.gupta |
295 |
'w8889_i':0,
|
| 31086 |
amit.gupta |
296 |
'w8947_i':0,
|
| 31105 |
amit.gupta |
297 |
'w9213_i':0,
|
| 31560 |
tejbeer |
298 |
'w9203_i':0,
|
| 31597 |
amit.gupta |
299 |
'show_default_b': "FOC" not in catalogMap['brand'],
|
| 31559 |
tejbeer |
300 |
'mop_f': mop
|
| 27375 |
amit.gupta |
301 |
}
|
|
|
302 |
for whId, color in catalogMap['avColor'].iteritems():
|
|
|
303 |
print catalogId, whId, color, catalogMap['title']
|
|
|
304 |
catalogObj['w%d_i'%(whId)] = color
|
| 30079 |
amit.gupta |
305 |
catalogObj['active_b'] = active
|
| 25132 |
amit.gupta |
306 |
catalogObjs.append(catalogObj)
|
| 22307 |
amit.gupta |
307 |
solr.delete(q='*:*')
|
|
|
308 |
solr.add(catalogObjs)
|
| 31086 |
amit.gupta |
309 |
#print catalogObjs
|
|
|
310 |
#for catalogObj in catalogObjs:
|
|
|
311 |
# solr.add(catalogObj)
|
| 22307 |
amit.gupta |
312 |
|
| 27375 |
amit.gupta |
313 |
|
| 22307 |
amit.gupta |
314 |
def pushData():
|
|
|
315 |
populateTagItems()
|
| 27375 |
amit.gupta |
316 |
|
| 31595 |
amit.gupta |
317 |
|
| 27375 |
amit.gupta |
318 |
if __name__ == '__main__':
|
| 22307 |
amit.gupta |
319 |
pushData()
|