| 17076 |
manish.sha |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
|
|
4 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
|
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
import time
|
|
|
7 |
from multiprocessing import Pool as ThreadPool
|
|
|
8 |
from multiprocessing import cpu_count
|
|
|
9 |
import optparse
|
|
|
10 |
from dtr.storage.MemCache import MemCache
|
|
|
11 |
from dtr.utils.utils import getCashBack, get_mongo_connection, SOURCE_MAP
|
|
|
12 |
import traceback
|
|
|
13 |
from operator import itemgetter
|
|
|
14 |
import chardet
|
|
|
15 |
from dtr.utils import HomeShop18Scraper
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
con = None
|
|
|
19 |
|
|
|
20 |
parser = optparse.OptionParser()
|
|
|
21 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
22 |
default="localhost",
|
|
|
23 |
type="string", help="The HOST where the mongo server is running",
|
|
|
24 |
metavar="mongo_host")
|
|
|
25 |
|
|
|
26 |
(options, args) = parser.parse_args()
|
|
|
27 |
|
|
|
28 |
mc = MemCache(options.mongoHost)
|
|
|
29 |
|
|
|
30 |
def populate():
|
|
|
31 |
toScrapMap = {}
|
| 17107 |
manish.sha |
32 |
bestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'rank':{'$gt':0}}))
|
| 17076 |
manish.sha |
33 |
for bestSeller in bestSellers:
|
| 17107 |
manish.sha |
34 |
snapdealBestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':7}))
|
| 17076 |
manish.sha |
35 |
for data in snapdealBestSellers:
|
|
|
36 |
if not toScrapMap.has_key(data['_id']):
|
|
|
37 |
data['dealFlag'] = 0
|
|
|
38 |
data['dealType'] = 0
|
|
|
39 |
toScrapMap[data['_id']] = data
|
| 17107 |
manish.sha |
40 |
dealFlagged = list(get_mongo_connection(host=options.mongoHost).Catalog.Deals.find({'source_id':7,'showDeal':1,'totalPoints':{'$gt':-100}}))
|
| 17076 |
manish.sha |
41 |
for deal in dealFlagged:
|
|
|
42 |
if not toScrapMap.has_key(deal['_id']):
|
| 17107 |
manish.sha |
43 |
data = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'_id':deal['_id']}))
|
| 17076 |
manish.sha |
44 |
data[0]['dealFlag'] = 0
|
|
|
45 |
data[0]['dealType'] = 0
|
|
|
46 |
toScrapMap[deal['_id']] = data[0]
|
| 17107 |
manish.sha |
47 |
manualDeals = list(get_mongo_connection(host=options.mongoHost).Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':7}))
|
| 17076 |
manish.sha |
48 |
for manualDeal in manualDeals:
|
|
|
49 |
if not toScrapMap.has_key(manualDeal['sku']):
|
| 17107 |
manish.sha |
50 |
data = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'_id':manualDeal['sku']}))
|
| 17076 |
manish.sha |
51 |
if len(data) > 0:
|
|
|
52 |
data[0]['dealFlag'] = 1
|
|
|
53 |
data[0]['dealType'] = manualDeal['dealType']
|
|
|
54 |
toScrapMap[manualDeal['sku']] = data[0]
|
|
|
55 |
else:
|
|
|
56 |
data = toScrapMap.get(manualDeal['sku'])
|
|
|
57 |
data['dealFlag'] = 1
|
|
|
58 |
data['dealType'] = manualDeal['dealType']
|
|
|
59 |
|
|
|
60 |
for val in toScrapMap.values():
|
|
|
61 |
updatePrices(val)
|
|
|
62 |
|
|
|
63 |
def updatePrices(data):
|
|
|
64 |
if data.get('ignorePricing') ==1:
|
|
|
65 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
66 |
return
|
|
|
67 |
if data['source_id']!=7:
|
|
|
68 |
return
|
|
|
69 |
print data['identifier']
|
|
|
70 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
|
|
71 |
print "returning"
|
|
|
72 |
return
|
|
|
73 |
|
|
|
74 |
try:
|
|
|
75 |
if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
|
|
|
76 |
print "sku id is already updated",data['_id']
|
|
|
77 |
return
|
|
|
78 |
except:
|
|
|
79 |
pass
|
|
|
80 |
|
|
|
81 |
result = None
|
|
|
82 |
try:
|
|
|
83 |
url = 'http://m.homeshop18.com/product.mobi?productId='+str(data['identifier'])
|
|
|
84 |
scraper = HomeShop18Scraper.HomeShop18Scraper()
|
|
|
85 |
result = scraper.read(url)
|
|
|
86 |
except:
|
|
|
87 |
print "Unable to scrape %d"%(data['_id'])
|
| 17107 |
manish.sha |
88 |
get_mongo_connection(host=options.mongoHost).Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':0,'priceUpdatedOn':to_java_date(datetime.now())}})
|
|
|
89 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0}})
|
| 17076 |
manish.sha |
90 |
return
|
|
|
91 |
|
|
|
92 |
inStock = 0
|
|
|
93 |
|
|
|
94 |
lowestOfferPrice = 0
|
|
|
95 |
if result is not None:
|
|
|
96 |
lowestOfferPrice = float(result['price']+result['shippingCharge'])
|
|
|
97 |
inStock = result['inStock']
|
|
|
98 |
|
|
|
99 |
print lowestOfferPrice
|
|
|
100 |
print inStock
|
|
|
101 |
print "*************"
|
|
|
102 |
|
|
|
103 |
if lowestOfferPrice ==0:
|
|
|
104 |
inStock = 0
|
|
|
105 |
|
|
|
106 |
if inStock == 1:
|
| 17107 |
manish.sha |
107 |
get_mongo_connection(host=options.mongoHost).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)
|
|
|
108 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
|
| 17076 |
manish.sha |
109 |
else:
|
|
|
110 |
lowestOfferPrice = data['available_price']
|
| 17107 |
manish.sha |
111 |
get_mongo_connection(host=options.mongoHost).Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':inStock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
|
|
|
112 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
|
| 17076 |
manish.sha |
113 |
|
|
|
114 |
try:
|
|
|
115 |
recomputeDeal(data)
|
|
|
116 |
except:
|
|
|
117 |
print "Unable to compute deal for ",data['skuBundleId']
|
|
|
118 |
|
|
|
119 |
def populateNegativeDeals():
|
| 17107 |
manish.sha |
120 |
negativeDeals = get_mongo_connection(host=options.mongoHost).Catalog.NegativeDeals.find().distinct('sku')
|
| 17076 |
manish.sha |
121 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
122 |
|
|
|
123 |
def recomputeDeal(item):
|
|
|
124 |
"""Lets recompute deal for this bundle"""
|
|
|
125 |
print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
|
|
|
126 |
skuBundleId = item['skuBundleId']
|
|
|
127 |
|
| 17107 |
manish.sha |
128 |
similarItems = list(get_mongo_connection(host=options.mongoHost).Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
| 17076 |
manish.sha |
129 |
bestPrice = float("inf")
|
|
|
130 |
bestOne = None
|
|
|
131 |
bestSellerPoints = 0
|
|
|
132 |
toUpdate = []
|
|
|
133 |
prepaidBestPrice = float("inf")
|
|
|
134 |
prepaidBestOne = None
|
|
|
135 |
prepaidBestSellerPoints = 0
|
|
|
136 |
for similarItem in similarItems:
|
|
|
137 |
if similarItem['codAvailable'] ==1:
|
|
|
138 |
if mc.get("negative_deals") is None:
|
|
|
139 |
populateNegativeDeals()
|
|
|
140 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 17107 |
manish.sha |
141 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
| 17076 |
manish.sha |
142 |
continue
|
|
|
143 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
| 17107 |
manish.sha |
144 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
| 17076 |
manish.sha |
145 |
continue
|
|
|
146 |
if similarItem['available_price'] < bestPrice:
|
|
|
147 |
bestOne = similarItem
|
|
|
148 |
bestPrice = similarItem['available_price']
|
|
|
149 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
150 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
151 |
bestOne = similarItem
|
|
|
152 |
bestPrice = similarItem['available_price']
|
|
|
153 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
154 |
else:
|
|
|
155 |
pass
|
|
|
156 |
else:
|
|
|
157 |
if mc.get("negative_deals") is None:
|
|
|
158 |
populateNegativeDeals()
|
|
|
159 |
if similarItem['in_stock'] == 0 or similarItem['_id'] in mc.get("negative_deals"):
|
| 17107 |
manish.sha |
160 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
|
| 17076 |
manish.sha |
161 |
continue
|
|
|
162 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
| 17107 |
manish.sha |
163 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
|
| 17076 |
manish.sha |
164 |
continue
|
|
|
165 |
if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
|
|
|
166 |
similarItem['available_price'] = similarItem['gross_price']
|
|
|
167 |
if similarItem['available_price'] < prepaidBestPrice:
|
|
|
168 |
prepaidBestOne = similarItem
|
|
|
169 |
prepaidBestPrice = similarItem['available_price']
|
|
|
170 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
171 |
elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
172 |
prepaidBestOne = similarItem
|
|
|
173 |
prepaidBestPrice = similarItem['available_price']
|
|
|
174 |
prepaidBestSellerPoints = similarItem['bestSellerPoints']
|
|
|
175 |
else:
|
|
|
176 |
pass
|
|
|
177 |
if bestOne is not None or prepaidBestOne is not None:
|
|
|
178 |
for similarItem in similarItems:
|
|
|
179 |
toUpdate.append(similarItem['_id'])
|
|
|
180 |
if bestOne is not None:
|
|
|
181 |
toUpdate.remove(bestOne['_id'])
|
| 17107 |
manish.sha |
182 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
|
| 17076 |
manish.sha |
183 |
if prepaidBestOne is not None:
|
|
|
184 |
if bestOne is not None:
|
|
|
185 |
if prepaidBestOne['available_price'] < bestOne['available_price']:
|
|
|
186 |
toUpdate.remove(prepaidBestOne['_id'])
|
| 17107 |
manish.sha |
187 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 17076 |
manish.sha |
188 |
else:
|
|
|
189 |
toUpdate.remove(prepaidBestOne['_id'])
|
| 17107 |
manish.sha |
190 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
|
| 17076 |
manish.sha |
191 |
if len(toUpdate) > 0:
|
| 17107 |
manish.sha |
192 |
get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
|
| 17076 |
manish.sha |
193 |
|
|
|
194 |
|
|
|
195 |
def main():
|
|
|
196 |
populate()
|
|
|
197 |
|
|
|
198 |
if __name__=='__main__':
|
|
|
199 |
main()
|
|
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
|