Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
17263 kshitij.so 1
from dtr.utils.utils import fetchResponseUsingProxy
14122 kshitij.so 2
from sys import exit
3
import json
15265 kshitij.so 4
import traceback
15950 kshitij.so 5
import datetime
14122 kshitij.so 6
 
14746 kshitij.so 7
 
15950 kshitij.so 8
 
17263 kshitij.so 9
headers = {
10
           'Browser-Name': 'Chrome',
11
           'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; A0001 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36 FKUA/Retail/550900/Android/Mobile (OnePlus/A0001)',
12
           'Host': 'mobileapi.flipkart.net'
14122 kshitij.so 13
        }
14
 
15950 kshitij.so 15
 
14122 kshitij.so 16
class FlipkartProductPageScraper:
17
    def __init__(self):
18
        self.count_trials = 0
19
        self.redirectCount = 0
20
 
17263 kshitij.so 21
    def read(self, identifier):
14122 kshitij.so 22
        response_data = ""
17263 kshitij.so 23
        self.fsn = identifier.upper().strip()
24
        url = "http://mobileapi.flipkart.net/2/discover/productInfo/0?pids=%s"%(self.fsn)
14122 kshitij.so 25
        try:
14538 kshitij.so 26
 
27
            """quick fix,need to add it conf""" 
28
 
17434 kshitij.so 29
            response_data = fetchResponseUsingProxy(url, headers, livePricing=None, proxy=True, flipkart=False)                
14122 kshitij.so 30
            print "Fetched response from flipkart for %s" %(url)
14538 kshitij.so 31
            #redirect_url = response.url
14122 kshitij.so 32
 
33
        except Exception as e:
14746 kshitij.so 34
            traceback.print_exc()
14122 kshitij.so 35
            print 'ERROR: ', e
36
            print 'Retrying'
37
            self.count_trials += 1
38
 
39
            if self.count_trials < 3:
40
                return self.read(url)
41
 
42
        self.response_data=response_data
17263 kshitij.so 43
        return self.parse()
44
 
45
    def parse(self):
46
        input_json = json.loads(self.response_data)
47
        inStock = not input_json['RESPONSE']['productInfo'][self.fsn]['availabilityDetails']['product.isOOS']
48
        preferred_seller = input_json['RESPONSE']['productInfo'][self.fsn]['preferredListingId']
49
        buyBoxPrice = 0
50
        lowestSp = 0
51
        for x in (input_json['RESPONSE']['productInfo'][self.fsn]['marketplace']):
52
#             print x['marketplace.listId'],
53
#             print '\t',
54
#             print x['product.availabilityDetails']['product.isOOS'],
55
#             print '\t',
56
#             print x['product.selling_price'],
57
#             print '\t',
58
#             print x['marketplace.seller.shippingCharge'],
59
#             print '\t',
60
#             print x['seller.displayName']
15950 kshitij.so 61
 
17263 kshitij.so 62
            if not x['product.availabilityDetails']['product.isOOS']:
63
                if lowestSp == 0 or lowestSp > x['product.selling_price']:
64
                    lowestSp = x['product.selling_price']
65
 
66
                if x['marketplace.listId'] == preferred_seller:
67
                    buyBoxPrice = x['product.selling_price']
68
 
69
 
70
        return {'lowestSp':lowestSp,'inStock':int(inStock),'buyBoxPrice':buyBoxPrice} 
71
 
72
 
73
def main():
15950 kshitij.so 74
    print datetime.datetime.now()
14122 kshitij.so 75
    scraper = FlipkartProductPageScraper()
17434 kshitij.so 76
    print scraper.read('MOBE9QXZK8VMMNDV')
15950 kshitij.so 77
    print datetime.datetime.now()
17263 kshitij.so 78
 
79
 
80
if __name__ == '__main__':
81
    main()