| 22307 |
amit.gupta |
1 |
from elixir import *
|
|
|
2 |
from shop2020.clients.InventoryClient import InventoryClient
|
|
|
3 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
4 |
from shop2020.model.v1.catalog.impl.DataService import Item, Tag_Listing, \
|
|
|
5 |
Tag_Ranking, PrivateDeals
|
|
|
6 |
from shop2020.thriftpy.model.v1.catalog.ttypes import status
|
|
|
7 |
from sqlalchemy.sql.expression import or_, desc
|
|
|
8 |
from sqlalchemy.sql.functions import now
|
|
|
9 |
import json
|
|
|
10 |
import optparse
|
|
|
11 |
import os
|
|
|
12 |
import pymongo
|
|
|
13 |
import pysolr
|
|
|
14 |
import shutil
|
|
|
15 |
import traceback
|
| 24028 |
amit.gupta |
16 |
from dtr.utils.utils import to_java_date
|
| 22307 |
amit.gupta |
17 |
#import pymongo
|
|
|
18 |
#import pysolr
|
|
|
19 |
|
| 26753 |
amit.gupta |
20 |
CATEGORY_MASTER = [10006, 10010, 14202, 14203]
|
| 22307 |
amit.gupta |
21 |
parser = optparse.OptionParser()
|
| 22308 |
amit.gupta |
22 |
parser.add_option("-d", "--d", dest="dbHost",
|
|
|
23 |
default="127.0.0.1",
|
| 22307 |
amit.gupta |
24 |
type="string", help="The HOST where the mysql server is running",
|
| 22308 |
amit.gupta |
25 |
metavar="DBHOST")
|
| 22307 |
amit.gupta |
26 |
parser.add_option("-s", "--s", dest="solrPath",
|
| 24028 |
amit.gupta |
27 |
default="http://localhost:8984/solr/ 1",
|
| 22307 |
amit.gupta |
28 |
type="string", help="Complete solr path",
|
| 22308 |
amit.gupta |
29 |
metavar="SOLRHOST")
|
|
|
30 |
parser.add_option("-m", "--m", dest="mongoHost",
|
| 22311 |
amit.gupta |
31 |
default="localhost",
|
| 22308 |
amit.gupta |
32 |
type="string", help="Complete solr path",
|
| 22307 |
amit.gupta |
33 |
metavar="HOST")
|
|
|
34 |
|
|
|
35 |
(options, args) = parser.parse_args()
|
|
|
36 |
|
| 22312 |
amit.gupta |
37 |
con=None
|
| 22307 |
amit.gupta |
38 |
|
| 22310 |
amit.gupta |
39 |
DataService.initialize(db_hostname=options.dbHost)
|
| 26576 |
amit.gupta |
40 |
solr = pysolr.Solr(options.solrPath, always_commit=True, timeout=10)
|
| 22307 |
amit.gupta |
41 |
|
| 22308 |
amit.gupta |
42 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
43 |
global con
|
|
|
44 |
if con is None:
|
|
|
45 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
46 |
try:
|
|
|
47 |
con = pymongo.MongoClient(host, port)
|
|
|
48 |
except Exception, e:
|
|
|
49 |
print e
|
|
|
50 |
return None
|
|
|
51 |
return con
|
| 22307 |
amit.gupta |
52 |
|
|
|
53 |
class __SkuInfo:
|
|
|
54 |
|
|
|
55 |
def __init__(self, id, ids, brand, model_name, category_id, subCategoryId,thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
|
|
|
56 |
category, subCategory):
|
|
|
57 |
#Skubundle id
|
|
|
58 |
self.id = id
|
|
|
59 |
#_id of skus
|
|
|
60 |
self.ids = ids
|
|
|
61 |
self.brand = brand
|
|
|
62 |
self.model_name = model_name
|
|
|
63 |
self.category_id = category_id
|
|
|
64 |
self.subCategoryId = subCategoryId
|
|
|
65 |
self.thumbnail = thumbnail
|
|
|
66 |
self.title= title
|
|
|
67 |
self.brand_synonyms = brand_synonyms
|
|
|
68 |
self.model_name_synonyms = model_name_synonyms
|
|
|
69 |
self.source_product_names = source_product_names
|
|
|
70 |
self.category = category
|
|
|
71 |
self.subCategory = subCategory
|
|
|
72 |
|
|
|
73 |
def toJSON(self):
|
|
|
74 |
return json.dumps(self, default=lambda o: o.__dict__,
|
|
|
75 |
sort_keys=True, indent=4)
|
|
|
76 |
|
|
|
77 |
class __Catalog:
|
|
|
78 |
|
|
|
79 |
def __init__(self, id, rank, title, items):
|
|
|
80 |
#Skubundle id
|
|
|
81 |
self.id = id
|
|
|
82 |
self.rank = rank
|
|
|
83 |
self.title = title
|
|
|
84 |
self._childDocuments_ = items
|
|
|
85 |
def toJSON(self):
|
|
|
86 |
return json.dumps(self, default=lambda o: o.__dict__)
|
|
|
87 |
|
|
|
88 |
class __Item:
|
|
|
89 |
def __init__(self, id, color, tags):
|
|
|
90 |
self.id = id
|
|
|
91 |
self.color = color
|
|
|
92 |
self._childDocuments_ = tags
|
|
|
93 |
def toJSON(self):
|
|
|
94 |
return json.dumps(self, default=lambda o: o.__dict__)
|
|
|
95 |
class __Tag:
|
|
|
96 |
def __init__(self, id, sellingPrice, mop):
|
|
|
97 |
self.id = id
|
|
|
98 |
self.sellingPrice = sellingPrice
|
|
|
99 |
self.mop = mop
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
#solr = pysolr.Solr(options.solrPath, timeout=10)
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
def todict(obj, classkey=None):
|
|
|
106 |
if isinstance(obj, dict):
|
|
|
107 |
data = {}
|
|
|
108 |
for (k, v) in obj.items():
|
|
|
109 |
data[k] = todict(v, classkey)
|
|
|
110 |
return data
|
|
|
111 |
elif hasattr(obj, "_ast"):
|
|
|
112 |
return todict(obj._ast())
|
|
|
113 |
elif hasattr(obj, "__iter__"):
|
|
|
114 |
return [todict(v, classkey) for v in obj]
|
|
|
115 |
elif hasattr(obj, "__dict__"):
|
|
|
116 |
data = dict([(key, todict(value, classkey))
|
|
|
117 |
for key, value in obj.__dict__.iteritems()
|
|
|
118 |
if not callable(value) and not key.startswith('_')])
|
|
|
119 |
if classkey is not None and hasattr(obj, "__class__"):
|
|
|
120 |
data[classkey] = obj.__class__.__name__
|
|
|
121 |
return data
|
|
|
122 |
else:
|
|
|
123 |
return obj
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
def populateTagItems():
|
| 24109 |
amit.gupta |
127 |
tagRankingList = session.query(Tag_Ranking).order_by(desc(Tag_Ranking.rankPoints)).all()
|
|
|
128 |
rankingList = []
|
|
|
129 |
featureMap = {}
|
|
|
130 |
for tagRanking in tagRankingList:
|
|
|
131 |
rankingList.append(tagRanking.catalogItemId)
|
|
|
132 |
featureMap[tagRanking.catalogItemId]=tagRanking.feature
|
| 22307 |
amit.gupta |
133 |
catalogMap = {}
|
|
|
134 |
#stmt = session.query(PrivateDeals).filter_by(isActive=1).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate)).subquery()
|
|
|
135 |
#query = session.query(Item, privateDealAlias.dealPrice).outerjoin((privateDealAlias, Item.id==privateDealAlias.item_id)).filter(Item.status != status.PHASED_OUT)
|
| 24008 |
amit.gupta |
136 |
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))
|
| 23802 |
amit.gupta |
137 |
projection={'defaultImageUrl':1}
|
| 22307 |
amit.gupta |
138 |
for tag, item in tuples:
|
| 26572 |
amit.gupta |
139 |
if item.brand == "Live Demo":
|
| 24071 |
amit.gupta |
140 |
continue
|
| 25131 |
amit.gupta |
141 |
if not catalogMap.has_key(item.catalog_item_id):
|
| 22307 |
amit.gupta |
142 |
catalogObj = {}
|
| 24118 |
amit.gupta |
143 |
catalogObj['feature'] = ""
|
| 22307 |
amit.gupta |
144 |
catalogObj['title'] = " ".join(filter(None, [item.brand, item.model_name, item.model_number]))
|
| 24053 |
amit.gupta |
145 |
catalogObj['brand'] = (item.brand)
|
|
|
146 |
if item.brand == 'Huawei' or item.brand == 'Honor':
|
|
|
147 |
catalogObj['brand'] = ('Huawei', 'Honor')
|
| 26750 |
amit.gupta |
148 |
if item.brand == 'Mi' or item.brand == 'Xiaomi' or item.brand == 'Redmi':
|
|
|
149 |
catalogObj['brand'] = ('Mi', 'Xiaomi', 'Redmi')
|
| 22307 |
amit.gupta |
150 |
catalogObj['identifier'] = item.catalog_item_id
|
|
|
151 |
catalogObj['items'] = {}
|
|
|
152 |
filterMap = {"_id":item.catalog_item_id}
|
|
|
153 |
#Dont include it catalog not available
|
|
|
154 |
try:
|
| 23802 |
amit.gupta |
155 |
catalogObj['imageUrl'] = get_mongo_connection(options.mongoHost).CONTENT.siteContent.find_one(filterMap, projection)['defaultImageUrl']
|
| 22317 |
amit.gupta |
156 |
print catalogObj['imageUrl']
|
| 22307 |
amit.gupta |
157 |
except:
|
| 23354 |
amit.gupta |
158 |
try:
|
| 26572 |
amit.gupta |
159 |
catalogObj['imageUrl'] = 'https://images.smartdukaan.com/uploads/campaigns/' + str(item.catalog_item_id )+ '.jpg'
|
| 23354 |
amit.gupta |
160 |
except:
|
|
|
161 |
traceback.print_exc()
|
| 22307 |
amit.gupta |
162 |
try:
|
| 24109 |
amit.gupta |
163 |
catalogObj['rank'] = rankingList.index(item.catalog_item_id)
|
| 22307 |
amit.gupta |
164 |
except:
|
|
|
165 |
#A very big number
|
| 24109 |
amit.gupta |
166 |
traceback.print_exc()
|
| 23354 |
amit.gupta |
167 |
catalogObj['rank'] = 50000000
|
| 24118 |
amit.gupta |
168 |
if featureMap.has_key(item.catalog_item_id) and featureMap.get(item.catalog_item_id) is not None:
|
| 24115 |
amit.gupta |
169 |
catalogObj['feature'] = featureMap.get(item.catalog_item_id)
|
| 23354 |
amit.gupta |
170 |
#Mobile and tablets are showcased in same category
|
| 26757 |
amit.gupta |
171 |
catalogObj['categoryId'] = item.category if item.category in CATEGORY_MASTER else 6
|
|
|
172 |
#Temporay change need to be removed
|
|
|
173 |
if item.category==10006:
|
|
|
174 |
catalogObj['categoryId'] = 3
|
| 26753 |
amit.gupta |
175 |
catalogObj['subCategoryId'] = item.category
|
| 24028 |
amit.gupta |
176 |
catalogObj['create_timestamp'] = to_java_date(tag.create_timestamp)
|
| 25130 |
amit.gupta |
177 |
catalogMap[item.catalog_item_id] = catalogObj
|
| 22307 |
amit.gupta |
178 |
|
| 23808 |
amit.gupta |
179 |
catalogObj = catalogMap.get(item.catalog_item_id)
|
|
|
180 |
|
| 26885 |
amit.gupta |
181 |
catalogObj['hot_deals'] = tag.hot_deals
|
| 26572 |
amit.gupta |
182 |
if tag.active and not catalogObj['items'].has_key(item.id):
|
| 24104 |
amit.gupta |
183 |
catalogObj['items'][item.id] = {'color': item.color.replace("f_", ""), 'tagPricing':[]}
|
| 26572 |
amit.gupta |
184 |
itemMap = catalogObj['items'].get(item.id)
|
|
|
185 |
itemMap['tagPricing'].append(tag)
|
| 22307 |
amit.gupta |
186 |
|
|
|
187 |
|
|
|
188 |
catalogObjs = []
|
|
|
189 |
for catalogId, catalogMap in catalogMap.iteritems():
|
|
|
190 |
itemsMap = catalogMap['items']
|
|
|
191 |
itemObjs = []
|
|
|
192 |
for itemId, itemMap in itemsMap.iteritems():
|
|
|
193 |
tags = itemMap['tagPricing']
|
|
|
194 |
for tag in tags:
|
| 22315 |
amit.gupta |
195 |
itemObj = {'id':('itemtag-%s-%s'%(itemId, tag.tag_id)), 'color_s':itemMap['color'], 'itemId_i': itemId, 'tagId_i':tag.tag_id,
|
| 26881 |
amit.gupta |
196 |
'mop_f': tag.mop, 'sellingPrice_f': tag.selling_price, 'active_b' : tag.active}
|
| 22307 |
amit.gupta |
197 |
itemObjs.append(itemObj)
|
| 26885 |
amit.gupta |
198 |
catalogObj = {'id':'catalog' + str(catalogId), 'rank_i':catalogMap['rank'], 'title_s': catalogMap['title'],
|
|
|
199 |
'hot_deals_b':catalogMap['hot_deals'],
|
| 26882 |
amit.gupta |
200 |
'_childDocuments_':itemObjs,
|
|
|
201 |
'child_b' : len(itemObjs) > 0,
|
| 26572 |
amit.gupta |
202 |
'catalogId_i':catalogId,
|
|
|
203 |
'imageUrl_s': catalogMap['imageUrl'].replace("saholic", "smartdukaan"),
|
|
|
204 |
'feature_s':catalogMap['feature'],
|
|
|
205 |
'brand_ss': catalogMap['brand'], 'create_s':catalogMap['create_timestamp'],
|
|
|
206 |
'categoryId_i':catalogMap['categoryId'],
|
|
|
207 |
'subCategoryId_i':catalogMap['subCategoryId']}
|
| 24116 |
amit.gupta |
208 |
print catalogObj
|
| 25132 |
amit.gupta |
209 |
catalogObjs.append(catalogObj)
|
| 22307 |
amit.gupta |
210 |
solr.delete(q='*:*')
|
|
|
211 |
solr.add(catalogObjs)
|
|
|
212 |
|
|
|
213 |
def pushData():
|
|
|
214 |
populateTagItems()
|
|
|
215 |
|
|
|
216 |
if __name__=='__main__':
|
|
|
217 |
pushData()
|