| 13829 |
kshitij.so |
1 |
import urllib2
|
|
|
2 |
import simplejson as json
|
|
|
3 |
import pymongo
|
| 15902 |
kshitij.so |
4 |
from dtr.utils.utils import to_java_date, getNlcPoints, changeToMobileUrl
|
| 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
|
| 15902 |
kshitij.so |
9 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper, ShopCluesScraper
|
| 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 |
|
| 15902 |
kshitij.so |
24 |
con = None
|
|
|
25 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5}
|
| 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 |
|
| 15902 |
kshitij.so |
34 |
ignoreItems = []
|
| 15608 |
kshitij.so |
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 |
|
| 15615 |
kshitij.so |
54 |
if data['_id'] in ignoreItems:
|
|
|
55 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
56 |
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']}
|
|
|
57 |
|
|
|
58 |
|
| 14577 |
kshitij.so |
59 |
if data['dealFlag'] ==1 and data['dealType'] ==1:
|
|
|
60 |
data['marketPlaceUrl'] = "http://www.amazon.in/dp/%s"%(data['identifier'].strip())
|
|
|
61 |
|
| 13918 |
kshitij.so |
62 |
try:
|
| 14828 |
kshitij.so |
63 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13918 |
kshitij.so |
64 |
print "sku id is already updated",data['_id']
|
| 13920 |
kshitij.so |
65 |
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 |
66 |
except:
|
|
|
67 |
pass
|
|
|
68 |
|
|
|
69 |
|
| 13829 |
kshitij.so |
70 |
url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
|
|
|
71 |
lowestPrice = 0.0
|
| 14309 |
kshitij.so |
72 |
try:
|
|
|
73 |
if data['dealFlag'] ==1 and data['dealType'] ==1:
|
|
|
74 |
print "Inside deal"
|
|
|
75 |
deal_url = "http://www.amazon.in/dp/%s"%(data['identifier'].strip())
|
|
|
76 |
print deal_url
|
| 15213 |
kshitij.so |
77 |
dealScraperAmazon = AmazonDealScraper.AmazonScraper(True)
|
| 14309 |
kshitij.so |
78 |
lowestPrice = dealScraperAmazon.read(deal_url)
|
|
|
79 |
print lowestPrice
|
|
|
80 |
if lowestPrice == 0:
|
|
|
81 |
raise
|
|
|
82 |
else:
|
| 15213 |
kshitij.so |
83 |
scraperAmazon = AmazonScraper(True)
|
| 14309 |
kshitij.so |
84 |
lowestPrice = scraperAmazon.read(url)
|
|
|
85 |
except Exception as e:
|
|
|
86 |
print e
|
| 15213 |
kshitij.so |
87 |
scraperAmazon = AmazonScraper(True)
|
| 14309 |
kshitij.so |
88 |
lowestPrice = scraperAmazon.read(url)
|
| 13929 |
kshitij.so |
89 |
print "LowestPrice ",lowestPrice
|
| 13829 |
kshitij.so |
90 |
inStock = 0
|
|
|
91 |
if lowestPrice > 0:
|
|
|
92 |
inStock = 1
|
|
|
93 |
if lowestPrice > 0:
|
| 13971 |
kshitij.so |
94 |
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 |
95 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':inStock,'dealType':data['dealType']}}, multi=True)
|
| 13829 |
kshitij.so |
96 |
else:
|
| 13929 |
kshitij.so |
97 |
lowestPrice = data['available_price']
|
| 13977 |
kshitij.so |
98 |
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 |
99 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0,'dealType':data['dealType']}}, multi=True)
|
| 13918 |
kshitij.so |
100 |
|
| 13829 |
kshitij.so |
101 |
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 |
102 |
except Exception as e:
|
|
|
103 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
104 |
print e
|
| 13920 |
kshitij.so |
105 |
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 |
106 |
|
| 14203 |
kshitij.so |
107 |
elif source_id ==4:
|
| 14207 |
kshitij.so |
108 |
|
| 14203 |
kshitij.so |
109 |
try:
|
| 14212 |
kshitij.so |
110 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
|
|
111 |
return {}
|
|
|
112 |
|
|
|
113 |
try:
|
| 14828 |
kshitij.so |
114 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 14212 |
kshitij.so |
115 |
print "sku id is already updated",data['_id']
|
|
|
116 |
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']}
|
|
|
117 |
except:
|
|
|
118 |
pass
|
|
|
119 |
|
| 14840 |
kshitij.so |
120 |
url = "http://50.116.3.101:8080/mobileapi/dtr-pricing?id=%s"%(data['identifier'])
|
| 14212 |
kshitij.so |
121 |
lowestPrice = 0.0
|
|
|
122 |
instock = 0
|
|
|
123 |
req = urllib2.Request(url,headers=headers)
|
|
|
124 |
response = urllib2.urlopen(req)
|
|
|
125 |
json_input = response.read()
|
|
|
126 |
response.close()
|
|
|
127 |
priceInfo = json.loads(json_input)
|
|
|
128 |
lowestPrice = priceInfo['response']['sellingPrice']
|
|
|
129 |
if lowestPrice > 0:
|
|
|
130 |
instock = 1
|
|
|
131 |
if instock == 1:
|
|
|
132 |
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)
|
|
|
133 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':instock}}, multi=True)
|
|
|
134 |
else:
|
|
|
135 |
lowestPrice = data['available_price']
|
|
|
136 |
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)
|
|
|
137 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
|
|
|
138 |
|
|
|
139 |
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 |
140 |
|
|
|
141 |
except Exception as e:
|
|
|
142 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
143 |
print e
|
| 14212 |
kshitij.so |
144 |
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 |
145 |
|
| 14207 |
kshitij.so |
146 |
|
| 13829 |
kshitij.so |
147 |
elif source_id ==3:
|
|
|
148 |
try:
|
| 13918 |
kshitij.so |
149 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13829 |
kshitij.so |
150 |
return {}
|
| 13918 |
kshitij.so |
151 |
|
|
|
152 |
try:
|
| 14828 |
kshitij.so |
153 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13920 |
kshitij.so |
154 |
print "sku id is already updated",data['_id']
|
|
|
155 |
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 |
156 |
|
|
|
157 |
except Exception as e:
|
| 13920 |
kshitij.so |
158 |
print "Exception snapdeal"
|
| 13919 |
kshitij.so |
159 |
print e
|
| 13918 |
kshitij.so |
160 |
|
| 15819 |
kshitij.so |
161 |
url="http://www.snapdeal.com/acors/json/v2/gvbps?supc=%s&catUrl=&bn=&catId=175&start=0&count=10000"%(data['identifier'])
|
| 13829 |
kshitij.so |
162 |
req = urllib2.Request(url,headers=headers)
|
|
|
163 |
response = urllib2.urlopen(req)
|
| 15819 |
kshitij.so |
164 |
vendorInfo = json.load(response)
|
| 14188 |
kshitij.so |
165 |
response.close()
|
| 13829 |
kshitij.so |
166 |
lowestOfferPrice = 0
|
|
|
167 |
inStock = 0
|
| 15253 |
kshitij.so |
168 |
buyBoxPrice = 0
|
|
|
169 |
isBuyBox = 1
|
| 15819 |
kshitij.so |
170 |
try:
|
|
|
171 |
buyBoxStock = vendorInfo['primaryVendor']['buyableInventory']
|
|
|
172 |
if buyBoxStock >0:
|
|
|
173 |
buyBoxPrice = vendorInfo['primaryVendor']['sellingPrice']
|
|
|
174 |
except:
|
|
|
175 |
pass
|
|
|
176 |
print buyBoxStock
|
|
|
177 |
print buyBoxPrice
|
|
|
178 |
sortedVendorsData = sorted(vendorInfo['vendors'], key=itemgetter('sellingPrice'))
|
| 15253 |
kshitij.so |
179 |
for sortedVendorData in sortedVendorsData:
|
|
|
180 |
lowestOfferPrice = float(sortedVendorData['sellingPrice'])
|
|
|
181 |
try:
|
|
|
182 |
stock = sortedVendorData['buyableInventory']
|
|
|
183 |
except:
|
|
|
184 |
stock = 0
|
| 13829 |
kshitij.so |
185 |
if stock > 0 and lowestOfferPrice > 0:
|
|
|
186 |
inStock = 1
|
|
|
187 |
break
|
|
|
188 |
|
|
|
189 |
print lowestOfferPrice
|
|
|
190 |
print inStock
|
|
|
191 |
print "*************"
|
| 15253 |
kshitij.so |
192 |
|
|
|
193 |
if buyBoxPrice != lowestOfferPrice:
|
|
|
194 |
isBuyBox = 0
|
|
|
195 |
|
| 13829 |
kshitij.so |
196 |
if inStock == 1:
|
| 15253 |
kshitij.so |
197 |
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 |
198 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)
|
| 13829 |
kshitij.so |
199 |
else:
|
| 13929 |
kshitij.so |
200 |
lowestOfferPrice = data['available_price']
|
| 15253 |
kshitij.so |
201 |
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 |
202 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
|
|
203 |
|
| 13829 |
kshitij.so |
204 |
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 |
205 |
except Exception as e:
|
| 15266 |
kshitij.so |
206 |
print traceback.print_exc()
|
| 14834 |
kshitij.so |
207 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
208 |
print e
|
| 13920 |
kshitij.so |
209 |
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 |
210 |
|
|
|
211 |
elif source_id == 2:
|
|
|
212 |
try:
|
| 13918 |
kshitij.so |
213 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13829 |
kshitij.so |
214 |
return {}
|
| 13918 |
kshitij.so |
215 |
|
| 15609 |
kshitij.so |
216 |
if data['_id'] in ignoreItems:
|
| 15608 |
kshitij.so |
217 |
print "Ignored items returning for %d"%(data['_id'])
|
| 15622 |
kshitij.so |
218 |
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']}
|
| 15608 |
kshitij.so |
219 |
|
| 13918 |
kshitij.so |
220 |
try:
|
| 14828 |
kshitij.so |
221 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13918 |
kshitij.so |
222 |
print "sku id is already updated",data['_id']
|
| 13920 |
kshitij.so |
223 |
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 |
224 |
except:
|
|
|
225 |
pass
|
|
|
226 |
|
|
|
227 |
lowestSp = 0
|
|
|
228 |
inStock = 0
|
| 15253 |
kshitij.so |
229 |
buyBoxPrice = 0
|
|
|
230 |
isBuyBox = 0
|
| 14189 |
kshitij.so |
231 |
scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
|
| 14121 |
kshitij.so |
232 |
try:
|
|
|
233 |
if data['marketPlaceUrl']!="" or data['marketPlaceUrl'] !="http://www.flipkart.com/ps/%s"%(data['identifier']):
|
|
|
234 |
result = scraperProductPage.read(data['marketPlaceUrl'])
|
| 14124 |
kshitij.so |
235 |
print result
|
| 14125 |
kshitij.so |
236 |
if result.get('lowestSp')!=0:
|
|
|
237 |
lowestSp = result.get('lowestSp')
|
| 14121 |
kshitij.so |
238 |
inStock = result.get('inStock')
|
| 15253 |
kshitij.so |
239 |
buyBoxPrice = result.get('buyBoxPrice')
|
| 14121 |
kshitij.so |
240 |
except:
|
|
|
241 |
print "Unable to scrape product page ",data['identifier']
|
|
|
242 |
if lowestSp ==0:
|
|
|
243 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
|
| 14189 |
kshitij.so |
244 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
| 15253 |
kshitij.so |
245 |
vendorsData, buyBoxInfo = (scraperFk.read(url))
|
| 14121 |
kshitij.so |
246 |
sortedVendorsData = []
|
|
|
247 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
|
|
248 |
print "data",sortedVendorsData
|
|
|
249 |
for vData in sortedVendorsData:
|
|
|
250 |
lowestSp = vData['sellingPrice']
|
|
|
251 |
break
|
|
|
252 |
if lowestSp > 0:
|
|
|
253 |
inStock = 1
|
| 15253 |
kshitij.so |
254 |
buyBoxPrice = buyBoxInfo[0].get('sellingPrice')
|
| 13829 |
kshitij.so |
255 |
print lowestSp
|
|
|
256 |
print inStock
|
| 15253 |
kshitij.so |
257 |
if buyBoxPrice is not None and buyBoxPrice == lowestSp:
|
|
|
258 |
isBuyBox = 1
|
| 13829 |
kshitij.so |
259 |
if lowestSp > 0:
|
| 15253 |
kshitij.so |
260 |
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 |
261 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
|
| 13829 |
kshitij.so |
262 |
else:
|
| 13929 |
kshitij.so |
263 |
lowestSp = data['available_price']
|
| 15253 |
kshitij.so |
264 |
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 |
265 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
|
|
266 |
|
| 13829 |
kshitij.so |
267 |
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 |
268 |
|
| 14834 |
kshitij.so |
269 |
except Exception as e:
|
|
|
270 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
271 |
print e
|
| 13920 |
kshitij.so |
272 |
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']}
|
| 15902 |
kshitij.so |
273 |
|
|
|
274 |
elif source_id == 5:
|
|
|
275 |
try:
|
|
|
276 |
if data['_id'] in ignoreItems:
|
|
|
277 |
print "Ignored items returning for %d"%(data['_id'])
|
|
|
278 |
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']}
|
|
|
279 |
|
|
|
280 |
try:
|
|
|
281 |
if ignoreLastUpdated and data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
|
|
282 |
print "sku id is already updated",data['_id']
|
|
|
283 |
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'],'coupon':data['coupon'], 'codAvailable':data['codAvailable']}
|
|
|
284 |
except:
|
|
|
285 |
pass
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
url = data['marketPlaceUrl']
|
|
|
289 |
lowestPrice = 0.0
|
|
|
290 |
try:
|
|
|
291 |
sc = ShopCluesScraper.ShopCluesScraper()
|
|
|
292 |
url = changeToMobileUrl(url, 5)
|
|
|
293 |
productInfo = sc.read(url)
|
|
|
294 |
except Exception as e:
|
| 15912 |
kshitij.so |
295 |
print traceback.print_exc()
|
| 15902 |
kshitij.so |
296 |
raise
|
|
|
297 |
print "LowestPrice ",productInfo['price']
|
|
|
298 |
if productInfo['price'] > 0 and productInfo['inStock']==1:
|
|
|
299 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':productInfo['price'],'coupon':productInfo['coupon'],'codAvailable':productInfo['isCod'],'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':productInfo['inStock']}})
|
|
|
300 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':productInfo['price'] , 'in_stock':productInfo['inStock'],'dealType':data['dealType'], 'rank':data['rank']}})
|
|
|
301 |
else:
|
|
|
302 |
lowestPrice = data['available_price']
|
|
|
303 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0,'priceUpdatedOn':to_java_date(now)}})
|
|
|
304 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0,'dealType':data['dealType'],'rank':data['rank']}})
|
|
|
305 |
|
| 15912 |
kshitij.so |
306 |
return {'_id':data['_id'],'available_price':productInfo['price'],'in_stock':productInfo['inStock'],'source_id':5,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail'], 'coupon':productInfo['coupon'],'codAvailable':productInfo['isCod']}
|
| 15902 |
kshitij.so |
307 |
except Exception as e:
|
|
|
308 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
|
|
309 |
print e
|
|
|
310 |
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'],'coupon':data['coupon'], 'codAvailable':data['codAvailable']}
|
|
|
311 |
|
| 13829 |
kshitij.so |
312 |
else:
|
|
|
313 |
return {}
|
| 15902 |
kshitij.so |
314 |
|
|
|
315 |
|
| 13918 |
kshitij.so |
316 |
|
| 15253 |
kshitij.so |
317 |
def recomputePoints(item, deal):
|
|
|
318 |
try:
|
|
|
319 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
|
|
320 |
except:
|
|
|
321 |
print traceback.print_exc()
|
|
|
322 |
nlcPoints = deal['nlcPoints']
|
|
|
323 |
if item['manualDealThresholdPrice'] >= deal['available_price']:
|
|
|
324 |
dealPoints = item['dealPoints']
|
|
|
325 |
else:
|
|
|
326 |
dealPoints = 0
|
|
|
327 |
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']}})
|
|
|
328 |
|
| 15266 |
kshitij.so |
329 |
|
|
|
330 |
def populateNegativeDeals():
|
|
|
331 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
332 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
333 |
|
|
|
334 |
def recomputeDeal(item):
|
| 13918 |
kshitij.so |
335 |
"""Lets recompute deal for this bundle"""
|
| 15266 |
kshitij.so |
336 |
print "Recomputing for bundleId",item.get('skuBundleId')
|
|
|
337 |
skuBundleId = item['skuBundleId']
|
| 13829 |
kshitij.so |
338 |
|
| 13918 |
kshitij.so |
339 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
340 |
bestPrice = float("inf")
|
|
|
341 |
bestOne = None
|
|
|
342 |
bestSellerPoints = 0
|
|
|
343 |
toUpdate = []
|
|
|
344 |
for similarItem in similarItems:
|
| 15266 |
kshitij.so |
345 |
if similarItem['_id'] == item['_id']:
|
|
|
346 |
recomputePoints(item, similarItem)
|
| 14332 |
kshitij.so |
347 |
if mc.get("negative_deals") is None:
|
| 14324 |
kshitij.so |
348 |
populateNegativeDeals()
|
| 14332 |
kshitij.so |
349 |
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 |
350 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
351 |
continue
|
| 15902 |
kshitij.so |
352 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
|
|
353 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
354 |
continue
|
| 13918 |
kshitij.so |
355 |
if similarItem['available_price'] < bestPrice:
|
|
|
356 |
bestOne = similarItem
|
|
|
357 |
bestPrice = similarItem['available_price']
|
|
|
358 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
359 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
360 |
bestOne = similarItem
|
|
|
361 |
bestPrice = similarItem['available_price']
|
|
|
362 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
363 |
else:
|
|
|
364 |
pass
|
|
|
365 |
if bestOne is not None:
|
|
|
366 |
for similarItem in similarItems:
|
|
|
367 |
toUpdate.append(similarItem['_id'])
|
|
|
368 |
toUpdate.remove(bestOne['_id'])
|
|
|
369 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
370 |
if len(toUpdate) > 0:
|
|
|
371 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
| 14342 |
kshitij.so |
372 |
|
|
|
373 |
print "Done with recomputing"
|
| 13829 |
kshitij.so |
374 |
|
|
|
375 |
def getLatestPrice(skuBundleId, source_id):
|
|
|
376 |
temp = []
|
|
|
377 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
|
|
378 |
for item in itemIds:
|
| 14309 |
kshitij.so |
379 |
item['dealFlag'] = 0
|
|
|
380 |
item['dealType'] = 0
|
| 15266 |
kshitij.so |
381 |
item['dealPoints'] = 0
|
|
|
382 |
item['manualDealThresholdPrice'] = None
|
| 15912 |
kshitij.so |
383 |
if item['source_id'] ==5 and item['rank'] == 0:
|
|
|
384 |
continue
|
| 15187 |
kshitij.so |
385 |
if item['source_id'] ==3:
|
|
|
386 |
item['marketPlaceUrl'] = item['marketPlaceUrl']+'?supc='+item.get('identifier')
|
| 14309 |
kshitij.so |
387 |
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']}))
|
|
|
388 |
if len(manualDeals) > 0:
|
|
|
389 |
item['dealFlag'] = 1
|
| 14760 |
kshitij.so |
390 |
item['dealType'] =manualDeals[0]['dealType']
|
| 15266 |
kshitij.so |
391 |
item['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
392 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14760 |
kshitij.so |
393 |
info = returnLatestPrice(item, source_id)
|
| 14794 |
kshitij.so |
394 |
print info
|
| 14760 |
kshitij.so |
395 |
try:
|
|
|
396 |
cashBack = getCashBack(item['_id'], item['source_id'], item['category_id'], mc, 'localhost')
|
| 14794 |
kshitij.so |
397 |
print "CashBack is ",cashBack
|
| 14760 |
kshitij.so |
398 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 14801 |
kshitij.so |
399 |
info['cash_back_type'] = 0
|
|
|
400 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
401 |
else:
|
|
|
402 |
if cashBack['cash_back_type'] in (1,2):
|
|
|
403 |
info['cash_back_type'] = cashBack['cash_back_type']
|
|
|
404 |
info['cash_back'] = float(cashBack['cash_back'])
|
|
|
405 |
else:
|
| 14801 |
kshitij.so |
406 |
info['cash_back_type'] = 0
|
|
|
407 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
408 |
except Exception as cashBackEx:
|
| 14794 |
kshitij.so |
409 |
traceback.print_exc()
|
| 14760 |
kshitij.so |
410 |
print "Error calculating cashback."
|
|
|
411 |
info['cash_back_type'] = 0
|
|
|
412 |
info['cash_back'] = 0
|
| 14794 |
kshitij.so |
413 |
print "info is ",info
|
| 14760 |
kshitij.so |
414 |
temp.append(info)
|
| 14367 |
kshitij.so |
415 |
try:
|
| 15266 |
kshitij.so |
416 |
thread = threading.Thread(target=recomputeDeal, args = (item,))
|
| 14367 |
kshitij.so |
417 |
thread.daemon = True
|
|
|
418 |
thread.start()
|
|
|
419 |
except:
|
| 15273 |
kshitij.so |
420 |
print traceback.print_exc()
|
| 14367 |
kshitij.so |
421 |
print "Unable to compute deal for ",skuBundleId
|
| 13829 |
kshitij.so |
422 |
return temp
|
|
|
423 |
|
| 13864 |
kshitij.so |
424 |
def getLatestPriceById(id):
|
|
|
425 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))
|
| 14309 |
kshitij.so |
426 |
item[0]['dealFlag'] = 0
|
|
|
427 |
item[0]['dealType'] = 0
|
| 15266 |
kshitij.so |
428 |
item[0]['dealPoints'] = 0
|
|
|
429 |
item[0]['manualDealThresholdPrice'] = None
|
| 14309 |
kshitij.so |
430 |
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']}))
|
|
|
431 |
if len(manualDeals) > 0:
|
|
|
432 |
item[0]['dealFlag'] = 1
|
| 15266 |
kshitij.so |
433 |
item[0]['dealType'] =manualDeals[0]['dealType']
|
|
|
434 |
item[0]['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
435 |
item[0]['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14367 |
kshitij.so |
436 |
|
| 14431 |
kshitij.so |
437 |
info = returnLatestPrice(item[0], item[0]['source_id'])
|
| 14794 |
kshitij.so |
438 |
print info
|
| 14367 |
kshitij.so |
439 |
try:
|
| 14794 |
kshitij.so |
440 |
cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'], mc, 'localhost')
|
|
|
441 |
print "CashBack is ",cashBack
|
| 14760 |
kshitij.so |
442 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 14801 |
kshitij.so |
443 |
info['cash_back_type'] = 0
|
|
|
444 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
445 |
else:
|
|
|
446 |
if cashBack['cash_back_type'] in (1,2):
|
|
|
447 |
info['cash_back_type'] = cashBack['cash_back_type']
|
|
|
448 |
info['cash_back'] = float(cashBack['cash_back'])
|
|
|
449 |
else:
|
| 14801 |
kshitij.so |
450 |
info['cash_back_type'] = 0
|
|
|
451 |
info['cash_back'] = 0
|
| 14760 |
kshitij.so |
452 |
except Exception as cashBackEx:
|
| 14794 |
kshitij.so |
453 |
traceback.print_exc()
|
| 14760 |
kshitij.so |
454 |
print cashBackEx
|
|
|
455 |
print "Error calculating cashback."
|
|
|
456 |
info['cash_back_type'] = 0
|
|
|
457 |
info['cash_back'] = 0
|
|
|
458 |
try:
|
| 15273 |
kshitij.so |
459 |
thread = threading.Thread(target=recomputeDeal, args = (item[0],))
|
| 14367 |
kshitij.so |
460 |
thread.daemon = True
|
|
|
461 |
thread.start()
|
|
|
462 |
except:
|
|
|
463 |
print "Unable to compute deal for ",item[0]['skuBundleId']
|
| 14431 |
kshitij.so |
464 |
return info
|
| 13829 |
kshitij.so |
465 |
|
| 14828 |
kshitij.so |
466 |
def updatePriceForNotificationBundles(skuBundleId):
|
| 15266 |
kshitij.so |
467 |
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 |
468 |
for item in itemIds:
|
| 14833 |
kshitij.so |
469 |
print item['_id']
|
| 14828 |
kshitij.so |
470 |
item['dealFlag'] = 0
|
|
|
471 |
item['dealType'] = 0
|
| 15266 |
kshitij.so |
472 |
item['dealPoints'] = 0
|
|
|
473 |
item['manualDealThresholdPrice'] = None
|
| 14828 |
kshitij.so |
474 |
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']}))
|
|
|
475 |
if len(manualDeals) > 0:
|
|
|
476 |
item['dealFlag'] = 1
|
|
|
477 |
item['dealType'] =manualDeals[0]['dealType']
|
| 15266 |
kshitij.so |
478 |
item['dealPoints'] = manualDeals[0]['dealPoints']
|
|
|
479 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 14828 |
kshitij.so |
480 |
info = returnLatestPrice(item, item['source_id'],False)
|
|
|
481 |
print info
|
|
|
482 |
|
| 13829 |
kshitij.so |
483 |
def main():
|
| 15912 |
kshitij.so |
484 |
print "retuned %s"%(str(getLatestPrice(33)))
|
| 13829 |
kshitij.so |
485 |
|
|
|
486 |
if __name__=='__main__':
|
| 13971 |
kshitij.so |
487 |
main()
|
|
|
488 |
|