Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
13829 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
from operator import itemgetter
7
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
13830 kshitij.so 8
from dtr.utils import FlipkartScraper
13829 kshitij.so 9
 
10
con = None
11
now = datetime.now()
12
SOURCE_MAP = {'AMAZON':1,'FLIPKART':2,'SNAPDEAL':3}
13
scraperFk = FlipkartScraper.FlipkartScraper()
14
scraperAmazon = AmazonScraper()
15
 
16
headers = { 
17
           'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
18
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',      
19
            'Accept-Language' : 'en-US,en;q=0.8',                     
20
            'Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'
21
        }
22
 
23
def get_mongo_connection(host='localhost', port=27017):
24
    global con
25
    if con is None:
26
        print "Establishing connection %s host and port %d" %(host,port)
27
        try:
28
            con = pymongo.MongoClient(host, port)
29
        except Exception, e:
30
            print e
31
            return None
32
    return con
33
 
34
def returnLatestPrice(data, source_id):
35
    if source_id == 1:
36
        try:
37
            if data['identifier'] is None or len(data['identifier'])==0:
38
                return {}
39
            url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
40
            lowestPrice = 0.0
41
            lowestPrice = scraperAmazon.read(url)
42
            inStock = 0
43
            if lowestPrice > 0:
44
                inStock = 1
45
            if lowestPrice > 0:
46
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
47
            else:
48
                pass
49
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0}}, multi=True)
50
            return {'_id':data['_id'],'available_price':lowestPrice,'in_stock':inStock,'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
51
        except:
52
            return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':1,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
53
 
54
    elif source_id ==3:
55
        try:
56
            if data['identifier'] is None or len(data['identifier'])==0:
57
                return {}
58
            url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
59
            req = urllib2.Request(url,headers=headers)
60
            response = urllib2.urlopen(req)
61
            json_input = response.read()
62
            vendorInfo = json.loads(json_input)
63
            lowestOfferPrice = 0
64
            inStock = 0
65
            for vendor in vendorInfo:
66
                lowestOfferPrice = float(vendor['sellingPrice'])
67
                stock = vendor['buyableInventory']
68
                if stock > 0 and lowestOfferPrice > 0:
69
                    inStock = 1
70
                    break
71
 
72
            print lowestOfferPrice
73
            print inStock
74
            print stock
75
            print "*************"
76
            if inStock  == 1:
13852 kshitij.so 77
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
13829 kshitij.so 78
            else:
13852 kshitij.so 79
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
13829 kshitij.so 80
            return {'_id':data['_id'],'available_price':lowestOfferPrice,'in_stock':inStock,'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
81
        except:
82
            return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':3,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
83
 
84
    elif source_id == 2:
85
        try:
86
            if data['identifier'] is None or len(data['identifier'])==0:
87
                return {}
88
            url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
89
            vendorsData = scraperFk.read(url)
90
            sortedVendorsData = []
91
            sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
92
            print "data",sortedVendorsData
93
            lowestSp, iterator = (0,)*2
94
            for vData in sortedVendorsData:
95
                if iterator == 0:
96
                    lowestSp = vData['sellingPrice']
97
                break
98
            if lowestSp > 0:
99
                inStock = 1
100
            print lowestSp
101
            print inStock
102
            if lowestSp > 0:
103
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
104
            else:
105
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
106
            return {'_id':data['_id'],'available_price':lowestSp,'in_stock':inStock,'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
107
        except:
108
            return {'_id':data['_id'],'available_price':data['available_price'],'in_stock':data['inStock'],'source_id':2,'source_product_name':data['source_product_name'],'marketPlaceUrl':data['marketPlaceUrl'],'thumbnail':data['thumbnail']}
109
    else:
110
        return {}
111
 
112
 
113
 
114
def getLatestPrice(skuBundleId, source_id):
115
    temp = []
116
    itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
117
    for item in itemIds:
118
        temp.append(returnLatestPrice(item, source_id))
119
    return temp
120
 
121
 
122
 
123
def main():
124
    print getLatestPrice(1,2)
125
 
126
if __name__=='__main__':
127
    main()