Subversion Repositories SmartDukaan

Rev

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

Rev 13871 Rev 13914
Line 1... Line 1...
1
import pymongo
1
import pymongo
2
from dtr.utils.utils import to_java_date
2
from dtr.utils.utils import to_java_date
3
from datetime import datetime
3
from datetime import datetime, timedelta
4
from operator import itemgetter
-
 
5
from shop2020.model.v1.catalog.script import FlipkartScraper
-
 
6
from dtr.utils import AmazonPriceOnlyScraper
4
from dtr.utils import AmazonPriceOnlyScraper
7
 
5
 
8
con = None
6
con = None
9
now = datetime.now()
7
now = datetime.now()
10
scraperAmazon = AmazonPriceOnlyScraper.AmazonScraper()
8
scraperAmazon = AmazonPriceOnlyScraper.AmazonScraper()
Line 29... Line 27...
29
        print str(data['identifier'])
27
        print str(data['identifier'])
30
        if data['identifier'] is None or len(data['identifier'].strip())==0:
28
        if data['identifier'] is None or len(data['identifier'].strip())==0:
31
            print "continue"
29
            print "continue"
32
            continue
30
            continue
33
        
31
        
-
 
32
        try:
-
 
33
            if data['updatedOn'] + timedelta(minutes=5) > to_java_date(now):
-
 
34
                print "sku id is already updated",data['_id'] 
-
 
35
                continue
-
 
36
        except:
-
 
37
            pass
-
 
38
        
34
        url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'].strip())
39
        url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'].strip())
35
        print url
40
        print url
36
        lowestPrice = 0.0
41
        lowestPrice = 0.0
37
        lowestPrice = scraperAmazon.read(url)
42
        lowestPrice = scraperAmazon.read(url)
38
        print lowestPrice
43
        print lowestPrice
39
        if lowestPrice > 0:
44
        if lowestPrice > 0:
40
            inStock = 1
45
            inStock = 1
41
        print lowestPrice
46
        print lowestPrice
42
        print inStock
47
        print inStock
43
        if lowestPrice > 0:
48
        if lowestPrice > 0:
44
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':1}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
49
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
-
 
50
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestPrice , 'in_stock':inStock}}, multi=True)
-
 
51
        else:
-
 
52
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
-
 
53
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
-
 
54
            
-
 
55
        try:
-
 
56
            recomputeDeal(data['skuBundleId'])
-
 
57
        except:
-
 
58
            print "Unable to compute deal for ",data['skuBundleId']
-
 
59
        
-
 
60
 
-
 
61
def recomputeDeal(skuBundleId):
-
 
62
    """Lets recompute deal for this bundle"""
-
 
63
    print "Recomputing for bundleId",skuBundleId
-
 
64
    
-
 
65
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
-
 
66
    bestPrice = float("inf")
-
 
67
    bestOne = None
-
 
68
    bestSellerPoints = 0
-
 
69
    toUpdate = []
-
 
70
    for similarItem in similarItems:
-
 
71
        if similarItem['in_stock'] == 0:
-
 
72
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
-
 
73
            continue
-
 
74
        if similarItem['available_price'] < bestPrice:
-
 
75
            bestOne = similarItem
-
 
76
            bestPrice = similarItem['available_price']
-
 
77
            bestSellerPoints = similarItem['bestSellerPoints']
-
 
78
        elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
-
 
79
            bestOne = similarItem
-
 
80
            bestPrice = similarItem['available_price']
-
 
81
            bestSellerPoints = similarItem['bestSellerPoints']
45
        else:
82
        else:
-
 
83
            pass
-
 
84
    if bestOne is not None:
-
 
85
        for similarItem in similarItems:
-
 
86
            toUpdate.append(similarItem['_id'])
-
 
87
        toUpdate.remove(bestOne['_id'])
46
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':1}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
88
        get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
47
        print "+++++++++++++++++++++++++++"
89
    if len(toUpdate) > 0:
-
 
90
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
48
 
91
 
49
def main():
92
def main():
50
    scrapeAmazon()
93
    scrapeAmazon()
51
            
94
            
52
if __name__=='__main__':
95
if __name__=='__main__':