Subversion Repositories SmartDukaan

Rev

Rev 14308 | Rev 14328 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14308 Rev 14325
Line 3... Line 3...
3
from datetime import datetime, timedelta
3
from datetime import datetime, timedelta
4
from dtr.utils import AmazonPriceOnlyScraper, AmazonDealScraper
4
from dtr.utils import AmazonPriceOnlyScraper, AmazonDealScraper
5
from multiprocessing import Pool as ThreadPool
5
from multiprocessing import Pool as ThreadPool
6
from multiprocessing import cpu_count
6
from multiprocessing import cpu_count
7
import optparse
7
import optparse
-
 
8
from dtr.storage.MemCache import MemCache
8
 
9
 
9
con = None
10
con = None
10
 
11
 
11
parser = optparse.OptionParser()
12
parser = optparse.OptionParser()
12
parser.add_option("-m", "--m", dest="mongoHost",
13
parser.add_option("-m", "--m", dest="mongoHost",
Line 14... Line 15...
14
                      type="string", help="The HOST where the mongo server is running",
15
                      type="string", help="The HOST where the mongo server is running",
15
                      metavar="mongo_host")
16
                      metavar="mongo_host")
16
 
17
 
17
(options, args) = parser.parse_args()
18
(options, args) = parser.parse_args()
18
 
19
 
-
 
20
mc = MemCache(options.mongoHost)
-
 
21
 
19
def get_mongo_connection(host=options.mongoHost, port=27017):
22
def get_mongo_connection(host=options.mongoHost, port=27017):
20
    global con
23
    global con
21
    if con is None:
24
    if con is None:
22
        print "Establishing connection %s host and port %d" %(host,port)
25
        print "Establishing connection %s host and port %d" %(host,port)
23
        try:
26
        try:
Line 112... Line 115...
112
    try:
115
    try:
113
        recomputeDeal(data['skuBundleId'])
116
        recomputeDeal(data['skuBundleId'])
114
    except:
117
    except:
115
        print "Unable to compute deal for ",data['skuBundleId']    
118
        print "Unable to compute deal for ",data['skuBundleId']    
116
            
119
            
117
        
120
def populateNegativeDeals():
-
 
121
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
-
 
122
    mc.set("negative_deals", negativeDeals, 600)      
118
 
123
 
119
def recomputeDeal(skuBundleId):
124
def recomputeDeal(skuBundleId):
120
    """Lets recompute deal for this bundle"""
125
    """Lets recompute deal for this bundle"""
121
    print "Recomputing for bundleId",skuBundleId
126
    print "Recomputing for bundleId",skuBundleId
122
    
127
    
Line 124... Line 129...
124
    bestPrice = float("inf")
129
    bestPrice = float("inf")
125
    bestOne = None
130
    bestOne = None
126
    bestSellerPoints = 0
131
    bestSellerPoints = 0
127
    toUpdate = []
132
    toUpdate = []
128
    for similarItem in similarItems:
133
    for similarItem in similarItems:
-
 
134
        if mc.get("negativeDeals") is None:
-
 
135
            populateNegativeDeals()
129
        if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
136
        if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negativeDeals"):
130
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
137
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
131
            continue
138
            continue
132
        if similarItem['available_price'] < bestPrice:
139
        if similarItem['available_price'] < bestPrice:
133
            bestOne = similarItem
140
            bestOne = similarItem
134
            bestPrice = similarItem['available_price']
141
            bestPrice = similarItem['available_price']