Subversion Repositories SmartDukaan

Rev

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

Rev 17107 Rev 19191
Line 13... Line 13...
13
from operator import itemgetter
13
from operator import itemgetter
14
import chardet
14
import chardet
15
from dtr.utils import HomeShop18Scraper
15
from dtr.utils import HomeShop18Scraper
16
 
16
 
17
 
17
 
-
 
18
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3,'SAHOLIC':4, 'SHOPCLUES.COM':5,'PAYTM.COM':6, 'HOMESHOP18.COM':7}
-
 
19
 
18
con = None
20
con = None
19
 
21
 
20
parser = optparse.OptionParser()
22
parser = optparse.OptionParser()
21
parser.add_option("-m", "--m", dest="mongoHost",
23
parser.add_option("-m", "--m", dest="mongoHost",
22
                      default="localhost",
24
                      default="localhost",
Line 25... Line 27...
25
 
27
 
26
(options, args) = parser.parse_args()
28
(options, args) = parser.parse_args()
27
 
29
 
28
mc = MemCache(options.mongoHost)
30
mc = MemCache(options.mongoHost)
29
 
31
 
-
 
32
def getNetPriceForItem(itemId, source_id, category_id ,price):
-
 
33
    cash_back_type = 0
-
 
34
    cash_back = 0
-
 
35
    try:
-
 
36
        cashBack = getCashBack(itemId, source_id, category_id, mc, options.mongoHost)
-
 
37
        if not cashBack or cashBack.get('cash_back_status')!=1:
-
 
38
            cash_back_type = 0
-
 
39
            cash_back = 0 
-
 
40
            
-
 
41
        else:
-
 
42
            if cashBack['cash_back_type'] in (1,2):
-
 
43
                
-
 
44
                if cashBack.get('maxCashBack') is not None:
-
 
45
                    
-
 
46
                    if cashBack.get('cash_back_type') ==1 and (float(cashBack.get('cash_back'))*price)/100 > cashBack.get('maxCashBack'):
-
 
47
                        cashBack['cash_back_type'] = 2
-
 
48
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
49
                    elif cashBack.get('cash_back_type') ==2 and cashBack.get('cash_back') > cashBack.get('maxCashBack'):
-
 
50
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
51
                    else:
-
 
52
                        pass
-
 
53
                
-
 
54
                
-
 
55
                
-
 
56
                cash_back_type = cashBack['cash_back_type']
-
 
57
                cash_back = float(cashBack['cash_back'])
-
 
58
    except Exception as cashBackEx:
-
 
59
        pass
-
 
60
    
-
 
61
    if cash_back_type ==1:
-
 
62
        return (price - float(cash_back)*price/100)
-
 
63
    elif cash_back_type ==2:
-
 
64
        return (price - cash_back)
-
 
65
    else:
-
 
66
        return price
-
 
67
 
-
 
68
 
30
def populate():
69
def populate():
31
    toScrapMap = {}
70
    toScrapMap = {}
32
    bestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'rank':{'$gt':0}}))
71
    bestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'rank':{'$gt':0}}))
33
    for bestSeller in bestSellers: 
72
    for bestSeller in bestSellers: 
34
        snapdealBestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':7}))
73
        snapdealBestSellers = list(get_mongo_connection(host=options.mongoHost).Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':7}))
Line 102... Line 141...
102
    
141
    
103
    if lowestOfferPrice ==0:
142
    if lowestOfferPrice ==0:
104
        inStock = 0
143
        inStock = 0
105
        
144
        
106
    if inStock  == 1:
145
    if inStock  == 1:
-
 
146
        netPriceAfterCashBack = getNetPriceForItem(data['_id'], SOURCE_MAP.get('HOMESHOP18.COM'), data['category_id'], lowestOfferPrice)
107
        get_mongo_connection(host=options.mongoHost).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}}, multi=True)
147
        get_mongo_connection(host=options.mongoHost).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}}, multi=True)
108
        get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
148
        get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock,'codAvailable':data['codAvailable'],'netPriceAfterCashBack':netPriceAfterCashBack}}, multi=True)
109
    else:
149
    else:
110
        lowestOfferPrice = data['available_price']
150
        lowestOfferPrice = data['available_price']
-
 
151
        netPriceAfterCashBack = getNetPriceForItem(data['_id'], SOURCE_MAP.get('HOMESHOP18.COM'), data['category_id'], lowestOfferPrice)
111
        get_mongo_connection(host=options.mongoHost).Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':inStock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
152
        get_mongo_connection(host=options.mongoHost).Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':inStock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
112
        get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
153
        get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable'],'netPriceAfterCashBack':netPriceAfterCashBack}}, multi=True)
113
        
154
        
114
    try:
155
    try:
115
        recomputeDeal(data)
156
        recomputeDeal(data)
116
    except:
157
    except:
117
        print "Unable to compute deal for ",data['skuBundleId']
158
        print "Unable to compute deal for ",data['skuBundleId']
