Subversion Repositories SmartDukaan

Rev

Rev 13844 | Go to most recent revision | Details | 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
5
from datetime import datetime
6
import time
7
 
8
con = None
9
now = datetime.now()
10
print to_java_date(now)
11
 
12
headers = { 
13
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
14
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
15
            'Accept-Language' : 'en-US,en;q=0.8',                     
16
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
17
        }
18
 
19
def get_mongo_connection(host='localhost', port=27017):
20
    global con
21
    if con is None:
22
        print "Establishing connection %s host and port %d" %(host,port)
23
        try:
24
            con = pymongo.MongoClient(host, port)
25
        except Exception, e:
26
            print e
27
            return None
28
    return con
29
 
30
def updatePrices():
31
    snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3}))
32
    for data in snapdealBestSellers:
33
        print data['identifier']
34
        if data['identifier'] is None or len(data['identifier'])==0:
35
            print "continue"
36
            continue
37
        url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
38
        print url
39
        time.sleep(1)
40
        req = urllib2.Request(url,headers=headers)
41
        response = urllib2.urlopen(req)
42
        json_input = response.read()
43
        vendorInfo = json.loads(json_input)
44
        lowestOfferPrice = 0
45
        instock = 0
46
        for vendor in vendorInfo:
47
            lowestOfferPrice = float(vendor['sellingPrice'])
48
            stock = vendor['buyableInventory']
49
            if stock > 0 and lowestOfferPrice > 0:
50
                instock = 1
51
                break
52
 
53
        print lowestOfferPrice
54
        print instock
55
        print stock
56
        print "*************"
57
        if instock  == 1:
58
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
59
        else:
60
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)
61
 
62
def main():
63
    updatePrices()
64
 
65
if __name__=='__main__':
66
    main()