Subversion Repositories SmartDukaan

Rev

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

Rev 15213 Rev 15253
Line 1... Line 1...
1
import urllib2
1
import urllib2
2
import simplejson as json
2
import simplejson as json
3
import pymongo
3
import pymongo
4
from dtr.utils.utils import to_java_date
4
from dtr.utils.utils import to_java_date, getNlcPoints
5
from datetime import datetime, timedelta
5
from datetime import datetime, timedelta
6
from operator import itemgetter
6
from operator import itemgetter
7
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
7
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
8
from dtr.utils import AmazonDealScraper
8
from dtr.utils import AmazonDealScraper
9
from dtr.utils import FlipkartScraper,NewFlipkartScraper
9
from dtr.utils import FlipkartScraper,NewFlipkartScraper
Line 148... Line 148...
148
                    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']}
148
                    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']}
149
                
149
                
150
            except Exception as e:
150
            except Exception as e:
151
                print "Exception snapdeal"
151
                print "Exception snapdeal"
152
                print e
152
                print e
153
                pass
-
 
154
            
-
 
155
            
153
            
156
            url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
154
            url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175"%(data['identifier'])
157
            req = urllib2.Request(url,headers=headers)
155
            req = urllib2.Request(url,headers=headers)
158
            response = urllib2.urlopen(req)
156
            response = urllib2.urlopen(req)
159
            json_input = response.read()
157
            json_input = response.read()
160
            response.close()
158
            response.close()
161
            vendorInfo = json.loads(json_input)
159
            vendorInfo = json.loads(json_input)
162
            lowestOfferPrice = 0
160
            lowestOfferPrice = 0
163
            inStock = 0
161
            inStock = 0
-
 
162
            buyBoxPrice = 0
-
 
163
            isBuyBox = 1
164
            for vendor in vendorInfo:
164
            for vendor in vendorInfo:
165
                lowestOfferPrice = float(vendor['sellingPrice'])
165
                buyBoxPrice = float(vendor['sellingPrice'])
-
 
166
                try:
166
                stock = vendor['buyableInventory']
167
                    buyBoxStock = vendor['buyableInventory']
-
 
168
                except:
-
 
169
                    buyBoxStock = 0
-
 
170
                if buyBoxStock > 0 and buyBoxPrice > 0:
-
 
171
                    break
-
 
172
                
-
 
173
            sortedVendorsData = sorted(vendorInfo, key=itemgetter('sellingPrice'))
-
 
174
            for sortedVendorData in sortedVendorsData:
-
 
175
                lowestOfferPrice = float(sortedVendorData['sellingPrice'])
-
 
176
                try:
-
 
177
                    stock = sortedVendorData['buyableInventory']
-
 
178
                except:
-
 
179
                    stock = 0
167
                if stock > 0 and lowestOfferPrice > 0:
180
                if stock > 0 and lowestOfferPrice > 0:
168
                    inStock = 1
181
                    inStock = 1
169
                    break
182
                    break
170
                    
183
                    
171
            print lowestOfferPrice
184
            print lowestOfferPrice
172
            print inStock
185
            print inStock
173
            print "*************"
186
            print "*************"
-
 
187
            
-
 
188
            if buyBoxPrice != lowestOfferPrice:
-
 
189
                isBuyBox = 0
-
 
190
            
174
            if inStock  == 1:
191
            if inStock  == 1:
175
                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)
192
                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,'buyBoxFlag':isBuyBox}}, multi=True)
176
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)
193
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':inStock}}, multi=True)
177
            else:
194
            else:
178
                lowestOfferPrice = data['available_price']
195
                lowestOfferPrice = data['available_price']
179
                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)
196
                get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock,'priceUpdatedOn':to_java_date(now),'buyBoxFlag':isBuyBox}}, multi=True)
180
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
197
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
181
            
198
            
182
            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']}
199
            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']}
183
        except Exception as e:
200
        except Exception as e:
184
            print "Exception for _id %d and source %s"%(data['_id'], source_id)
201
            print "Exception for _id %d and source %s"%(data['_id'], source_id)
Line 197... Line 214...
197
            except:
214
            except:
198
                pass
215
                pass
199
            
216
            
200
            lowestSp = 0
