Subversion Repositories SmartDukaan

Rev

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

Rev 13877 Rev 13917
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
5
from datetime import datetime
5
from datetime import datetime, timedelta
6
import time
6
import time
7
 
7
 
8
con = None
8
con = None
9
now = datetime.now()
9
now = datetime.now()
10
print to_java_date(now)
10
print to_java_date(now)
Line 32... Line 32...
32
    for data in snapdealBestSellers:
32
    for data in snapdealBestSellers:
33
        print data['identifier']
33
        print data['identifier']
34
        if data['identifier'] is None or len(data['identifier'].strip())==0:
34
        if data['identifier'] is None or len(data['identifier'].strip())==0:
35
            print "continue"
35
            print "continue"
36
            continue
36
            continue
-
 
37
        
-
 
38
        try:
-
 
39
            if data['updatedOn'] + timedelta(minutes=5) > to_java_date(now):
-
 
40
                print "sku id is already updated",data['_id'] 
-
 
41
                continue
-
 
42
        except:
-
 
43
            pass
-
 
44
        
37
        url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
45
        url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
38
        print url
46
        print url
39
        time.sleep(1)
47
        time.sleep(1)
40
        lowestOfferPrice = 0
48
        lowestOfferPrice = 0
41
        instock = 0
49
        instock = 0
Line 54... Line 62...
54
        print lowestOfferPrice
62
        print lowestOfferPrice
55
        print instock
63
        print instock
56
        print stock
64
        print stock
57
        print "*************"
65
        print "*************"
58
        if instock  == 1:
66
        if instock  == 1:
59
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':3}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
67
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
-
 
68
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
60
        else:
69
        else:
61
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':3}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
70
            get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
-
 
71
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
-
 
72
        
-
 
73
        try:
-
 
74
            recomputeDeal(data['skuBundleId'])
-
 
75
        except:
-
 
76
            print "Unable to compute deal for ",data['skuBundleId']
-
 
77
            
-
 
78
        
-
 
79
def recomputeDeal(skuBundleId):
-
 
80
    """Lets recompute deal for this bundle"""
-
 
81
    print "Recomputing for bundleId",skuBundleId
62
 
82
    
-
 
83
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
-
 
84
    bestPrice = float("inf")
-
 
85
    bestOne = None
-
 
86
    bestSellerPoints = 0
-
 
87
    toUpdate = []
-
 
88
    for similarItem in similarItems:
-
 
89
        if similarItem['in_stock'] == 0:
-
 
90
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
-
 
91
            continue
-
 
92
        if similarItem['available_price'] < bestPrice:
-
 
93
            bestOne = similarItem
-
 
94
            bestPrice = similarItem['available_price']
-
 
95
            bestSellerPoints = similarItem['bestSellerPoints']
-
 
96
        elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
-
 
97
            bestOne = similarItem
-
 
98
            bestPrice = similarItem['available_price']
-
 
99
            bestSellerPoints = similarItem['bestSellerPoints']
-
 
100
        else:
-
 
101
            pass
-
 
102
    if bestOne is not None:
-
 
103
        for similarItem in similarItems:
-
 
104
            toUpdate.append(similarItem['_id'])
-
 
105
        toUpdate.remove(bestOne['_id'])
-
 
106
        get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
-
 
107
    if len(toUpdate) > 0:
-
 
108
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
-
 
109
        
63
def main():
110
def main():
64
    updatePrices()
111
    updatePrices()
65
 
112
 
66
if __name__=='__main__':
113
if __name__=='__main__':
67
    main()
114
    main()
68
115