| 13828 |
kshitij.so |
1 |
import pymongo
|
| 15269 |
kshitij.so |
2 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
| 13915 |
kshitij.so |
3 |
from datetime import datetime, timedelta
|
| 13828 |
kshitij.so |
4 |
from operator import itemgetter
|
| 14123 |
kshitij.so |
5 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper
|
| 14178 |
kshitij.so |
6 |
from multiprocessing import Pool as ThreadPool
|
| 14172 |
kshitij.so |
7 |
from multiprocessing import cpu_count
|
| 14255 |
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
|
| 15269 |
kshitij.so |
11 |
import traceback
|
| 13828 |
kshitij.so |
12 |
|
|
|
13 |
con = None
|
|
|
14 |
|
| 14255 |
kshitij.so |
15 |
parser = optparse.OptionParser()
|
|
|
16 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
17 |
default="localhost",
|
|
|
18 |
type="string", help="The HOST where the mongo server is running",
|
|
|
19 |
metavar="mongo_host")
|
|
|
20 |
|
|
|
21 |
(options, args) = parser.parse_args()
|
|
|
22 |
|
| 14325 |
kshitij.so |
23 |
mc = MemCache(options.mongoHost)
|
|
|
24 |
|
| 16869 |
kshitij.so |
25 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5,'PAYTM.COM':6}
|
| 15610 |
kshitij.so |
26 |
|
| 14255 |
kshitij.so |
27 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
28 |
global con
|
|
|
29 |
if con is None:
|
|
|
30 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
31 |
try:
|
|
|
32 |
con = pymongo.MongoClient(host, port)
|
|
|
33 |
except Exception, e:
|
|
|
34 |
print e
|
|
|
35 |
return None
|
|
|
36 |
return con
|
|
|
37 |
|
| 14149 |
kshitij.so |
38 |
def populate():
|
|
|
39 |
toScrapMap = {}
|
| 14131 |
kshitij.so |
40 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
|
|
|
41 |
for bestSeller in bestSellers:
|
| 14149 |
kshitij.so |
42 |
amazonBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':2}))
|
|
|
43 |
for data in amazonBestSellers:
|
|
|
44 |
if not toScrapMap.has_key(data['_id']):
|
| 15269 |
kshitij.so |
45 |
data['dealFlag'] = 0
|
|
|
46 |
data['dealType'] = 0
|
| 14149 |
kshitij.so |
47 |
toScrapMap[data['_id']] = data
|
| 16175 |
kshitij.so |
48 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':2,'showDeal':1,'totalPoints':{'$gt':-100}}))
|
| 14251 |
kshitij.so |
49 |
for deal in dealFlagged:
|
|
|
50 |
if not toScrapMap.has_key(deal['_id']):
|
| 14262 |
kshitij.so |
51 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
| 15269 |
kshitij.so |
52 |
data[0]['dealFlag'] = 0
|
|
|
53 |
data[0]['dealType'] = 0
|
| 14262 |
kshitij.so |
54 |
toScrapMap[deal['_id']] = data[0]
|
| 15269 |
kshitij.so |
55 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':2}))
|
|
|
56 |
for manualDeal in manualDeals:
|
|
|
57 |
if not toScrapMap.has_key(manualDeal['sku']):
|
|
|
58 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
|
|
|
59 |
if len(data) > 0:
|
|
|
60 |
data[0]['dealFlag'] = 1
|
|
|
61 |
data[0]['dealType'] = manualDeal['dealType']
|
|
|
62 |
toScrapMap[manualDeal['sku']] = data[0]
|
|
|
63 |
else:
|
|
|
64 |
data = toScrapMap.get(manualDeal['sku'])
|
|
|
65 |
data['dealFlag'] = 1
|
|
|
66 |
data['dealType'] = manualDeal['dealType']
|
| 14178 |
kshitij.so |
67 |
pool = ThreadPool(cpu_count() *2)
|
| 14149 |
kshitij.so |
68 |
pool.map(scrapeFlipkart,toScrapMap.values())
|
|
|
69 |
pool.close()
|
|
|
70 |
pool.join()
|
| 14251 |
kshitij.so |
71 |
print "joining threads at %s"%(str(datetime.now()))
|
| 14149 |
kshitij.so |
72 |
|
|
|
73 |
def scrapeFlipkart(data):
|
|
|
74 |
if data['source_id']!=2:
|
| 14157 |
kshitij.so |
75 |
return
|
| 14149 |
kshitij.so |
76 |
retryCount = 0
|
|
|
77 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 14157 |
kshitij.so |
78 |
print "returning in valid identifier"
|
|
|
79 |
return
|
| 14149 |
kshitij.so |
80 |
|
| 16503 |
kshitij.so |
81 |
if data.get('ignorePricing') ==1:
|
| 15610 |
kshitij.so |
82 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
83 |
return
|
|
|
84 |
|
| 14149 |
kshitij.so |
85 |
try:
|
|
|
86 |
if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
|
| 15269 |
kshitij.so |
87 |
print "sku id is already updated %d" %(data['_id'])
|
| 14157 |
kshitij.so |
88 |
return
|
| 14149 |
kshitij.so |
89 |
except:
|
|
|
90 |
pass
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
lowestSp = 0
|
|
|
94 |
inStock = 0
|
| 15269 |
kshitij.so |
95 |
buyBoxPrice = 0
|
|
|
96 |
isBuyBox = 0
|
| 14157 |
kshitij.so |
97 |
scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
|
| 14149 |
kshitij.so |
98 |
try:
|
| 17264 |
kshitij.so |
99 |
result = scraperProductPage.read(data['identifier'])
|
|
|
100 |
if result.get('lowestSp')!=0:
|
|
|
101 |
lowestSp = result.get('lowestSp')
|
|
|
102 |
inStock = result.get('inStock')
|
|
|
103 |
buyBoxPrice = result.get('buyBoxPrice')
|
| 14149 |
kshitij.so |
104 |
except:
|
| 15269 |
kshitij.so |
105 |
print "Unable to scrape product page %s" %(data['identifier'])
|
| 17264 |
kshitij.so |
106 |
return
|
| 14149 |
kshitij.so |
107 |
|
|
|
108 |
|
|
|
109 |
print lowestSp
|
|
|
110 |
print inStock
|
| 15269 |
kshitij.so |
111 |
if buyBoxPrice is not None and buyBoxPrice == lowestSp:
|
|
|
112 |
isBuyBox = 1
|
| 14149 |
kshitij.so |
113 |
if lowestSp > 0:
|
| 15269 |
kshitij.so |
114 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':inStock,'buyBoxFlag':isBuyBox}}, multi=True)
|
| 16019 |
kshitij.so |
115 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
|
| 14149 |
kshitij.so |
116 |
else:
|
| 15269 |
kshitij.so |
117 |
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 |
118 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable']}})
|
| 14149 |
kshitij.so |
119 |
|
|
|
120 |
try:
|
| 15269 |
kshitij.so |
121 |
recomputeDeal(data)
|
| 14149 |
kshitij.so |
122 |
except:
|
| 15269 |
kshitij.so |
123 |
print "Unable to compute deal for %s"%(data['skuBundleId'])
|
| 13828 |
kshitij.so |
124 |
|
| 16503 |
kshitij.so |
125 |
#def recomputePoints(item, deal):
|
|
|
126 |
# try:
|
|
|
127 |
# if item.get('available_price') == deal['available_price']:
|
|
|
128 |
# print "No need to compute points for %d , as price is still same" %(item['_id'])
|
|
|
129 |
# raise
|
|
|
130 |
# nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
|
|
131 |
# except:
|
|
|
132 |
# print traceback.print_exc()
|
|
|
133 |
# nlcPoints = deal['nlcPoints']
|
|
|
134 |
#
|
|
|
135 |
# 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())}}))
|
|
|
136 |
# if len(bundleDealPoints) > 0:
|
|
|
137 |
# item['manualDealThresholdPrice'] = bundleDealPoints[0]['dealThresholdPrice']
|
|
|
138 |
# dealPoints = bundleDealPoints[0]['dealPoints']
|
|
|
139 |
# else:
|
|
|
140 |
# dealPoints = 0
|
|
|
141 |
# item['manualDealThresholdPrice'] = None
|
|
|
142 |
#
|
|
|
143 |
# 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']}})
|
| 15269 |
kshitij.so |
144 |
|
| 14325 |
kshitij.so |
145 |
def populateNegativeDeals():
|
|
|
146 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
147 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
148 |
|
| 15269 |
kshitij.so |
149 |
def recomputeDeal(item):
|
| 13915 |
kshitij.so |
150 |
"""Lets recompute deal for this bundle"""
|
| 15269 |
kshitij.so |
151 |
print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
|
|
|
152 |
skuBundleId = item['skuBundleId']
|
| 13915 |
kshitij.so |
153 |
|
|
|
154 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
155 |
bestPrice = float("inf")
|
|
|
156 |
bestOne = None
|
|
|
157 |
bestSellerPoints = 0
|
|
|
158 |
toUpdate = []
|
| 16019 |
kshitij.so |
159 |
prepaidBestPrice = float("inf")
|
|
|
160 |
prepaidBestOne = None
|
|
|
161 |
prepaidBestSellerPoints = 0
|
| 13915 |
kshitij.so |
162 |
for similarItem in similarItems:
|
| 16019 |
kshitij.so |
163 |
if similarItem['codAvailable'] ==1:
|
|
|
164 |
if mc.get("negative_deals") is None:
|
|
|
165 |
populateNegativeDeals()
|
| 16175 |
kshitij.so |
166 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
167 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
168 |
continue
|
|
|
169 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
170 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
171 |
continue
|
|
|
172 |
if similarItem['available_price'] < bestPrice:
|
|
|
173 |
bestOne = similarItem
|
|
|
174 |
bestPrice = similarItem['available_price']
|
|
|
175 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
176 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
177 |
bestOne = similarItem
|
|
|
178 |
bestPrice = similarItem['available_price']
|
|
|
179 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
180 |
else:
|
|
|
181 |
pass
|
| 13915 |
kshitij.so |
182 |
else:
|
| 16019 |
kshitij.so |
183 |
if mc.get("negative_deals") is None:
|
|
|
184 |
populateNegativeDeals()
|
| 16175 |
kshitij.so |
185 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 16019 |
kshitij.so |
186 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
|
|
187 |
continue
|
|
|
188 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
189 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
|
|
190 |
continue
|
| 16869 |
kshitij.so |
191 |
if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
|
|
|
192 |
similarItem['available_price'] = similarItem['gross_price']
|
| 16019 |
kshitij.so |
193 |
if similarItem['available_price'] < prepaidBestPrice:
|
|
|
194 |
prepaidBestOne = similarItem
|
|
|
195 |
prepaidBestPrice = similarItem['available_price']
|
|
|
196 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
197 |
elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
198 |
prepaidBestOne = similarItem
|
|
|
199 |
prepaidBestPrice = similarItem['available_price']
|
|
|
200 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
201 |
else:
|
|
|
202 |
pass
|
| 16026 |
kshitij.so |
203 |
if bestOne is not None or prepaidBestOne is not None:
|
| 13915 |
kshitij.so |
204 |
for similarItem in similarItems:
|
|
|
205 |
toUpdate.append(similarItem['_id'])
|
| 16026 |
kshitij.so |
206 |
if bestOne is not None:
|
|
|
207 |
toUpdate.remove(bestOne['_id'])
|
|
|
208 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
|
|
|
209 |
if prepaidBestOne is not None:
|
| 16074 |
kshitij.so |
210 |
if bestOne is not None:
|
|
|
211 |
if prepaidBestOne['available_price'] < bestOne['available_price']:
|
|
|
212 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
213 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
|
|
214 |
else:
|
|
|
215 |
toUpdate.remove(prepaidBestOne['_id'])
|
|
|
216 |
get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 13915 |
kshitij.so |
217 |
if len(toUpdate) > 0:
|
| 16019 |
kshitij.so |
218 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
|
| 13915 |
kshitij.so |
219 |
|
| 13828 |
kshitij.so |
220 |
def main():
|
| 14157 |
kshitij.so |
221 |
populate()
|
| 13828 |
kshitij.so |
222 |
|
|
|
223 |
if __name__=='__main__':
|
|
|
224 |
main()
|