| Line 1... |
Line 1... |
| 1 |
import urllib2
|
1 |
import urllib2
|
| 2 |
import simplejson as json
|
2 |
import simplejson as json
|
| 3 |
import pymongo
|
3 |
import pymongo
|
| 4 |
from dtr.utils.utils import to_java_date
|
4 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
| 5 |
from datetime import datetime, timedelta
|
5 |
from datetime import datetime, timedelta
|
| 6 |
import time
|
6 |
import time
|
| 7 |
from multiprocessing import Pool as ThreadPool
|
7 |
from multiprocessing import Pool as ThreadPool
|
| 8 |
from multiprocessing import cpu_count
|
8 |
from multiprocessing import cpu_count
|
| 9 |
import optparse
|
9 |
import optparse
|
| 10 |
from dtr.storage.MemCache import MemCache
|
10 |
from dtr.storage.MemCache import MemCache
|
| 11 |
from dtr.utils.utils import getCashBack
|
11 |
from dtr.utils.utils import getCashBack
|
| - |
|
12 |
import traceback
|
| - |
|
13 |
from operator import itemgetter
|
| 12 |
|
14 |
|
| 13 |
con = None
|
15 |
con = None
|
| 14 |
|
16 |
|
| 15 |
parser = optparse.OptionParser()
|
17 |
parser = optparse.OptionParser()
|
| 16 |
parser.add_option("-m", "--m", dest="mongoHost",
|
18 |
parser.add_option("-m", "--m", dest="mongoHost",
|
| Line 45... |
Line 47... |
| 45 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
|
47 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
|
| 46 |
for bestSeller in bestSellers:
|
48 |
for bestSeller in bestSellers:
|
| 47 |
snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
|
49 |
snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
|
| 48 |
for data in snapdealBestSellers:
|
50 |
for data in snapdealBestSellers:
|
| 49 |
if not toScrapMap.has_key(data['_id']):
|
51 |
if not toScrapMap.has_key(data['_id']):
|
| - |
|
52 |
data['dealFlag'] = 0
|
| - |
|
53 |
data['dealType'] = 0
|
| - |
|
54 |
data['dealPoints'] = 0
|
| - |
|
55 |
data['manualDealThresholdPrice'] = None
|
| 50 |
toScrapMap[data['_id']] = data
|
56 |
toScrapMap[data['_id']] = data
|
| 51 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':3,'showDeal':1,'totalPoints':{'$gt':0}}))
|
57 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':3,'showDeal':1,'totalPoints':{'$gt':0}}))
|
| 52 |
for deal in dealFlagged:
|
58 |
for deal in dealFlagged:
|
| 53 |
if not toScrapMap.has_key(deal['_id']):
|
59 |
if not toScrapMap.has_key(deal['_id']):
|
| 54 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
60 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
| - |
|
61 |
data[0]['dealFlag'] = 0
|
| - |
|
62 |
data[0]['dealType'] = 0
|
| - |
|
63 |
data[0]['dealPoints'] = 0
|
| - |
|
64 |
data[0]['manualDealThresholdPrice'] = None
|
| 55 |
toScrapMap[deal['_id']] = data[0]
|
65 |
toScrapMap[deal['_id']] = data[0]
|
| - |
|
66 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':3}))
|
| - |
|
67 |
for manualDeal in manualDeals:
|
| - |
|
68 |
if not toScrapMap.has_key(manualDeal['sku']):
|
| - |
|
69 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
|
| - |
|
70 |
if len(data) > 0:
|
| - |
|
71 |
data[0]['dealFlag'] = 1
|
| - |
|
72 |
data[0]['dealType'] = manualDeal['dealType']
|
| - |
|
73 |
data[0]['dealPoints'] = manualDeal['dealPoints']
|
| - |
|
74 |
data[0]['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
|
| - |
|
75 |
toScrapMap[manualDeal['sku']] = data[0]
|
| - |
|
76 |
else:
|
| - |
|
77 |
data = toScrapMap.get(manualDeal['sku'])
|
| - |
|
78 |
data['dealFlag'] = 1
|
| - |
|
79 |
data['dealType'] = manualDeal['dealType']
|
| - |
|
80 |
data['dealPoints'] = manualDeal['dealPoints']
|
| - |
|
81 |
data['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
|
| 56 |
pool = ThreadPool(cpu_count() *2)
|
82 |
pool = ThreadPool(cpu_count() *2)
|
| 57 |
pool.map(updatePrices,toScrapMap.values())
|
83 |
pool.map(updatePrices,toScrapMap.values())
|
| 58 |
pool.close()
|
84 |
pool.close()
|
| 59 |
pool.join()
|
85 |
pool.join()
|
| 60 |
print "joining threads at %s"%(str(datetime.now()))
|
86 |
print "joining threads at %s"%(str(datetime.now()))
|
| Line 73... |
Line 99... |
| 73 |
print "sku id is already updated",data['_id']
|
99 |
print "sku id is already updated",data['_id']
|
| 74 |
return
|
100 |
return
|
| 75 |
except:
|
101 |
except:
|
| 76 |
pass
|
102 |
pass
|
| 77 |
|
103 |
|
| 78 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
|
104 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175"%(data['identifier'].strip())
|
| 79 |
print url
|
105 |
print url
|
| 80 |
time.sleep(1)
|
106 |
time.sleep(1)
|
| 81 |
lowestOfferPrice = 0
|
107 |
lowestOfferPrice = 0
|
| 82 |
instock = 0
|
108 |
instock = 0
|
| - |
|
109 |
buyBoxPrice = 0
|
| - |
|
110 |
isBuyBox = 1
|
| - |
|
111 |
stock = 0
|
| 83 |
req = urllib2.Request(url,headers=headers)
|
112 |
req = urllib2.Request(url,headers=headers)
|
| 84 |
response = urllib2.urlopen(req)
|
113 |
response = urllib2.urlopen(req)
|
| 85 |
json_input = response.read()
|
114 |
json_input = response.read()
|
| 86 |
response.close()
|
115 |
response.close()
|
| 87 |
if len(json_input) > 0:
|
116 |
if len(json_input) > 0:
|
| 88 |
vendorInfo = json.loads(json_input)
|
117 |
vendorInfo = json.loads(json_input)
|
| 89 |
for vendor in vendorInfo:
|
118 |
for vendor in vendorInfo:
|
| 90 |
lowestOfferPrice = float(vendor['sellingPrice'])
|
119 |
buyBoxPrice = float(vendor['sellingPrice'])
|
| 91 |
try:
|
120 |
try:
|
| 92 |
stock = vendor['buyableInventory']
|
121 |
buyBoxStock = vendor['buyableInventory']
|
| 93 |
except:
|
122 |
except:
|
| 94 |
stock = 0
|
123 |
buyBoxStock = 0
|
| - |
|
124 |
if buyBoxStock > 0 and buyBoxPrice > 0:
|
| - |
|
125 |
break
|
| - |
|
126 |
|
| - |
|
127 |
sortedVendorsData = sorted(vendorInfo, key=itemgetter('sellingPrice'))
|
| - |
|
128 |
for sortedVendorData in sortedVendorsData:
|
| - |
|
129 |
lowestOfferPrice = float(sortedVendorData['sellingPrice'])
|
| - |
|
130 |
try:
|
| - |
|
131 |
stock = sortedVendorData['buyableInventory']
|
| - |
|
132 |
except:
|
| - |
|
133 |
pass
|
| 95 |
if stock > 0 and lowestOfferPrice > 0:
|
134 |
if stock > 0 and lowestOfferPrice > 0:
|
| 96 |
instock = 1
|
135 |
instock = 1
|
| 97 |
break
|
136 |
break
|
| 98 |
else:
|
- |
|
| 99 |
lowestOfferPrice = 0
|
137 |
if buyBoxPrice != lowestOfferPrice:
|
| 100 |
stock = 0
|
- |
|
| 101 |
instock = 0
|
138 |
isBuyBox = 0
|
| 102 |
|
139 |
|
| 103 |
print lowestOfferPrice
|
140 |
print lowestOfferPrice
|
| 104 |
print instock
|
141 |
print instock
|
| 105 |
print stock
|
- |
|
| 106 |
print "Lowest Offer Price for id %d is %d , stock is %d and stock count is %d" %(data['_id'],lowestOfferPrice,instock,stock)
|
142 |
print "Lowest Offer Price for id %d is %d , stock is %d and stock count is %d" %(data['_id'],lowestOfferPrice,instock,stock)
|
| 107 |
print "*************"
|
143 |
print "*************"
|
| 108 |
if instock == 1:
|
144 |
if instock == 1:
|
| 109 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':instock}}, multi=True)
|
145 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':instock,'buyBoxFlag':isBuyBox}}, multi=True)
|
| 110 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
|
146 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
|
| 111 |
else:
|
147 |
else:
|
| 112 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':instock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
|
148 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':instock,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':isBuyBox}}, multi=True)
|
| 113 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
|
149 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
|
| 114 |
|
150 |
|
| 115 |
try:
|
151 |
try:
|
| 116 |
recomputeDeal(data['skuBundleId'])
|
152 |
recomputeDeal(data)
|
| 117 |
except:
|
153 |
except:
|
| 118 |
print "Unable to compute deal for ",data['skuBundleId']
|
154 |
print "Unable to compute deal for ",data['skuBundleId']
|
| 119 |
|
155 |
|
| 120 |
|
156 |
|
| 121 |
def populateNegativeDeals():
|
157 |
def populateNegativeDeals():
|
| 122 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
158 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
| 123 |
mc.set("negative_deals", negativeDeals, 600)
|
159 |
mc.set("negative_deals", negativeDeals, 600)
|
| - |
|
160 |
|
| - |
|
161 |
def recomputePoints(item, deal):
|
| - |
|
162 |
try:
|
| - |
|
163 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
| - |
|
164 |
except:
|
| - |
|
165 |
traceback.print_exc()
|
| - |
|
166 |
nlcPoints = deal['nlcPoints']
|
| - |
|
167 |
if item['manualDealThresholdPrice'] >= deal['available_price']:
|
| - |
|
168 |
dealPoints = item['dealPoints']
|
| - |
|
169 |
else:
|
| - |
|
170 |
dealPoints = 0
|
| - |
|
171 |
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']}})
|
| - |
|
172 |
|
| 124 |
|
173 |
|
| 125 |
def recomputeDeal(skuBundleId):
|
174 |
def recomputeDeal(item):
|
| 126 |
"""Lets recompute deal for this bundle"""
|
175 |
"""Lets recompute deal for this bundle"""
|
| 127 |
print "Recomputing for bundleId",skuBundleId
|
176 |
print "Recomputing for bundleId",item.get('skuBundleId')
|
| - |
|
177 |
skuBundleId = item['skuBundleId']
|
| 128 |
|
178 |
|
| 129 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
179 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
| 130 |
bestPrice = float("inf")
|
180 |
bestPrice = float("inf")
|
| 131 |
bestOne = None
|
181 |
bestOne = None
|
| 132 |
bestSellerPoints = 0
|
182 |
bestSellerPoints = 0
|
| 133 |
toUpdate = []
|
183 |
toUpdate = []
|
| 134 |
for similarItem in similarItems:
|
184 |
for similarItem in similarItems:
|
| 135 |
if mc.get("negative_deals") is None:
|
185 |
if mc.get("negative_deals") is None:
|
| 136 |
populateNegativeDeals()
|
186 |
populateNegativeDeals()
|
| 137 |
try:
|
187 |
# try:
|
| 138 |
cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
188 |
# cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
| 139 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
189 |
# if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 140 |
pass
|
190 |
# pass
|
| 141 |
else:
|
191 |
# else:
|
| 142 |
if cashBack['cash_back_type'] ==1:
|
192 |
# if cashBack['cash_back_type'] ==1:
|
| 143 |
similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
193 |
# similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
| 144 |
elif cashBack['cash_back_type'] ==2:
|
194 |
# elif cashBack['cash_back_type'] ==2:
|
| 145 |
similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
195 |
# similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
| 146 |
else:
|
196 |
# else:
|
| 147 |
pass
|
197 |
# pass
|
| 148 |
except Exception as cashBackEx:
|
198 |
# except Exception as cashBackEx:
|
| 149 |
print cashBackEx
|
199 |
# print cashBackEx
|
| 150 |
print "Error calculating cashback."
|
200 |
# print "Error calculating cashback."
|
| - |
|
201 |
if similarItem['_id'] == item['_id']:
|
| - |
|
202 |
try:
|
| - |
|
203 |
recomputePoints(item, similarItem)
|
| - |
|
204 |
except:
|
| - |
|
205 |
traceback.print_exc()
|
| 151 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
206 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 152 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
207 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| 153 |
continue
|
208 |
continue
|
| 154 |
if similarItem['available_price'] < bestPrice:
|
209 |
if similarItem['available_price'] < bestPrice:
|
| 155 |
bestOne = similarItem
|
210 |
bestOne = similarItem
|