Line 123... Line 164...
123
def recomputeDeal(item):
164
def recomputeDeal(item):
124
    """Lets recompute deal for this bundle"""
165
    """Lets recompute deal for this bundle"""
125
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
166
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
126
    skuBundleId = item['skuBundleId']
167
    skuBundleId = item['skuBundleId']
127
    
168
    
128
    similarItems = list(get_mongo_connection(host=options.mongoHost).Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
169
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('netPriceAfterCashBack',pymongo.ASCENDING)]))
129
    bestPrice = float("inf")
170
    bestPrice = float("inf")
130
    bestOne = None
171
    bestOne = None
131
    bestSellerPoints = 0
172
    bestSellerPoints = 0
132
    toUpdate = []
173
    toUpdate = []
133
    prepaidBestPrice = float("inf")
174
    prepaidBestPrice = float("inf")
Line 136... Line 177...
136
    for similarItem in similarItems:
177
    for similarItem in similarItems:
137
        if similarItem['codAvailable'] ==1:
178
        if similarItem['codAvailable'] ==1:
138
            if mc.get("negative_deals") is None:
179
            if mc.get("negative_deals") is None:
139
                populateNegativeDeals()
180
                populateNegativeDeals()
140
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
181
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
141
                get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
182
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
142
                continue
183
                continue
143
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
184
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
144
                get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
185
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
145
                continue
186
                continue
146
            if similarItem['available_price'] < bestPrice:
187
            if similarItem.get('netPriceAfterCashBack') < bestPrice:
147
                bestOne = similarItem
188
                bestOne = similarItem
148
                bestPrice = similarItem['available_price']
189
                bestPrice = similarItem.get('netPriceAfterCashBack')
149
                bestSellerPoints = similarItem['bestSellerPoints']
190
                bestSellerPoints = similarItem['bestSellerPoints']
150
            elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
191
            elif similarItem.get('netPriceAfterCashBack') == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
151
                bestOne = similarItem
192
                bestOne = similarItem
152
                bestPrice = similarItem['available_price']
193
                bestPrice = similarItem.get('netPriceAfterCashBack')
153
                bestSellerPoints = similarItem['bestSellerPoints']
194
                bestSellerPoints = similarItem['bestSellerPoints']
154
            else:
195
            else:
155
                pass
196
                pass
156
        else:
197
        else:
157
            if mc.get("negative_deals") is None:
198
            if mc.get("negative_deals") is None:
158
                populateNegativeDeals()
199
                populateNegativeDeals()
159
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
200
            if similarItem['in_stock'] == 0  or similarItem['_id'] in mc.get("negative_deals"):
160
                get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
201
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
161
                continue
202
                continue
162
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
203
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
163
                get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
204
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
164
                continue
205
                continue
165
            if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
-
 
166
                similarItem['available_price'] = similarItem['gross_price']
-
 
167
            if similarItem['available_price'] < prepaidBestPrice:
206
            if similarItem.get('netPriceAfterCashBack') < prepaidBestPrice:
168
                prepaidBestOne = similarItem
207
                prepaidBestOne = similarItem
169
                prepaidBestPrice = similarItem['available_price']
208
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
170
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
209
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
171
            elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
210
            elif similarItem.get('netPriceAfterCashBack') == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
172
                prepaidBestOne = similarItem
211
                prepaidBestOne = similarItem
173
                prepaidBestPrice = similarItem['available_price']
212
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
174
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
213
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
175
            else:
214
            else:
176
                pass
215
                pass
177
    if bestOne is not None or prepaidBestOne is not None:
216
    if bestOne is not None or prepaidBestOne is not None:
178
        for similarItem in similarItems:
217
        for similarItem in similarItems:
179
            toUpdate.append(similarItem['_id'])
218
            toUpdate.append(similarItem['_id'])
180
        if bestOne is not None:
219
        if bestOne is not None:
181
            toUpdate.remove(bestOne['_id'])
220
            toUpdate.remove(bestOne['_id'])
182
            get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
221
            get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
183
        if prepaidBestOne is not None:
222
        if prepaidBestOne is not None:
184
            if bestOne is not None:
223
            if bestOne is not None:
185
                if prepaidBestOne['available_price'] < bestOne['available_price']: 
224
                if prepaidBestOne.get('netPriceAfterCashBack') < bestOne.get('netPriceAfterCashBack'): 
186
                    toUpdate.remove(prepaidBestOne['_id'])
225
                    toUpdate.remove(prepaidBestOne['_id'])
187
                    get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
226
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
188
            else:
227
            else:
189
                toUpdate.remove(prepaidBestOne['_id'])
228
                toUpdate.remove(prepaidBestOne['_id'])
190
                get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
229
                get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
191
    if len(toUpdate) > 0:
230
    if len(toUpdate) > 0:
192
        get_mongo_connection(host=options.mongoHost).Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
231
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
193
 
232
 
194
 
233
 
195
def main():
234
def main():
196
    populate()
235
    populate()
197
 
236