| 13828 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
3 |
from shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Item
|
|
|
4 |
from sqlalchemy.sql.functions import now
|
|
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
import pymongo
|
|
|
7 |
from dtr.utils.utils import to_java_date
|
| 13843 |
kshitij.so |
8 |
import optparse
|
| 14325 |
kshitij.so |
9 |
from dtr.storage.MemCache import MemCache
|
| 14705 |
kshitij.so |
10 |
from dtr.utils.utils import getCashBack
|
| 15268 |
kshitij.so |
11 |
from dtr.utils.utils import getNlcPoints
|
|
|
12 |
import traceback
|
| 13828 |
kshitij.so |
13 |
|
|
|
14 |
dealsMap = {}
|
|
|
15 |
con = None
|
|
|
16 |
dealsCatalogIds = []
|
|
|
17 |
itemCatalogMap = {}
|
|
|
18 |
|
| 13843 |
kshitij.so |
19 |
parser = optparse.OptionParser()
|
| 14259 |
kshitij.so |
20 |
parser.add_option("-H", "--host", dest="hostname",
|
| 13843 |
kshitij.so |
21 |
default="localhost",
|
|
|
22 |
type="string", help="The HOST where the DB server is running",
|
| 14259 |
kshitij.so |
23 |
metavar="db_host")
|
| 14253 |
kshitij.so |
24 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
25 |
default="localhost",
|
|
|
26 |
type="string", help="The HOST where the mongo server is running",
|
|
|
27 |
metavar="mongo_host")
|
| 13828 |
kshitij.so |
28 |
|
| 13849 |
kshitij.so |
29 |
(options, args) = parser.parse_args()
|
| 14325 |
kshitij.so |
30 |
|
|
|
31 |
mc = MemCache(options.mongoHost)
|
|
|
32 |
|
| 13843 |
kshitij.so |
33 |
DataService.initialize(db_hostname=options.hostname)
|
|
|
34 |
|
| 16019 |
kshitij.so |
35 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5}
|
|
|
36 |
|
| 14253 |
kshitij.so |
37 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
38 |
global con
|
|
|
39 |
if con is None:
|
|
|
40 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
41 |
try:
|
|
|
42 |
con = pymongo.MongoClient(host, port)
|
|
|
43 |
except Exception, e:
|
|
|
44 |
print e
|
|
|
45 |
return None
|
|
|
46 |
return con
|
|
|
47 |
|
|
|
48 |
def getPrivateDeals():
|
| 14259 |
kshitij.so |
49 |
try:
|
|
|
50 |
global dealsMap
|
|
|
51 |
dealsMap = dict()
|
|
|
52 |
all_active_items_query = session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))
|
|
|
53 |
all_active_private_deals = all_active_items_query.all()
|
|
|
54 |
if all_active_private_deals is not None or all_active_private_deals!=[]:
|
|
|
55 |
for active_private_deal in all_active_private_deals:
|
| 15268 |
kshitij.so |
56 |
print active_private_deal.item_id
|
| 14259 |
kshitij.so |
57 |
item = Item.get_by(id = active_private_deal.item_id)
|
|
|
58 |
if item.sellingPrice > active_private_deal.dealPrice and item.status==3:
|
|
|
59 |
dealsMap[active_private_deal.item_id] = active_private_deal
|
|
|
60 |
finally:
|
|
|
61 |
session.close()
|
| 13828 |
kshitij.so |
62 |
|
|
|
63 |
def getItemsToUpdate():
|
|
|
64 |
global dealsCatalogIds
|
|
|
65 |
global itemCatalogMap
|
| 15268 |
kshitij.so |
66 |
toScrapMap = {}
|
| 14130 |
kshitij.so |
67 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))
|
|
|
68 |
for bestSeller in bestSellers:
|
|
|
69 |
saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
|
|
|
70 |
for d in saholicCatalogIds:
|
|
|
71 |
if d['source_id']!=4:
|
|
|
72 |
continue
|
| 15268 |
kshitij.so |
73 |
d['dealFlag'] = 0
|
|
|
74 |
d['dealType'] = 0
|
|
|
75 |
toScrapMap[d['_id']] = d
|
| 14130 |
kshitij.so |
76 |
dealsCatalogIds.append(long(d['identifier'].strip()))
|
| 16180 |
kshitij.so |
77 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':-100}}))
|
| 15268 |
kshitij.so |
78 |
for deal in dealFlagged:
|
|
|
79 |
if not toScrapMap.has_key(deal['_id']):
|
|
|
80 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
|
|
81 |
data[0]['dealFlag'] = 0
|
|
|
82 |
data[0]['dealType'] = 0
|
|
|
83 |
toScrapMap[deal['_id']] = data[0]
|
|
|
84 |
if long(data[0]['identifier'].strip()) not in dealsCatalogIds:
|
|
|
85 |
dealsCatalogIds.append(long(data[0]['identifier'].strip()))
|
|
|
86 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':4}))
|
|
|
87 |
for manualDeal in manualDeals:
|
|
|
88 |
if not toScrapMap.has_key(manualDeal['sku']):
|
|
|
89 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
|
|
|
90 |
if len(data) > 0:
|
|
|
91 |
data[0]['dealFlag'] = 1
|
|
|
92 |
data[0]['dealType'] = manualDeal['dealType']
|
|
|
93 |
toScrapMap[manualDeal['sku']] = data[0]
|
|
|
94 |
if long(data[0]['identifier'].strip()) not in dealsCatalogIds:
|
|
|
95 |
dealsCatalogIds.append(long(data[0]['identifier'].strip()))
|
|
|
96 |
else:
|
|
|
97 |
data = toScrapMap.get(manualDeal['sku'])
|
|
|
98 |
data['dealFlag'] = 1
|
|
|
99 |
data['dealType'] = manualDeal['dealType']
|
|
|
100 |
|
| 13828 |
kshitij.so |
101 |
items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
|
|
|
102 |
for item in items:
|
|
|
103 |
temp = []
|
|
|
104 |
if not itemCatalogMap.has_key(item.catalog_item_id):
|
|
|
105 |
temp.append(item)
|
| 14179 |
kshitij.so |
106 |
print "****",item.catalog_item_id
|
| 13828 |
kshitij.so |
107 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
108 |
else:
|
|
|
109 |
val = itemCatalogMap.get(item.catalog_item_id)
|
|
|
110 |
for l in val:
|
|
|
111 |
temp.append(l)
|
|
|
112 |
temp.append(item)
|
|
|
113 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
114 |
|
| 15268 |
kshitij.so |
115 |
for saholicCatalogId in toScrapMap.itervalues():
|
| 14129 |
kshitij.so |
116 |
if saholicCatalogId['source_id']!=4:
|
|
|
117 |
continue
|
| 13916 |
kshitij.so |
118 |
d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
|
| 13828 |
kshitij.so |
119 |
available_price = None
|
| 14179 |
kshitij.so |
120 |
in_stock = 0
|
|
|
121 |
if d_items is not None:
|
| 13828 |
kshitij.so |
122 |
for d_item in d_items:
|
| 14179 |
kshitij.so |
123 |
in_stock = 0
|
| 13828 |
kshitij.so |
124 |
if d_item.status == 3:
|
|
|
125 |
in_stock =1
|
| 14179 |
kshitij.so |
126 |
else:
|
|
|
127 |
continue
|
|
|
128 |
if dealsMap.get(d_item.id) is not None:
|
|
|
129 |
available_price = dealsMap.get(d_item.id).dealPrice
|
|
|
130 |
if (available_price !=None):
|
| 13828 |
kshitij.so |
131 |
break
|
| 14179 |
kshitij.so |
132 |
if (available_price is None):
|
|
|
133 |
in_stock = 0
|
|
|
134 |
if d_items is not None:
|
|
|
135 |
for d_item in d_items:
|
|
|
136 |
if d_item.status == 3:
|
|
|
137 |
available_price = d_item.sellingPrice
|
|
|
138 |
in_stock =1
|
|
|
139 |
break
|
| 13828 |
kshitij.so |
140 |
print long(saholicCatalogId['identifier'])
|
|
|
141 |
print in_stock
|
|
|
142 |
print available_price
|
|
|
143 |
print dealsMap.get(d_item.id)
|
|
|
144 |
print "++++++++++++++++++++++++++"
|
| 13850 |
kshitij.so |
145 |
if available_price > 0 or available_price is not None:
|
| 14127 |
kshitij.so |
146 |
get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':in_stock}}, multi=True)
|
| 13916 |
kshitij.so |
147 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price , 'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
148 |
else:
|
| 14127 |
kshitij.so |
149 |
get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':in_stock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
|
| 13916 |
kshitij.so |
150 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
151 |
|
| 13916 |
kshitij.so |
152 |
try:
|
| 15268 |
kshitij.so |
153 |
recomputeDeal(saholicCatalogId)
|
| 13916 |
kshitij.so |
154 |
except:
|
|
|
155 |
print "Unable to compute deal for ",saholicCatalogId['skuBundleId']
|
|
|
156 |
|
| 14325 |
kshitij.so |
157 |
def populateNegativeDeals():
|
|
|
158 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
| 15268 |
kshitij.so |
159 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
160 |
|
| 16504 |
kshitij.so |
161 |
#def recomputePoints(item, deal):
|
|
|
162 |
# try:
|
|
|
163 |
# if item.get('available_price') == deal['available_price']:
|
|
|
164 |
# print "No need to compute points for %d , as price is still same" %(item['_id'])
|
|
|
165 |
# raise
|
|
|
166 |
# except:
|
|
|
167 |
# traceback.print_exc()
|
|
|
168 |
# nlcPoints = deal['nlcPoints']
|
|
|
169 |
#
|
|
|
170 |
# bundleDealPoints = list(get_mongo_connection().Catalog.DealPoints.find({'skuBundleId':item['skuBundleId'],'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}))
|
|
|
171 |
# if len(bundleDealPoints) > 0:
|
|
|
172 |
# item['manualDealThresholdPrice'] = bundleDealPoints[0]['dealThresholdPrice']
|
|
|
173 |
# dealPoints = bundleDealPoints[0]['dealPoints']
|
|
|
174 |
# else:
|
|
|
175 |
# dealPoints = 0
|
|
|
176 |
# item['manualDealThresholdPrice'] = None
|
|
|
177 |
#
|
|
|
178 |
#
|
|
|
179 |
# get_mongo_connection().Catalog.Deals.update({'_id':deal['_id']},{"$set":{'totalPoints':deal['totalPoints'] - deal['nlcPoints'] + nlcPoints - deal['dealPoints'] +dealPoints , 'nlcPoints': nlcPoints, 'dealPoints': dealPoints, 'manualDealThresholdPrice': item['manualDealThresholdPrice']}})
|
| 15268 |
kshitij.so |
180 |
|
|
|
181 |
def recomputeDeal(item):
|
| 13916 |
kshitij.so |
182 |
"""Lets recompute deal for this bundle"""
|
| 16019 |
kshitij.so |
183 |
print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
|
| 15268 |
kshitij.so |
184 |
skuBundleId = item['skuBundleId']
|
| 13916 |
kshitij.so |
185 |
|
|
|
186 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
187 |
bestPrice = float("inf")
|
|
|
188 |
bestOne = None
|
|
|
189 |
bestSellerPoints = 0
|
|
|
190 |
toUpdate = []
|
| 16019 |
kshitij.so |
191 |
prepaidBestPrice = float("inf")
|
|
|
192 |
prepaidBestOne = None
|
|
|
193 |
prepaidBestSellerPoints = 0
|
| 13916 |
kshitij.so |
194 |
for similarItem in similarItems:
|
| 16019 |
kshitij.so |
195 |
if similarItem['codAvailable'] ==1:
|
|
|
196 |
if mc.get("negative_deals") is None:
|
|
|
197 |
populateNegativeDeals()
|
| 16180 |
kshitij.so |
198 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
199 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
200 |
continue
|
|
|
201 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
202 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
203 |
continue
|
|
|
204 |
if similarItem['available_price'] < bestPrice:
|
|
|
205 |
bestOne = similarItem
|
|
|
206 |
bestPrice = similarItem['available_price']
|
|
|
207 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
208 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
209 |
bestOne = similarItem
|
|
|
210 |
bestPrice = similarItem['available_price']
|
|
|
211 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
212 |
else:
|
|
|
213 |
pass
|
| 13916 |
kshitij.so |
214 |
else:
|
| 16019 |
kshitij.so |
215 |
if mc.get("negative_deals") is None:
|
|
|
216 |
populateNegativeDeals()
|
| 16180 |
kshitij.so |
217 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
218 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
219 |
continue
|
|
|
220 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
221 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
222 |
continue
|
|
|
223 |
if similarItem['available_price'] < prepaidBestPrice:
|
|
|
224 |
prepaidBestOne = similarItem
|
|
|
225 |
prepaidBestPrice = similarItem['available_price']
|
|
|
226 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
227 |
elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
228 |
prepaidBestOne = similarItem
|
|
|
229 |
prepaidBestPrice = similarItem['available_price']
|
|
|
230 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
231 |
else:
|
|
|
232 |
pass
|
| 16026 |
kshitij.so |
233 |
if bestOne is not None or prepaidBestOne is not None:
|
| 13916 |
kshitij.so |
234 |
for similarItem in similarItems:
|
|
|
235 |
toUpdate.append(similarItem['_id'])
|
| 16026 |
kshitij.so |
236 |
if bestOne is not None:
|
|
|
237 |
toUpdate.remove(bestOne['_id'])
|
|
|
238 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
|
|
|
239 |
if prepaidBestOne is not None:
|
| 16075 |
kshitij.so |
240 |
if bestOne is not None:
|
|
|
241 |
if prepaidBestOne['available_price'] < bestOne['available_price']:
|
|
|
242 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
243 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
|
|
244 |
else:
|
|
|
245 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
246 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 13916 |
kshitij.so |
247 |
if len(toUpdate) > 0:
|
| 16019 |
kshitij.so |
248 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
|
|
|
249 |
|
|
|
250 |
|
| 13916 |
kshitij.so |
251 |
|
| 13828 |
kshitij.so |
252 |
def main():
|
|
|
253 |
getPrivateDeals()
|
| 14259 |
kshitij.so |
254 |
try:
|
|
|
255 |
getItemsToUpdate()
|
|
|
256 |
finally:
|
|
|
257 |
session.close()
|
| 15268 |
kshitij.so |
258 |
print "Done with saholic pricing"
|
| 13828 |
kshitij.so |
259 |
|
|
|
260 |
if __name__=='__main__':
|
|
|
261 |
main()
|