Subversion Repositories SmartDukaan

Rev

Rev 13864 | Rev 13918 | 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
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':source_id}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':0}}, multi=True)
49
            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']}
50
        except:
51
            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']}
52
 
53
    elif source_id ==3:
54
        try:
55
            if data['identifier'] is None or len(data['identifier'])==0:
56
                return {}
57
            url="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])
58
            req = urllib2.Request(url,headers=headers)
59
            response = urllib2.urlopen(req)
60
            json_input = response.read()
61
            vendorInfo = json.loads(json_input)
62
            lowestOfferPrice = 0
63
            inStock = 0
64
            for vendor in vendorInfo:
65
                lowestOfferPrice = float(vendor['sellingPrice'])
66
                stock = vendor['buyableInventory']
67
                if stock > 0 and lowestOfferPrice > 0:
68
                    inStock = 1
69
                    break
70
 
71
            print lowestOfferPrice
72
            print inStock
73
            print stock
74
            print "*************"
75
            if inStock  == 1:
13852 kshitij.so 76
                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 77
            else:
13852 kshitij.so 78
                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 79
            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']}
80
        except:
81
            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']}
82
 
83
    elif source_id == 2:
84
        try:
85
            if data['identifier'] is None or len(data['identifier'])==0:
86
                return {}
87
            url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
88
            vendorsData = scraperFk.read(url)
89
            sortedVendorsData = []
90
            sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
91
            print "data",sortedVendorsData
92
            lowestSp, iterator = (0,)*2
93
            for vData in sortedVendorsData:
94
                if iterator == 0:
95
                    lowestSp = vData['sellingPrice']
96
                break
97
            if lowestSp > 0:
98
                inStock = 1
99
            print lowestSp
100
            print inStock
101
            if lowestSp > 0:
102
                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)
103
            else:
104
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
105
            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']}
106
        except:
107
            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']}
108
    else:
109
        return {}
110
 
111
 
112
 
113
def getLatestPrice(skuBundleId, source_id):
114
    temp = []
115
    itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
116
    for item in itemIds:
117
        temp.append(returnLatestPrice(item, source_id))
118
    return temp
119
 
13864 kshitij.so 120
def getLatestPriceById(id):
121
    item = list(get_mongo_connection().Catalog.MasterData.find({'_id':id}))
13866 kshitij.so 122
    return returnLatestPrice(item[0], item[0]['source_id'])
13864 kshitij.so 123
 
13829 kshitij.so 124
 
125
 
13864 kshitij.so 126
 
13829 kshitij.so 127
def main():
13866 kshitij.so 128
    print getLatestPriceById(7)
13829 kshitij.so 129
 
130
if __name__=='__main__':
131
    main()