217
            lowestSp = 0
201
            inStock = 0
218
            inStock = 0
-
 
219
            buyBoxPrice = 0
-
 
220
            isBuyBox = 0
202
            scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
221
            scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
203
            try:
222
            try:
204
                if data['marketPlaceUrl']!="" or data['marketPlaceUrl'] !="http://www.flipkart.com/ps/%s"%(data['identifier']):
223
                if data['marketPlaceUrl']!="" or data['marketPlaceUrl'] !="http://www.flipkart.com/ps/%s"%(data['identifier']):
205
                    result = scraperProductPage.read(data['marketPlaceUrl'])
224
                    result = scraperProductPage.read(data['marketPlaceUrl'])
206
                    print result
225
                    print result
207
                    if result.get('lowestSp')!=0:
226
                    if result.get('lowestSp')!=0:
208
                        lowestSp = result.get('lowestSp')
227
                        lowestSp = result.get('lowestSp')
209
                        inStock = result.get('inStock')
228
                        inStock = result.get('inStock')
-
 
229
                        buyBoxPrice = result.get('buyBoxPrice')
210
            except:
230
            except:
211
                print "Unable to scrape product page ",data['identifier']
231
                print "Unable to scrape product page ",data['identifier']
212
            if lowestSp ==0:
232
            if lowestSp ==0:
213
                url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
233
                url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
214
                scraperFk = FlipkartScraper.FlipkartScraper()
234
                scraperFk = FlipkartScraper.FlipkartScraper()
215
                vendorsData = scraperFk.read(url)
235
                vendorsData, buyBoxInfo = (scraperFk.read(url))
216
                sortedVendorsData = []
236
                sortedVendorsData = []
217
                sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
237
                sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
218
                print "data",sortedVendorsData
238
                print "data",sortedVendorsData
219
                for vData in sortedVendorsData:
239
                for vData in sortedVendorsData:
220
                    lowestSp = vData['sellingPrice']
240
                    lowestSp = vData['sellingPrice']
221
                    break
241
                    break
222
                if lowestSp > 0:
242
                if lowestSp > 0:
223
                    inStock = 1
243
                    inStock = 1
-
 
244
                buyBoxPrice = buyBoxInfo[0].get('sellingPrice')
224
            print lowestSp
245
            print lowestSp
225
            print inStock
246
            print inStock
-
 
247
            if buyBoxPrice is not None and buyBoxPrice == lowestSp:
-
 
248
                isBuyBox = 1
226
            if lowestSp > 0:
249
            if lowestSp > 0:
227
                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)
250
                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,'buyBoxFlag':isBuyBox}}, multi=True)
228
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
251
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
229
            else:
252
            else:
230
                lowestSp = data['available_price']
253
                lowestSp = data['available_price']
231
                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)
254
                get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock,'priceUpdatedOn':to_java_date(now),'buyBoxFlag':isBuyBox}}, multi=True)
232
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
255
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
233
                
256
                
234
            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']}
257
            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']}
235
            
258
            
236
        except Exception as e:
259
        except Exception as e:
Line 243... Line 266...
243
 
266
 
244
def populateNegativeDeals():
267
def populateNegativeDeals():
245
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
268
    negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
246
    mc.set("negative_deals", negativeDeals, 600)
269
    mc.set("negative_deals", negativeDeals, 600)
247
 
270
 
-
 
271
def recomputePoints(item, deal):
-
 
272
    try:
-
 
273
        nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
-
 
274
    except:
-
 
275
        print traceback.print_exc()
-
 
276
        nlcPoints = deal['nlcPoints']
-
 
277
    if item['manualDealThresholdPrice'] >= deal['available_price']:
-
 
278
        dealPoints = item['dealPoints']
-
 
279
    else:
-
 
280
        dealPoints = 0
-
 
281
    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']}})
-
 
282
 
248
def recomputeDeal(skuBundleId):
283
def recomputeDeal(skuBundleId):
249
    """Lets recompute deal for this bundle"""
284
    """Lets recompute deal for this bundle"""
250
    print "Recomputing for bundleId",skuBundleId
285
    print "Recomputing for bundleId",skuBundleId
251
    
286
    
252
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
287
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))