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