Subversion Repositories SmartDukaan

Rev

Rev 13980 | Rev 14132 | 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():
14127 kshitij.so 29
    snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
13828 kshitij.so 30
    for data in snapdealBestSellers:
14127 kshitij.so 31
        if data['source_id']!=3:
32
            continue
13828 kshitij.so 33
        print data['identifier']
13847 kshitij.so 34
        if data['identifier'] is None or len(data['identifier'].strip())==0:
13828 kshitij.so 35
            print "continue"
36
            continue
13917 kshitij.so 37
 
38
        try:
14127 kshitij.so 39
            if data['priceUpdatedOn'] > to_java_date(datetime.now() - timedelta(minutes=5)):
13917 kshitij.so 40
                print "sku id is already updated",data['_id'] 
41
                continue
42
        except:
43
            pass
44
 
13877 kshitij.so 45
        url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'].strip())
13828 kshitij.so 46
        print url
47
        time.sleep(1)
13844 kshitij.so 48
        lowestOfferPrice = 0
49
        instock = 0
13828 kshitij.so 50
        req = urllib2.Request(url,headers=headers)
51
        response = urllib2.urlopen(req)
52
        json_input = response.read()
13844 kshitij.so 53
        if len(json_input) > 0:
54
            vendorInfo = json.loads(json_input)
55
            for vendor in vendorInfo:
56
                lowestOfferPrice = float(vendor['sellingPrice'])
57
                stock = vendor['buyableInventory']
58
                if stock > 0 and lowestOfferPrice > 0:
59
                    instock = 1
60
                    break
61
 
13828 kshitij.so 62
        print lowestOfferPrice
63
        print instock
64
        print stock
65
        print "*************"
66
        if instock  == 1:
14127 kshitij.so 67
            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)
13917 kshitij.so 68
            get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestOfferPrice , 'in_stock':instock}}, multi=True)
13828 kshitij.so 69
        else:
14127 kshitij.so 70
            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)
13917 kshitij.so 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
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:
13974 kshitij.so 89
        if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
13917 kshitij.so 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
 
13828 kshitij.so 110
def main():
111
    updatePrices()
112
 
113
if __name__=='__main__':
114
    main()