Rev 13975 | Rev 14124 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import urllib2import simplejson as jsonimport pymongofrom dtr.utils.utils import to_java_datefrom datetime import datetime, timedeltafrom operator import itemgetterfrom dtr.utils.AmazonPriceOnlyScraper import AmazonScraperfrom dtr.utils import FlipkartScrapercon = NoneSOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3}scraperFk = FlipkartScraper.FlipkartScraper()scraperAmazon = AmazonScraper()headers = {'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11','Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language' : 'en-US,en;q=0.8','Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'}def get_mongo_connection(host='localhost', port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef returnLatestPrice(data, source_id):now = datetime.now()if source_id == 1:try:if data['identifier'] is None or len(data['identifier'].strip())==0:return {}try:if data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):print "sku id is already updated",data['_id']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']}except:passurl = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])lowestPrice = 0.0lowestPrice = scraperAmazon.read(url)print "LowestPrice ",lowestPriceinStock = 0if lowestPrice > 0:inStock = 1if lowestPrice > 0: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)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':inStock}}, multi=True)else:lowestPrice = data['available_price']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)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':0}}, multi=True)try:recomputeDeal(data['skuBundleId'])except:print "Unable to compute deal for ",data['skuBundleId']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']}except: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']}elif source_id ==3:try:if data['identifier'] is None or len(data['identifier'].strip())==0:return {}try:if data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):print "sku id is already updated",data['_id']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']}except Exception as e:print "Exception snapdeal"print epassurl="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])req = urllib2.Request(url,headers=headers)response = urllib2.urlopen(req)json_input = response.read()vendorInfo = json.loads(json_input)lowestOfferPrice = 0inStock = 0for vendor in vendorInfo:lowestOfferPrice = float(vendor['sellingPrice'])stock = vendor['buyableInventory']if stock > 0 and lowestOfferPrice > 0:inStock = 1breakprint lowestOfferPriceprint inStockprint "*************"if inStock == 1: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}}, multi=True)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)else:lowestOfferPrice = data['available_price']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)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)try:recomputeDeal(data['skuBundleId'])except:print "Unable to compute deal for ",data['skuBundleId']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']}except: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']}elif source_id == 2:try:if data['identifier'] is None or len(data['identifier'].strip())==0:return {}try:if data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):print "sku id is already updated",data['_id']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']}except:passurl = "http://www.flipkart.com/ps/%s"%(data['identifier'])vendorsData = scraperFk.read(url)sortedVendorsData = []sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))print "data",sortedVendorsDatalowestSp = 0inStock = 0for vData in sortedVendorsData:lowestSp = vData['sellingPrice']breakif lowestSp > 0:inStock = 1print lowestSpprint inStockif lowestSp > 0: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}}, multi=True)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)else:lowestSp = data['available_price']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)get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)try:recomputeDeal(data['skuBundleId'])except:print "Unable to compute deal for ",data['skuBundleId']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']}except: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']}else:return {}def recomputeDeal(skuBundleId):"""Lets recompute deal for this bundle"""print "Recomputing for bundleId",skuBundleIdsimilarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))bestPrice = float("inf")bestOne = NonebestSellerPoints = 0toUpdate = []for similarItem in similarItems:if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})continueif similarItem['available_price'] < bestPrice:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']else:passif bestOne is not None:for similarItem in similarItems:toUpdate.append(similarItem['_id'])toUpdate.remove(bestOne['_id'])get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})if len(toUpdate) > 0:get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)def getLatestPrice(skuBundleId, source_id):temp = []itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))for item in itemIds:temp.append(returnLatestPrice(item, source_id))return tempdef getLatestPriceById(id):item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))return returnLatestPrice(item[0], item[0]['source_id'])def main():print getLatestPriceById(8850)if __name__=='__main__':main()"""21.06$"""