Subversion Repositories SmartDukaan

Rev

Rev 13830 | Go to most recent revision | Details | 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 shop2020.model.v1.catalog.script import FlipkartScraper
8
from dtr.utils.AmazonPriceOnlyScraper import AmazonScraper
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:
77
                pass
78
                #get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier']}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
79
            else:
80
                pass
81
                #get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':3}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
82
            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']}
83
        except:
84
            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']}
85
 
86
    elif source_id == 2:
87
        try:
88
            if data['identifier'] is None or len(data['identifier'])==0:
89
                return {}
90
            url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
91
            vendorsData = scraperFk.read(url)
92
            sortedVendorsData = []
93
            sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
94
            print "data",sortedVendorsData
95
            lowestSp, iterator = (0,)*2
96
            for vData in sortedVendorsData:
97
                if iterator == 0:
98
                    lowestSp = vData['sellingPrice']
99
                break
100
            if lowestSp > 0:
101
                inStock = 1
102
            print lowestSp
103
            print inStock
104
            if lowestSp > 0:
105
                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)
106
            else:
107
                get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'],'source_id':2}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
108
            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']}
109
        except:
110
            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']}
111
    else:
112
        return {}
113
 
114
 
115
 
116
def getLatestPrice(skuBundleId, source_id):
117
    temp = []
118
    itemIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId,'source_id' : source_id}))
119
    for item in itemIds:
120
        temp.append(returnLatestPrice(item, source_id))
121
    return temp
122
 
123
 
124
 
125
def main():
126
    print getLatestPrice(1,2)
127
 
128
if __name__=='__main__':
129
    main()