Subversion Repositories SmartDukaan

Rev

Rev 14127 | Rev 14180 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13828 kshitij.so 1
import urllib2
2
import simplejson as json
3
import pymongo
4
from dtr.utils.utils import to_java_date
13917 kshitij.so 5
from datetime import datetime, timedelta
13828 kshitij.so 6
import time
7
 
8
con = None
9
 
10
headers = { 
11
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
12
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
13
            'Accept-Language' : 'en-US,en;q=0.8',                     
14
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
15
        }
16
 
17
def get_mongo_connection(host='localhost', port=27017):
18
    global con
19
    if con is None:
20
        print "Establishing connection %s host and port %d" %(host,port)
21
        try:
22
            con = pymongo.MongoClient(host, port)
23
        except Exception, e:
24
            print e
25
            return None
26
    return con
27
 
28
def updatePrices():
14132 kshitij.so 29
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
30
    for bestSeller in bestSellers: 
31
        snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':3}))
32
        for data in snapdealBestSellers:
33
            if data['source_id']!=3:
13917 kshitij.so 34
                continue
14132 kshitij.so 35
            print data['identifier']
36
            if data['identifier'] is None or len(data['identifier'].strip())==0:
37
                print "continue"
38
                continue
13917 kshitij.so 39
 
14132 kshitij.so 40
            try:
41
                if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
42
                    print "sku id is already updated",data['_id'] 
43
                    continue
44
            except:
45
                pass
46
 
47
            url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
48
            print url
49
            time.sleep(1)
50
            lowestOfferPrice = 0
51
            instock = 0
52
            req = urllib2.Request(url,headers=headers)
53
            response = urllib2.urlopen(req)
54
            json_input = response.read()
55
            if len(json_input) > 0:
56
                vendorInfo = json.loads(json_input)
57
                for vendor in vendorInfo:
58
                    lowestOfferPrice = float(vendor['sellingPrice'])
59
                    stock = vendor['buyableInventory']
60
                    if stock > 0 and lowestOfferPrice > 0:
61
                        instock = 1
62
                        break
63
 
64
            print lowestOfferPrice
65
            print instock
66
            print stock
67
            print "*************"
68
            if instock  == 1:
69
                get_mongo_connection().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)
70
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
71
            else:
72
                get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':instock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)
73
                get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':instock}}, multi=True)
74
 
75
            try:
76
                recomputeDeal(data['skuBundleId'])
77
            except:
78
                print "Unable to compute deal for ",data['skuBundleId']
79
 
13917 kshitij.so 80
 
81
def recomputeDeal(skuBundleId):
82
    """Lets recompute deal for this bundle"""
83
    print "Recomputing for bundleId",skuBundleId
84
 
85
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
86
    bestPrice = float("inf")
87
    bestOne = None
88
    bestSellerPoints = 0
89
    toUpdate = []
90
    for similarItem in similarItems:
13974 kshitij.so 91
        if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
13917 kshitij.so 92
            get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
93
            continue
94
        if similarItem['available_price'] < bestPrice:
95
            bestOne = similarItem
96
            bestPrice = similarItem['available_price']
97
            bestSellerPoints = similarItem['bestSellerPoints']
98
        elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
99
            bestOne = similarItem
100
            bestPrice = similarItem['available_price']
101
            bestSellerPoints = similarItem['bestSellerPoints']
102
        else:
103
            pass
104
    if bestOne is not None:
105
        for similarItem in similarItems:
106
            toUpdate.append(similarItem['_id'])
107
        toUpdate.remove(bestOne['_id'])
108
        get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
109
    if len(toUpdate) > 0:
110
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
111
 
13828 kshitij.so 112
def main():
113
    updatePrices()
114
 
115
if __name__=='__main__':
116
    main()