| 13829 |
kshitij.so |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
| 15253 |
kshitij.so |
4 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
| 13918 |
kshitij.so |
5 |
from datetime import datetime, timedelta
|
| 13829 |
kshitij.so |
6 |
from operator import itemgetter
|
|
|
7 |
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
|
| 14309 |
kshitij.so |
8 |
from dtr.utils import AmazonDealScraper
|
| 14121 |
kshitij.so |
9 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper
|
| 14324 |
kshitij.so |
10 |
from dtr.storage.MemCache import MemCache
|
| 14334 |
kshitij.so |
11 |
from functools import partial
|
|
|
12 |
import threading
|
| 14760 |
kshitij.so |
13 |
from dtr.utils.utils import getCashBack
|
| 14794 |
kshitij.so |
14 |
import traceback
|
| 15174 |
kshitij.so |
15 |
from shop2020.config.client.ConfigClient import ConfigClient
|
| 13829 |
kshitij.so |
16 |
|
| 15174 |
kshitij.so |
17 |
config_client = ConfigClient()
|
|
|
18 |
host_memCache = config_client.get_property('mem_cache_host_dtr')
|
|
|
19 |
host = config_client.get_property('mongo_dtr_host')
|
| 14324 |
kshitij.so |
20 |
|
| 15174 |
kshitij.so |
21 |
mc = MemCache(host_memCache)
|
|
|
22 |
|
|
|
23 |
|
| 13829 |
kshitij.so |
24 |
con = None
|
| 14205 |
kshitij.so |
25 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4}
|
| 13829 |
kshitij.so |
26 |
|
|
|
27 |
headers = {
|
|
|
28 |
'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
|
|
|
29 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
|
30 |
'Accept-Language' : 'en-US,en;q=0.8',
|
|
|
31 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
|
|
32 |
}
|
|
|
33 |
|
| 15608 |
kshitij.so |
34 |
ignoreItems = [4490,4491]
|
|
|
35 |
|
| 15174 |
kshitij.so |
36 |
def get_mongo_connection(port=27017):
|
| 13829 |
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 |
|
| 14828 |
kshitij.so |
47 |
def returnLatestPrice(data, source_id, ignoreLastUpdated = True):
|
| 13918 |
kshitij.so |
48 |
now = datetime.now()
|
| 13829 |
kshitij.so |
49 |
if source_id == 1:
|
|
|
50 |
try:
|
| 13918 |
kshitij.so |
51 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13829 |
kshitij.so |
52 |
return {}
|
| 13918 |
kshitij.so |
53 |
|
| 14577 |
kshitij.so |
54 |
if data['dealFlag'] ==1 and data['dealType'] ==1:
|
|
|
55 |
data['marketPlaceUrl'] = "http://www.amazon.in/dp/%s"%(data['identifier'].strip())
|
|
|
56 |
|
| 13918 |
kshitij.so |
57 |
try:
|
| 14828 |
kshitij.so |
58 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13918 |
kshitij.so |
59 |
print "sku id is already updated",data['_id']
|
| 13920 |
kshitij.so |
60 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13918 |
kshitij.so |
61 |
except:
|
|
|
62 |
pass
|
|
|
63 |
|
|
|
64 |
|
| 13829 |
kshitij.so |
65 |
url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
|
|
|
66 |
lowestPrice = 0.0
|
| 14309 |
kshitij.so |
67 |
try:
|
|
|
68 |
if data['dealFlag'] ==1 and data['dealType'] ==1:
|
|
|
69 |
print "Inside deal"
|
|
|
70 |
deal_url = "http://www.amazon.in/dp/%s"%(data['identifier'].strip())
|
|
|
71 |
print deal_url
|
| 15213 |
kshitij.so |
72 |
dealScraperAmazon = AmazonDealScraper.AmazonScraper(True)
|
| 14309 |
kshitij.so |
73 |
lowestPrice = dealScraperAmazon.read(deal_url)
|
|
|
74 |
print lowestPrice
|
|
|
75 |
if lowestPrice == 0:
|
|
|
76 |
raise
|
|
|
77 |
else:
|
| 15213 |
kshitij.so |
78 |
scraperAmazon = AmazonScraper(True)
|
| 14309 |
kshitij.so |
79 |
lowestPrice = scraperAmazon.read(url)
|
|
|
80 |
except Exception as e:
|
|
|
81 |
print e
|
| 15213 |
kshitij.so |
82 |
scraperAmazon = AmazonScraper(True)
|
| 14309 |
kshitij.so |
83 |
lowestPrice = scraperAmazon.read(url)
|
| 13929 |
kshitij.so |
84 |
print "LowestPrice ",lowestPrice
|
| 13829 |
kshitij.so |
85 |
inStock = 0
|
|
|
86 |
if lowestPrice > 0:
|
|
|
87 |
inStock = 1
|
|
|
88 |
if lowestPrice > 0:
|
| 13971 |
kshitij.so |
89 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| 14309 |
kshitij.so |
90 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':inStock,'dealType':data['dealType']}}, multi=True)
|
| 13829 |
kshitij.so |
91 |
else:
|
| 13929 |
kshitij.so |
92 |
lowestPrice = data['available_price']
|
| 13977 |
kshitij.so |
93 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0,'priceUpdatedOn':to_java_date(now)}}, multi=True)
|
| 14309 |
kshitij.so |
94 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0,'dealType':data['dealType']}}, multi=True)
|
| 13918 |
kshitij.so |
95 |
|
| 13829 |
kshitij.so |
96 |
return {'_id':data['_id'],'available_price':lowestPrice,'in_stock':inStock,'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 14834 |
kshitij.so |
97 |
except Exception as e:
|
|
|
98 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
99 |
print e
|
| 13920 |
kshitij.so |
100 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13829 |
kshitij.so |
101 |
|
| 14203 |
kshitij.so |
102 |
elif source_id ==4:
|
| 14207 |
kshitij.so |
103 |
|
| 14203 |
kshitij.so |
104 |
try:
|
| 14212 |
kshitij.so |
105 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
|
|
106 |
return {}
|
|
|
107 |
|
|
|
108 |
try:
|
| 14828 |
kshitij.so |
109 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 14212 |
kshitij.so |
110 |
print "sku id is already updated",data['_id']
|
|
|
111 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':4,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
|
|
112 |
except:
|
|
|
113 |
pass
|
|
|
114 |
|
| 14840 |
kshitij.so |
115 |
url = "http://50.116.3.101:8080/mobileapi/dtr-pricing?id=%s"%(data['identifier'])
|
| 14212 |
kshitij.so |
116 |
lowestPrice = 0.0
|
|
|
117 |
instock = 0
|
|
|
118 |
req = urllib2.Request(url,headers=headers)
|
|
|
119 |
response = urllib2.urlopen(req)
|
|
|
120 |
json_input = response.read()
|
|
|
121 |
response.close()
|
|
|
122 |
priceInfo = json.loads(json_input)
|
|
|
123 |
lowestPrice = priceInfo['response']['sellingPrice']
|
|
|
124 |
if lowestPrice > 0:
|
|
|
125 |
instock = 1
|
|
|
126 |
if instock == 1:
|
|
|
127 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
|
|
|
128 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':instock}}, multi=True)
|
|
|
129 |
else:
|
|
|
130 |
lowestPrice = data['available_price']
|
|
|
131 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock,'priceUpdatedOn':to_java_date(now)}}, multi=True)
|
|
|
132 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
|
|
|
133 |
|
|
|
134 |
return {'_id':data['_id'],'available_price':lowestPrice,'in_stock':instock,'source_id':4,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 14834 |
kshitij.so |
135 |
|
|
|
136 |
except Exception as e:
|
|
|
137 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
138 |
print e
|
| 14212 |
kshitij.so |
139 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':4,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 14203 |
kshitij.so |
140 |
|
| 14207 |
kshitij.so |
141 |
|
| 13829 |
kshitij.so |
142 |
elif source_id ==3:
|
|
|
143 |
try:
|
| 13918 |
kshitij.so |
144 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13829 |
kshitij.so |
145 |
return {}
|
| 13918 |
kshitij.so |
146 |
|
|
|
147 |
try:
|
| 14828 |
kshitij.so |
148 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13920 |
kshitij.so |
149 |
print "sku id is already updated",data['_id']
|
|
|
150 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13919 |
kshitij.so |
151 |
|
|
|
152 |
except Exception as e:
|
| 13920 |
kshitij.so |
153 |
print "Exception snapdeal"
|
| 13919 |
kshitij.so |
154 |
print e
|
| 13918 |
kshitij.so |
155 |
|
| 15253 |
kshitij.so |
156 |
url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175"%(data['identifier'])
|
| 13829 |
kshitij.so |
157 |
req = urllib2.Request(url,headers=headers)
|
|
|
158 |
response = urllib2.urlopen(req)
|
| 14210 |
kshitij.so |
159 |
json_input = response.read()
|
| 14188 |
kshitij.so |
160 |
response.close()
|
| 13829 |
kshitij.so |
161 |
vendorInfo = json.loads(json_input)
|
|
|
162 |
lowestOfferPrice = 0
|
|
|
163 |
inStock = 0
|
| 15253 |
kshitij.so |
164 |
buyBoxPrice = 0
|
|
|
165 |
isBuyBox = 1
|
| 13829 |
kshitij.so |
166 |
for vendor in vendorInfo:
|
| 15253 |
kshitij.so |
167 |
buyBoxPrice = float(vendor['sellingPrice'])
|
|
|
168 |
try:
|
|
|
169 |
buyBoxStock = vendor['buyableInventory']
|
|
|
170 |
except:
|
|
|
171 |
buyBoxStock = 0
|
|
|
172 |
if buyBoxStock > 0 and buyBoxPrice > 0:
|
|
|
173 |
break
|
|
|
174 |
|
|
|
175 |
sortedVendorsData = sorted(vendorInfo, key=itemgetter('sellingPrice'))
|
|
|
176 |
for sortedVendorData in sortedVendorsData:
|
|
|
177 |
lowestOfferPrice = float(sortedVendorData['sellingPrice'])
|
|
|
178 |
try:
|
|
|
179 |
stock = sortedVendorData['buyableInventory']
|
|
|
180 |
except:
|
|
|
181 |
stock = 0
|
| 13829 |
kshitij.so |
182 |
if stock > 0 and lowestOfferPrice > 0:
|
|
|
183 |
inStock = 1
|
|
|
184 |
break
|
|
|
185 |
|
|
|
186 |
print lowestOfferPrice
|
|
|
187 |
print inStock
|
|
|
188 |
print "*************"
|
| 15253 |
kshitij.so |
189 |
|
|
|
190 |
if buyBoxPrice != lowestOfferPrice:
|
|
|
191 |
isBuyBox = 0
|
|
|
192 |
|
| 13829 |
kshitij.so |
193 |
if inStock == 1:
|
| 15253 |
kshitij.so |
194 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':inStock,'buyBoxFlag':isBuyBox}}, multi=True)
|
| 13918 |
kshitij.so |
195 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)
|
| 13829 |
kshitij.so |
196 |
else:
|
| 13929 |
kshitij.so |
197 |
lowestOfferPrice = data['available_price']
|
| 15253 |
kshitij.so |
198 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock,'priceUpdatedOn':to_java_date(now),'buyBoxFlag':isBuyBox}}, multi=True)
|
| 13918 |
kshitij.so |
199 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
|
|
200 |
|
| 13829 |
kshitij.so |
201 |
return {'_id':data['_id'],'available_price':lowestOfferPrice,'in_stock':inStock,'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 14834 |
kshitij.so |
202 |
except Exception as e:
|
| 15266 |
kshitij.so |
203 |
print traceback.print_exc()
|
| 14834 |
kshitij.so |
204 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
205 |
print e
|
| 13920 |
kshitij.so |
206 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13829 |
kshitij.so |
207 |
|
|
|
208 |
elif source_id == 2:
|
|
|
209 |
try:
|
| 13918 |
kshitij.so |
210 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13829 |
kshitij.so |
211 |
return {}
|
| 13918 |
kshitij.so |
212 |
|
| 15608 |
kshitij.so |
213 |
if data['_id'] in ignoreLastUpdated:
|
|
|
214 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
215 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
|
|
216 |
|
| 13918 |
kshitij.so |
217 |
try:
|
| 14828 |
kshitij.so |
218 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13918 |
kshitij.so |
219 |
print "sku id is already updated",data['_id']
|
| 13920 |
kshitij.so |
220 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13918 |
kshitij.so |
221 |
except:
|
|
|
222 |
pass
|
|
|
223 |
|
|
|
224 |
lowestSp = 0
|
|
|
225 |
inStock = 0
|
| 15253 |
kshitij.so |
226 |
buyBoxPrice = 0
|
|
|
227 |
isBuyBox = 0
|
| 14189 |
kshitij.so |
228 |
scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
|
| 14121 |
kshitij.so |
229 |
try:
|
|
|
230 |
if data['marketPlaceUrl']!="" or data['marketPlaceUrl'] !="http://www.flipkart.com/ps/%s"%(data['identifier']):
|
|
|
231 |
result = scraperProductPage.read(data['marketPlaceUrl'])
|
| 14124 |
kshitij.so |
232 |
print result
|
| 14125 |
kshitij.so |
233 |
if result.get('lowestSp')!=0:
|
|
|
234 |
lowestSp = result.get('lowestSp')
|
| 14121 |
kshitij.so |
235 |
inStock = result.get('inStock')
|
| 15253 |
kshitij.so |
236 |
buyBoxPrice = result.get('buyBoxPrice')
|
| 14121 |
kshitij.so |
237 |
except:
|
|
|
238 |
print "Unable to scrape product page ",data['identifier']
|
|
|
239 |
if lowestSp ==0:
|
|
|
240 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
|
| 14189 |
kshitij.so |
241 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
| 15253 |
kshitij.so |
242 |
vendorsData, buyBoxInfo = (scraperFk.read(url))
|
| 14121 |
kshitij.so |
243 |
sortedVendorsData = []
|
|
|
244 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
|
|
245 |
print "data",sortedVendorsData
|
|
|
246 |
for vData in sortedVendorsData:
|
|
|
247 |
lowestSp = vData['sellingPrice']
|
|
|
248 |
break
|
|
|
249 |
if lowestSp > 0:
|
|
|
250 |
inStock = 1
|
| 15253 |
kshitij.so |
251 |
buyBoxPrice = buyBoxInfo[0].get('sellingPrice')
|
| 13829 |
kshitij.so |
252 |
print lowestSp
|
|
|
253 |
print inStock
|
| 15253 |
kshitij.so |
254 |
if buyBoxPrice is not None and buyBoxPrice == lowestSp:
|
|
|
255 |
isBuyBox = 1
|
| 13829 |
kshitij.so |
256 |
if lowestSp > 0:
|
| 15253 |
kshitij.so |
257 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':inStock,'buyBoxFlag':isBuyBox}}, multi=True)
|
| 13918 |
kshitij.so |
258 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
|
| 13829 |
kshitij.so |
259 |
else:
|
| 13929 |
kshitij.so |
260 |
lowestSp = data['available_price']
|
| 15253 |
kshitij.so |
261 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock,'priceUpdatedOn':to_java_date(now),'buyBoxFlag':isBuyBox}}, multi=True)
|
| 13918 |
kshitij.so |
262 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
|
|
263 |
|
| 13829 |
kshitij.so |
264 |
return {'_id':data['_id'],'available_price':lowestSp,'in_stock':inStock,'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 14342 |
kshitij.so |
265 |
|
| 14834 |
kshitij.so |
266 |
except Exception as e:
|
|
|
267 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
268 |
print e
|
| 13920 |
kshitij.so |
269 |
return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['in_stock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
|
| 13829 |
kshitij.so |
270 |
else:
|
|
|
271 |
return {}
|
| 13918 |
kshitij.so |
272 |
|
| 15253 |
kshitij.so |
273 |
def recomputePoints(item, deal):
|
|
|
274 |
try:
|
|
|
275 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
|
|
276 |
except:
|
|
|
277 |
print traceback.print_exc()
|
|
|
278 |
nlcPoints = deal['nlcPoints']
|
|
|
279 |
if item['manualDealThresholdPrice'] >= deal['available_price']:
|
|
|
280 |
dealPoints = item['dealPoints']
|
|
|
281 |
else:
|
|
|
282 |
dealPoints = 0
|
|
|
283 |
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']}})
|
|
|
284 |
|
| 15266 |
kshitij.so |
285 |
|
|
|
286 |
def populateNegativeDeals():
|
|
|
287 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
288 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
289 |
|
|
|
290 |
def recomputeDeal(item):
|
| 13918 |
kshitij.so |
291 |
"""Lets recompute deal for this bundle"""
|
| 15266 |
kshitij.so |
292 |
print "Recomputing for bundleId",item.get('skuBundleId')
|
|
|
293 |
skuBundleId = item['skuBundleId']
|
| 13829 |
kshitij.so |
294 |
|
| 13918 |
kshitij.so |
295 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
296 |
bestPrice = float("inf")
|
|
|
297 |
bestOne = None
|
|
|
298 |
bestSellerPoints = 0
|
|
|
299 |
toUpdate = []
|
|
|
300 |
for similarItem in similarItems:
|
| 15266 |
kshitij.so |
301 |
if similarItem['_id'] == item['_id']:
|
|
|
302 |
recomputePoints(item, similarItem)
|
| 14332 |
kshitij.so |
303 |
if mc.get("negative_deals") is None:
|
| 14324 |
kshitij.so |
304 |
populateNegativeDeals()
|
| 14332 |
kshitij.so |
305 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 13918 |
kshitij.so |
306 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
307 |
continue
|
|
|
308 |
if similarItem['available_price'] < bestPrice:
|
|
|
309 |
bestOne = similarItem
|
|
|
310 |
bestPrice = similarItem['available_price']
|
|
|
311 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
312 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
313 |
bestOne = similarItem
|
|
|
314 |
bestPrice = similarItem['available_price']
|
|
|
315 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
316 |
else:
|
|
|
317 |
pass
|
|
|
318 |
if bestOne is not None:
|
|
|
319 |
for similarItem in similarItems:
|
|
|
320 |
toUpdate.append(similarItem['_id'])
|
|
|
321 |
toUpdate.remove(bestOne['_id'])
|
|
|
322 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
323 |
if len(toUpdate) > 0:
|
|
|
324 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
| 14342 |
kshitij.so |
325 |
|
|
|
326 |
print "Done with recomputing"
|
| 13829 |
kshitij.so |
327 |
|
|
|
328 |
def getLatestPrice(skuBundleId, source_id):
|
|
|
329 |
temp = []
|
|
|
330 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
|
|
331 |
for item in itemIds:
|
| 14309 |
kshitij.so |
332 |
item['dealFlag'] = 0
|
|
|
333 |
item['dealType'] = 0
|
| 15266 |
kshitij.so |
334 |
item['dealPoints'] = 0
|
|
|
335 |
item['manualDealThresholdPrice'] = None
|
| 15187 |
kshitij.so |
336 |
if item['source_id'] ==3:
|
|
|
337 |
item['marketPlaceUrl'] = item['marketPlaceUrl']+'?supc='+item.get('identifier')
|
| 14309 |
kshitij.so |
338 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':source_id, 'sku':item['_id']}))
|
|
|
339 |
if len(manualDeals) > 0:
|
|
|
340 |
item['dealFlag'] = 1
|
| 14760 |
kshitij.so |
341 |
item['dealType'] =manualDeals[0]['dealType']
|
| 15266 |
kshitij.so |
342 |
item['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
343 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14760 |
kshitij.so |
344 |
info = returnLatestPrice(item, source_id)
|
| 14794 |
kshitij.so |
345 |
print info
|
| 14760 |
kshitij.so |
346 |
try:
|
|
|
347 |
cashBack = getCashBack(item['_id'], item['source_id'], item['category_id'], mc, 'localhost')
|
| 14794 |
kshitij.so |
348 |
print "CashBack is ",cashBack
|
| 14760 |
kshitij.so |
349 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 14801 |
kshitij.so |
350 |
info['cash_back_type'] = 0
|
|
|
351 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
352 |
else:
|
|
|
353 |
if cashBack['cash_back_type'] in (1,2):
|
|
|
354 |
info['cash_back_type'] = cashBack['cash_back_type']
|
|
|
355 |
info['cash_back'] = float(cashBack['cash_back'])
|
|
|
356 |
else:
|
| 14801 |
kshitij.so |
357 |
info['cash_back_type'] = 0
|
|
|
358 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
359 |
except Exception as cashBackEx:
|
| 14794 |
kshitij.so |
360 |
traceback.print_exc()
|
| 14760 |
kshitij.so |
361 |
print "Error calculating cashback."
|
|
|
362 |
info['cash_back_type'] = 0
|
|
|
363 |
info['cash_back'] = 0
|
| 14794 |
kshitij.so |
364 |
print "info is ",info
|
| 14760 |
kshitij.so |
365 |
temp.append(info)
|
| 14367 |
kshitij.so |
366 |
try:
|
| 15266 |
kshitij.so |
367 |
thread = threading.Thread(target=recomputeDeal, args = (item,))
|
| 14367 |
kshitij.so |
368 |
thread.daemon = True
|
|
|
369 |
thread.start()
|
|
|
370 |
except:
|
| 15273 |
kshitij.so |
371 |
print traceback.print_exc()
|
| 14367 |
kshitij.so |
372 |
print "Unable to compute deal for ",skuBundleId
|
| 13829 |
kshitij.so |
373 |
return temp
|
|
|
374 |
|
| 13864 |
kshitij.so |
375 |
def getLatestPriceById(id):
|
|
|
376 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))
|
| 14309 |
kshitij.so |
377 |
item[0]['dealFlag'] = 0
|
|
|
378 |
item[0]['dealType'] = 0
|
| 15266 |
kshitij.so |
379 |
item[0]['dealPoints'] = 0
|
|
|
380 |
item[0]['manualDealThresholdPrice'] = None
|
| 14309 |
kshitij.so |
381 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':item[0]['source_id'], 'sku':item[0]['_id']}))
|
|
|
382 |
if len(manualDeals) > 0:
|
|
|
383 |
item[0]['dealFlag'] = 1
|
| 15266 |
kshitij.so |
384 |
item[0]['dealType'] =manualDeals[0]['dealType']
|
|
|
385 |
item[0]['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
386 |
item[0]['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14367 |
kshitij.so |
387 |
|
| 14431 |
kshitij.so |
388 |
info = returnLatestPrice(item[0], item[0]['source_id'])
|
| 14794 |
kshitij.so |
389 |
print info
|
| 14367 |
kshitij.so |
390 |
try:
|
| 14794 |
kshitij.so |
391 |
cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'], mc, 'localhost')
|
|
|
392 |
print "CashBack is ",cashBack
|
| 14760 |
kshitij.so |
393 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 14801 |
kshitij.so |
394 |
info['cash_back_type'] = 0
|
|
|
395 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
396 |
else:
|
|
|
397 |
if cashBack['cash_back_type'] in (1,2):
|
|
|
398 |
info['cash_back_type'] = cashBack['cash_back_type']
|
|
|
399 |
info['cash_back'] = float(cashBack['cash_back'])
|
|
|
400 |
else:
|
| 14801 |
kshitij.so |
401 |
info['cash_back_type'] = 0
|
|
|
402 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
403 |
except Exception as cashBackEx:
|
| 14794 |
kshitij.so |
404 |
traceback.print_exc()
|
| 14760 |
kshitij.so |
405 |
print cashBackEx
|
|
|
406 |
print "Error calculating cashback."
|
|
|
407 |
info['cash_back_type'] = 0
|
|
|
408 |
info['cash_back'] = 0
|
|
|
409 |
try:
|
| 15273 |
kshitij.so |
410 |
thread = threading.Thread(target=recomputeDeal, args = (item[0],))
|
| 14367 |
kshitij.so |
411 |
thread.daemon = True
|
|
|
412 |
thread.start()
|
|
|
413 |
except:
|
|
|
414 |
print "Unable to compute deal for ",item[0]['skuBundleId']
|
| 14431 |
kshitij.so |
415 |
return info
|
| 13829 |
kshitij.so |
416 |
|
| 14828 |
kshitij.so |
417 |
def updatePriceForNotificationBundles(skuBundleId):
|
| 15266 |
kshitij.so |
418 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'priceUpdatedOn':{'$lte':to_java_date(datetime.now() - timedelta(minutes=2))},'source_id':{"$in":SOURCE_MAP.values()}}))
|
| 14828 |
kshitij.so |
419 |
for item in itemIds:
|
| 14833 |
kshitij.so |
420 |
print item['_id']
|
| 14828 |
kshitij.so |
421 |
item['dealFlag'] = 0
|
|
|
422 |
item['dealType'] = 0
|
| 15266 |
kshitij.so |
423 |
item['dealPoints'] = 0
|
|
|
424 |
item['manualDealThresholdPrice'] = None
|
| 14828 |
kshitij.so |
425 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':item['source_id'], 'sku':item['_id']}))
|
|
|
426 |
if len(manualDeals) > 0:
|
|
|
427 |
item['dealFlag'] = 1
|
|
|
428 |
item['dealType'] =manualDeals[0]['dealType']
|
| 15266 |
kshitij.so |
429 |
item['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
430 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14828 |
kshitij.so |
431 |
info = returnLatestPrice(item, item['source_id'],False)
|
|
|
432 |
print info
|
|
|
433 |
|
| 13829 |
kshitij.so |
434 |
def main():
|
| 15266 |
kshitij.so |
435 |
print "retuned %s"%(str(getLatestPriceById(23151)))
|
| 13829 |
kshitij.so |
436 |
|
|
|
437 |
if __name__=='__main__':
|
| 13971 |
kshitij.so |
438 |
main()
|
|
|
439 |
|