| 13828 |
kshitij.so |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
| 15270 |
kshitij.so |
4 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
| 13917 |
kshitij.so |
5 |
from datetime import datetime, timedelta
|
| 13828 |
kshitij.so |
6 |
import time
|
| 14180 |
kshitij.so |
7 |
from multiprocessing import Pool as ThreadPool
|
|
|
8 |
from multiprocessing import cpu_count
|
| 14254 |
kshitij.so |
9 |
import optparse
|
| 14325 |
kshitij.so |
10 |
from dtr.storage.MemCache import MemCache
|
| 14705 |
kshitij.so |
11 |
from dtr.utils.utils import getCashBack
|
| 15270 |
kshitij.so |
12 |
import traceback
|
|
|
13 |
from operator import itemgetter
|
| 16085 |
kshitij.so |
14 |
import chardet
|
| 13828 |
kshitij.so |
15 |
|
|
|
16 |
con = None
|
|
|
17 |
|
| 14254 |
kshitij.so |
18 |
parser = optparse.OptionParser()
|
|
|
19 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
20 |
default="localhost",
|
|
|
21 |
type="string", help="The HOST where the mongo server is running",
|
|
|
22 |
metavar="mongo_host")
|
|
|
23 |
|
|
|
24 |
(options, args) = parser.parse_args()
|
|
|
25 |
|
| 14325 |
kshitij.so |
26 |
mc = MemCache(options.mongoHost)
|
| 14254 |
kshitij.so |
27 |
|
| 16869 |
kshitij.so |
28 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5,'PAYTM.COM':6}
|
| 16019 |
kshitij.so |
29 |
|
| 13828 |
kshitij.so |
30 |
headers = {
|
|
|
31 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
|
|
32 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
33 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
34 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
|
|
35 |
}
|
|
|
36 |
|
| 16429 |
kshitij.so |
37 |
|
| 14254 |
kshitij.so |
38 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
39 |
global con
|
|
|
40 |
if con is None:
|
|
|
41 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
42 |
try:
|
|
|
43 |
con = pymongo.MongoClient(host, port)
|
|
|
44 |
except Exception, e:
|
|
|
45 |
print e
|
|
|
46 |
return None
|
|
|
47 |
return con
|
|
|
48 |
|
| 14180 |
kshitij.so |
49 |
def populate():
|
|
|
50 |
toScrapMap = {}
|
| 14132 |
kshitij.so |
51 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
|
|
|
52 |
for bestSeller in bestSellers:
|
|
|
53 |
snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
|
|
|
54 |
for data in snapdealBestSellers:
|
| 14180 |
kshitij.so |
55 |
if not toScrapMap.has_key(data['_id']):
|
| 15270 |
kshitij.so |
56 |
data['dealFlag'] = 0
|
|
|
57 |
data['dealType'] = 0
|
| 14180 |
kshitij.so |
58 |
toScrapMap[data['_id']] = data
|
| 16174 |
kshitij.so |
59 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':3,'showDeal':1,'totalPoints':{'$gt':-100}}))
|
| 14252 |
kshitij.so |
60 |
for deal in dealFlagged:
|
|
|
61 |
if not toScrapMap.has_key(deal['_id']):
|
| 14261 |
kshitij.so |
62 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
| 15270 |
kshitij.so |
63 |
data[0]['dealFlag'] = 0
|
|
|
64 |
data[0]['dealType'] = 0
|
| 14261 |
kshitij.so |
65 |
toScrapMap[deal['_id']] = data[0]
|
| 15270 |
kshitij.so |
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 |
toScrapMap[manualDeal['sku']] = data[0]
|
|
|
74 |
else:
|
|
|
75 |
data = toScrapMap.get(manualDeal['sku'])
|
|
|
76 |
data['dealFlag'] = 1
|
|
|
77 |
data['dealType'] = manualDeal['dealType']
|
| 16502 |
kshitij.so |
78 |
|
| 16019 |
kshitij.so |
79 |
for val in toScrapMap.values():
|
|
|
80 |
updatePrices(val)
|
|
|
81 |
# pool = ThreadPool(cpu_count() *2)
|
|
|
82 |
# offset = 0
|
|
|
83 |
# limit =100
|
|
|
84 |
# while(offset<=len(toScrapMap.values())):
|
|
|
85 |
# pool.map(updatePrices,toScrapMap.values()[offset:offset+limit])
|
|
|
86 |
# offset = offset + limit
|
|
|
87 |
# pool.close()
|
|
|
88 |
# pool.join()
|
|
|
89 |
# print "joining threads at %s"%(str(datetime.now()))
|
| 14180 |
kshitij.so |
90 |
|
|
|
91 |
|
|
|
92 |
def updatePrices(data):
|
| 16502 |
kshitij.so |
93 |
if data.get('ignorePricing') ==1:
|
| 16429 |
kshitij.so |
94 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
95 |
return
|
| 14180 |
kshitij.so |
96 |
if data['source_id']!=3:
|
|
|
97 |
return
|
|
|
98 |
print data['identifier']
|
|
|
99 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
|
|
100 |
print "returning"
|
|
|
101 |
return
|
|
|
102 |
|
|
|
103 |
try:
|
|
|
104 |
if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
|
|
|
105 |
print "sku id is already updated",data['_id']
|
|
|
106 |
return
|
|
|
107 |
except:
|
|
|
108 |
pass
|
|
|
109 |
|
| 17294 |
kshitij.so |
110 |
url="http://www.snapdeal.com/acors/json/v2/gvbps?supc=%s&catUrl=&bn=&catId=175&start=0&count=10000&scoreCategoryUrl=mobiles-mobile-phones&make2Order=false&auto=false&isO2OVendorRequired=true"%(data['identifier'].strip())
|
| 14180 |
kshitij.so |
111 |
print url
|
|
|
112 |
lowestOfferPrice = 0
|
|
|
113 |
instock = 0
|
| 15270 |
kshitij.so |
114 |
buyBoxPrice = 0
|
|
|
115 |
isBuyBox = 1
|
|
|
116 |
stock = 0
|
| 15820 |
kshitij.so |
117 |
try:
|
| 16019 |
kshitij.so |
118 |
req = urllib2.Request(url,headers=headers)
|
|
|
119 |
response = urllib2.urlopen(req)
|
| 16085 |
kshitij.so |
120 |
snapdeal_data = response.read()
|
| 16087 |
kshitij.so |
121 |
response.close()
|
| 16019 |
kshitij.so |
122 |
except:
|
|
|
123 |
print "Unable to scrape %d"%(data['_id'])
|
| 16087 |
kshitij.so |
124 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':0,'priceUpdatedOn':to_java_date(datetime.now())}})
|
|
|
125 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0}})
|
| 16019 |
kshitij.so |
126 |
return
|
| 16085 |
kshitij.so |
127 |
encoding = chardet.detect(snapdeal_data)
|
| 16019 |
kshitij.so |
128 |
try:
|
| 16085 |
kshitij.so |
129 |
snapdeal_data = snapdeal_data.decode(encoding.get('encoding'))
|
| 15820 |
kshitij.so |
130 |
except:
|
| 16429 |
kshitij.so |
131 |
snapdeal_data = snapdeal_data.decode('latin-1')
|
| 16085 |
kshitij.so |
132 |
try:
|
|
|
133 |
vendorInfo = json.loads(snapdeal_data)
|
|
|
134 |
except:
|
| 16086 |
kshitij.so |
135 |
print "Unable to load json for %d"%(data['_id'])
|
| 15820 |
kshitij.so |
136 |
return
|
| 15270 |
kshitij.so |
137 |
|
| 15820 |
kshitij.so |
138 |
try:
|
|
|
139 |
buyBoxStock = vendorInfo['primaryVendor']['buyableInventory']
|
|
|
140 |
if buyBoxStock >0:
|
|
|
141 |
buyBoxPrice = vendorInfo['primaryVendor']['sellingPrice']
|
|
|
142 |
except:
|
|
|
143 |
pass
|
|
|
144 |
|
|
|
145 |
sortedVendorsData = sorted(vendorInfo['vendors'], key=itemgetter('sellingPrice'))
|
|
|
146 |
for sortedVendorData in sortedVendorsData:
|
|
|
147 |
lowestOfferPrice = float(sortedVendorData['sellingPrice'])
|
|
|
148 |
try:
|
|
|
149 |
stock = sortedVendorData['buyableInventory']
|
|
|
150 |
except:
|
|
|
151 |
pass
|
|
|
152 |
if stock > 0 and lowestOfferPrice > 0:
|
|
|
153 |
instock = 1
|
|
|
154 |
break
|
|
|
155 |
if buyBoxPrice != lowestOfferPrice:
|
|
|
156 |
isBuyBox = 0
|
|
|
157 |
|
| 14180 |
kshitij.so |
158 |
print lowestOfferPrice
|
|
|
159 |
print instock
|
| 14181 |
kshitij.so |
160 |
print "Lowest Offer Price for id %d is %d , stock is %d and stock count is %d" %(data['_id'],lowestOfferPrice,instock,stock)
|
| 14180 |
kshitij.so |
161 |
print "*************"
|
|
|
162 |
if instock == 1:
|
| 15270 |
kshitij.so |
163 |
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)
|
| 16019 |
kshitij.so |
164 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock,'codAvailable':data['codAvailable']}}, multi=True)
|
| 14180 |
kshitij.so |
165 |
else:
|
| 15270 |
kshitij.so |
166 |
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)
|
| 16019 |
kshitij.so |
167 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock,'codAvailable':data['codAvailable']}})
|
| 14180 |
kshitij.so |
168 |
|
|
|
169 |
try:
|
| 15270 |
kshitij.so |
170 |
recomputeDeal(data)
|
| 14180 |
kshitij.so |
171 |
except:
|
|
|
172 |
print "Unable to compute deal for ",data['skuBundleId']
|
|
|
173 |
|
| 14325 |
kshitij.so |
174 |
|
|
|
175 |
def populateNegativeDeals():
|
|
|
176 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
| 15270 |
kshitij.so |
177 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
178 |
|
| 16502 |
kshitij.so |
179 |
#def recomputePoints(item, deal):
|
|
|
180 |
# try:
|
|
|
181 |
# if item.get('available_price') == deal['available_price']:
|
|
|
182 |
# print "No need to compute points for %d , as price is still same" %(item['_id'])
|
|
|
183 |
# raise
|
|
|
184 |
# nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
|
|
185 |
# except:
|
|
|
186 |
# print traceback.print_exc()
|
|
|
187 |
# nlcPoints = deal['nlcPoints']
|
|
|
188 |
#
|
|
|
189 |
# 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())}}))
|
|
|
190 |
# if len(bundleDealPoints) > 0:
|
|
|
191 |
# item['manualDealThresholdPrice'] = bundleDealPoints[0]['dealThresholdPrice']
|
|
|
192 |
# dealPoints = bundleDealPoints[0]['dealPoints']
|
|
|
193 |
# else:
|
|
|
194 |
# dealPoints = 0
|
|
|
195 |
# item['manualDealThresholdPrice'] = None
|
|
|
196 |
#
|
|
|
197 |
# 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']}})
|
| 15270 |
kshitij.so |
198 |
|
| 14180 |
kshitij.so |
199 |
|
| 15270 |
kshitij.so |
200 |
def recomputeDeal(item):
|
| 13917 |
kshitij.so |
201 |
"""Lets recompute deal for this bundle"""
|
| 16019 |
kshitij.so |
202 |
print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
|
| 15270 |
kshitij.so |
203 |
skuBundleId = item['skuBundleId']
|
| 13917 |
kshitij.so |
204 |
|
|
|
205 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
206 |
bestPrice = float("inf")
|
|
|
207 |
bestOne = None
|
|
|
208 |
bestSellerPoints = 0
|
|
|
209 |
toUpdate = []
|
| 16019 |
kshitij.so |
210 |
prepaidBestPrice = float("inf")
|
|
|
211 |
prepaidBestOne = None
|
|
|
212 |
prepaidBestSellerPoints = 0
|
| 13917 |
kshitij.so |
213 |
for similarItem in similarItems:
|
| 16019 |
kshitij.so |
214 |
if similarItem['codAvailable'] ==1:
|
|
|
215 |
if mc.get("negative_deals") is None:
|
|
|
216 |
populateNegativeDeals()
|
| 16174 |
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'] < bestPrice:
|
|
|
224 |
bestOne = similarItem
|
|
|
225 |
bestPrice = similarItem['available_price']
|
|
|
226 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
227 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
228 |
bestOne = similarItem
|
|
|
229 |
bestPrice = similarItem['available_price']
|
|
|
230 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
231 |
else:
|
|
|
232 |
pass
|
| 13917 |
kshitij.so |
233 |
else:
|
| 16019 |
kshitij.so |
234 |
if mc.get("negative_deals") is None:
|
|
|
235 |
populateNegativeDeals()
|
| 16174 |
kshitij.so |
236 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
237 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
238 |
continue
|
|
|
239 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
240 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
241 |
continue
|
| 16869 |
kshitij.so |
242 |
if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
|
|
|
243 |
similarItem['available_price'] = similarItem['gross_price']
|
| 16019 |
kshitij.so |
244 |
if similarItem['available_price'] < prepaidBestPrice:
|
|
|
245 |
prepaidBestOne = similarItem
|
|
|
246 |
prepaidBestPrice = similarItem['available_price']
|
|
|
247 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
248 |
elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
249 |
prepaidBestOne = similarItem
|
|
|
250 |
prepaidBestPrice = similarItem['available_price']
|
|
|
251 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
252 |
else:
|
|
|
253 |
pass
|
| 16026 |
kshitij.so |
254 |
if bestOne is not None or prepaidBestOne is not None:
|
| 13917 |
kshitij.so |
255 |
for similarItem in similarItems:
|
|
|
256 |
toUpdate.append(similarItem['_id'])
|
| 16026 |
kshitij.so |
257 |
if bestOne is not None:
|
|
|
258 |
toUpdate.remove(bestOne['_id'])
|
|
|
259 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
|
|
|
260 |
if prepaidBestOne is not None:
|
| 16070 |
kshitij.so |
261 |
if bestOne is not None:
|
|
|
262 |
if prepaidBestOne['available_price'] < bestOne['available_price']:
|
|
|
263 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
264 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
|
|
265 |
else:
|
|
|
266 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
267 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 13917 |
kshitij.so |
268 |
if len(toUpdate) > 0:
|
| 16019 |
kshitij.so |
269 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
|
| 16026 |
kshitij.so |
270 |
|
| 13828 |
kshitij.so |
271 |
def main():
|
| 14180 |
kshitij.so |
272 |
populate()
|
| 13828 |
kshitij.so |
273 |
|
|
|
274 |
if __name__=='__main__':
|
|
|
275 |
main()
|