Subversion Repositories SmartDukaan

Rev

Rev 14705 | Rev 15823 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

import urllib2
import simplejson as json
import pymongo
from dtr.utils.utils import to_java_date, getNlcPoints 
from datetime import datetime, timedelta
import time
from multiprocessing import Pool as ThreadPool
from multiprocessing import cpu_count
import optparse
from dtr.storage.MemCache import MemCache
from dtr.utils.utils import getCashBack
import traceback
from operator import itemgetter

con = None

parser = optparse.OptionParser()
parser.add_option("-m", "--m", dest="mongoHost",
                      default="localhost",
                      type="string", help="The HOST where the mongo server is running",
                      metavar="mongo_host")

(options, args) = parser.parse_args()

mc = MemCache(options.mongoHost)

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=options.mongoHost, port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

def populate():
    toScrapMap = {}
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
    for bestSeller in bestSellers: 
        snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
        for data in snapdealBestSellers:
            if not toScrapMap.has_key(data['_id']):
                data['dealFlag'] = 0
                data['dealType'] = 0
                data['dealPoints'] = 0
                data['manualDealThresholdPrice'] = None
                toScrapMap[data['_id']] = data
    dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':3,'showDeal':1,'totalPoints':{'$gt':0}}))
    for deal in dealFlagged:
        if not toScrapMap.has_key(deal['_id']):
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
            data[0]['dealFlag'] = 0
            data[0]['dealType'] = 0
            data[0]['dealPoints'] = 0
            data[0]['manualDealThresholdPrice'] = None
            toScrapMap[deal['_id']] = data[0]
    manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':3}))
    for manualDeal in manualDeals:
        if not toScrapMap.has_key(manualDeal['sku']):
            data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
            if len(data) > 0:
                data[0]['dealFlag'] = 1
                data[0]['dealType'] = manualDeal['dealType']
                data[0]['dealPoints'] = manualDeal['dealPoints']
                data[0]['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
                toScrapMap[manualDeal['sku']] = data[0]
        else:
            data = toScrapMap.get(manualDeal['sku'])
            data['dealFlag'] = 1
            data['dealType'] = manualDeal['dealType']
            data['dealPoints'] = manualDeal['dealPoints']
            data['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
    pool = ThreadPool(cpu_count() *2)
    pool.map(updatePrices,toScrapMap.values())
    pool.close()
    pool.join()
    print "joining threads at %s"%(str(datetime.now()))


def updatePrices(data):
    if data['source_id']!=3:
        return
    print data['identifier']
    if data['identifier'] is None or len(data['identifier'].strip())==0:
        print "returning"
        return
    
    try:
        if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
            print "sku id is already updated",data['_id'] 
            return
    except:
        pass
    
    url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175"%(data['identifier'].strip())
    print url
    time.sleep(1)
    lowestOfferPrice = 0
    instock = 0
    buyBoxPrice = 0
    isBuyBox = 1
    stock = 0
    req = urllib2.Request(url,headers=headers)
    response = urllib2.urlopen(req)
    json_input = response.read()
    response.close()
    if len(json_input) > 0:
        vendorInfo = json.loads(json_input)
        for vendor in vendorInfo:
            buyBoxPrice = float(vendor['sellingPrice'])
            try:
                buyBoxStock = vendor['buyableInventory']
            except:
                buyBoxStock = 0
            if buyBoxStock > 0 and buyBoxPrice > 0:
                break
            
        sortedVendorsData = sorted(vendorInfo, key=itemgetter('sellingPrice'))
        for sortedVendorData in sortedVendorsData:
            lowestOfferPrice = float(sortedVendorData['sellingPrice'])
            try:
                stock = sortedVendorData['buyableInventory']
            except:
                pass
            if stock > 0 and lowestOfferPrice > 0:
                instock = 1
                break
        if buyBoxPrice != lowestOfferPrice:
            isBuyBox = 0

    print lowestOfferPrice
    print instock
    print "Lowest Offer Price for id %d is %d , stock is %d and stock count is %d" %(data['_id'],lowestOfferPrice,instock,stock)
    print "*************"
    if instock  == 1:
        get_mongo_connection().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,'buyBoxFlag':isBuyBox}}, multi=True)
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
    else:
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':instock,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':isBuyBox}}, multi=True)
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
    
    try:
        recomputeDeal(data)
    except:
        print "Unable to compute deal for ",data['skuBundleId']
    

def populateNegativeDeals():
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
    mc.set("negative_deals", negativeDeals, 600)

def recomputePoints(item, deal):
    try:
        nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
    except:
        traceback.print_exc()
        nlcPoints = deal['nlcPoints']
    if item['manualDealThresholdPrice'] >= deal['available_price']:
        dealPoints = item['dealPoints']
    else:
        dealPoints = 0
    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']}})

    
def recomputeDeal(item):
    """Lets recompute deal for this bundle"""
    print "Recomputing for bundleId",item.get('skuBundleId')
    skuBundleId = item['skuBundleId']
    
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
    bestPrice = float("inf")
    bestOne = None
    bestSellerPoints = 0
    toUpdate = []
    for similarItem in similarItems:
        if mc.get("negative_deals") is None:
            populateNegativeDeals()
#        try:
#            cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
#            if not cashBack or cashBack.get('cash_back_status')!=1:
#                pass
#            else:
#                if cashBack['cash_back_type'] ==1:
#                    similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
#                elif cashBack['cash_back_type'] ==2:
#                    similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
#                else:
#                    pass
#        except Exception as cashBackEx:
#            print cashBackEx
#            print "Error calculating cashback."
        if similarItem['_id'] == item['_id']:
            try:
                recomputePoints(item, similarItem)
            except:
                traceback.print_exc()
        if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
            continue
        if similarItem['available_price'] < bestPrice:
            bestOne = similarItem
            bestPrice = similarItem['available_price']
            bestSellerPoints = similarItem['bestSellerPoints']
        elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
            bestOne = similarItem
            bestPrice = similarItem['available_price']
            bestSellerPoints = similarItem['bestSellerPoints']
        else:
            pass
    if 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 main():
    populate()

if __name__=='__main__':
    main()