| Line 1... |
Line 1... |
| 1 |
import urllib2
|
1 |
import urllib2
|
| 2 |
import simplejson as json
|
2 |
import simplejson as json
|
| 3 |
import pymongo
|
3 |
import pymongo
|
| 4 |
from dtr.utils.utils import to_java_date, getNlcPoints
|
4 |
from dtr.utils.utils import to_java_date, getNlcPoints, changeToMobileUrl
|
| 5 |
from datetime import datetime, timedelta
|
5 |
from datetime import datetime, timedelta
|
| 6 |
from operator import itemgetter
|
6 |
from operator import itemgetter
|
| 7 |
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
|
7 |
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
|
| 8 |
from dtr.utils import AmazonDealScraper
|
8 |
from dtr.utils import AmazonDealScraper
|
| 9 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper
|
9 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper, ShopCluesScraper
|
| 10 |
from dtr.storage.MemCache import MemCache
|
10 |
from dtr.storage.MemCache import MemCache
|
| 11 |
from functools import partial
|
11 |
from functools import partial
|
| 12 |
import threading
|
12 |
import threading
|
| 13 |
from dtr.utils.utils import getCashBack
|
13 |
from dtr.utils.utils import getCashBack
|
| 14 |
import traceback
|
14 |
import traceback
|
| Line 19... |
Line 19... |
| 19 |
host = config_client.get_property('mongo_dtr_host')
|
19 |
host = config_client.get_property('mongo_dtr_host')
|
| 20 |
|
20 |
|
| 21 |
mc = MemCache(host_memCache)
|
21 |
mc = MemCache(host_memCache)
|
| 22 |
|
22 |
|
| 23 |
|
23 |
|
| 24 |
con = None
|
24 |
con = None
|
| 25 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4}
|
25 |
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5}
|
| 26 |
|
26 |
|
| 27 |
headers = {
|
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',
|
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',
|
29 |
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 30 |
'Accept-Language' : 'en-US,en;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'
|
31 |
'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
|
| 32 |
}
|
32 |
}
|
| 33 |
|
33 |
|
| 34 |
ignoreItems = [23219]
|
34 |
ignoreItems = []
|
| 35 |
|
35 |
|
| 36 |
def get_mongo_connection(port=27017):
|
36 |
def get_mongo_connection(port=27017):
|
| 37 |
global con
|
37 |
global con
|
| 38 |
if con is None:
|
38 |
if con is None:
|
| 39 |
print "Establishing connection %s host and port %d" %(host,port)
|
39 |
print "Establishing connection %s host and port %d" %(host,port)
|
| Line 268... |
Line 268... |
| 268 |
|
268 |
|
| 269 |
except Exception as e:
|
269 |
except Exception as e:
|
| 270 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
270 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
| 271 |
print e
|
271 |
print e
|
| 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']}
|
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']}
|
| - |
|
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:
|
| - |
|
295 |
raise
|
| - |
|
296 |
print "LowestPrice ",productInfo['price']
|
| - |
|
297 |
if productInfo['price'] > 0 and productInfo['inStock']==1:
|
| - |
|
298 |
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']}})
|
| - |
|
299 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':productInfo['price'] , 'in_stock':productInfo['inStock'],'dealType':data['dealType'], 'rank':data['rank']}})
|
| - |
|
300 |
else:
|
| - |
|
301 |
lowestPrice = data['available_price']
|
| - |
|
302 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0,'priceUpdatedOn':to_java_date(now)}})
|
| - |
|
303 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0,'dealType':data['dealType'],'rank':data['rank']}})
|
| - |
|
304 |
|
| - |
|
305 |
return {'_id':data['_id'],'available_price':lowestPrice,'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']}
|
| - |
|
306 |
except Exception as e:
|
| - |
|
307 |
print "Exception for _id %d and source %s"%(data['_id'], source_id)
|
| - |
|
308 |
print e
|
| - |
|
309 |
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']}
|
| - |
|
310 |
|
| 273 |
else:
|
311 |
else:
|
| 274 |
return {}
|
312 |
return {}
|
| - |
|
313 |
|
| - |
|
314 |
|
| 275 |
|
315 |
|
| 276 |
def recomputePoints(item, deal):
|
316 |
def recomputePoints(item, deal):
|
| 277 |
try:
|
317 |
try:
|
| 278 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
318 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
| 279 |
except:
|
319 |
except:
|
| Line 306... |
Line 346... |
| 306 |
if mc.get("negative_deals") is None:
|
346 |
if mc.get("negative_deals") is None:
|
| 307 |
populateNegativeDeals()
|
347 |
populateNegativeDeals()
|
| 308 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
348 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 309 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
349 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| 310 |
continue
|
350 |
continue
|
| - |
|
351 |
if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
|
| - |
|
352 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| - |
|
353 |
continue
|
| 311 |
if similarItem['available_price'] < bestPrice:
|
354 |
if similarItem['available_price'] < bestPrice:
|
| 312 |
bestOne = similarItem
|
355 |
bestOne = similarItem
|
| 313 |
bestPrice = similarItem['available_price']
|
356 |
bestPrice = similarItem['available_price']
|
| 314 |
bestSellerPoints = similarItem['bestSellerPoints']
|
357 |
bestSellerPoints = similarItem['bestSellerPoints']
|
| 315 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
358 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
| Line 330... |
Line 373... |
| 330 |
|
373 |
|
| 331 |
def getLatestPrice(skuBundleId, source_id):
|
374 |
def getLatestPrice(skuBundleId, source_id):
|
| 332 |
temp = []
|
375 |
temp = []
|
| 333 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
376 |
itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
|
| 334 |
for item in itemIds:
|
377 |
for item in itemIds:
|
| - |
|
378 |
if item['source_id'] ==5 and item['rank'] == 0:
|
| - |
|
379 |
continue
|
| 335 |
item['dealFlag'] = 0
|
380 |
item['dealFlag'] = 0
|
| 336 |
item['dealType'] = 0
|
381 |
item['dealType'] = 0
|
| 337 |
item['dealPoints'] = 0
|
382 |
item['dealPoints'] = 0
|
| 338 |
item['manualDealThresholdPrice'] = None
|
383 |
item['manualDealThresholdPrice'] = None
|
| 339 |
if item['source_id'] ==3:
|
384 |
if item['source_id'] ==3:
|
| Line 433... |
Line 478... |
| 433 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
478 |
item['manualDealThresholdPrice'] = manualDeals[0]['dealThresholdPrice']
|
| 434 |
info = returnLatestPrice(item, item['source_id'],False)
|
479 |
info = returnLatestPrice(item, item['source_id'],False)
|
| 435 |
print info
|
480 |
print info
|
| 436 |
|
481 |
|
| 437 |
def main():
|
482 |
def main():
|
| 438 |
print "retuned %s"%(str(getLatestPriceById(8840)))
|
483 |
print "retuned %s"%(str(getLatestPriceById(23827)))
|
| 439 |
|
484 |
|
| 440 |
if __name__=='__main__':
|
485 |
if __name__=='__main__':
|
| 441 |
main()
|
486 |
main()
|
| 442 |
|
487 |
|