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