Subversion Repositories SmartDukaan

Rev

Rev 18326 | Blame | Compare with Previous | Last modification | View Log | RSS feed

from dtr.utils.utils import fetchResponseUsingProxy
from sys import exit
import json
import traceback
import datetime



headers = {
           'Browser-Name': 'Chrome',
           '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)',
           'Host': 'mobileapi.flipkart.net'
        }


class FlipkartProductPageScraper:
    def __init__(self):
        self.count_trials = 0
        self.redirectCount = 0
    
    def read(self, identifier):
        response_data = ""
        self.fsn = identifier.upper().strip()
        url = "http://mobileapi.flipkart.net/2/discover/productInfo/0?pids=%s"%(self.fsn)
        print url
        try:
            
            """quick fix,need to add it conf""" 
            
            response_data = fetchResponseUsingProxy(url, headers, livePricing=None, proxy=True, flipkart=False)
            print "Fetched response from flipkart for %s" %(url)
            #redirect_url = response.url

        except Exception as e:
            traceback.print_exc()
            print 'ERROR: ', e
            print 'Retrying'
            self.count_trials += 1

            if self.count_trials < 3:
                return self.read(url)

        self.response_data=response_data
        return self.parse()

    def parse(self):
        input_json = json.loads(self.response_data)
        inStock = not input_json['RESPONSE']['productInfo'][self.fsn]['availabilityDetails']['product.isOOS']
        preferred_seller = input_json['RESPONSE']['productInfo'][self.fsn]['preferredListingId']
        buyBoxPrice = 0
        lowestSp = 0
        try:
            for priceMap in  input_json['RESPONSE']['productInfo'][self.fsn]['priceWidget']['prices']:
                if priceMap['isFinal']:
                    buyBoxPrice =  float(priceMap['price'])
        except:
            print "Exception in %s "%(self.fsn)
            traceback.print_exc()
        for x in (input_json['RESPONSE']['productInfo'][self.fsn]['marketplace']):
#             print x['marketplace.listId'],
#             print '\t',
#             print x['product.availabilityDetails']['product.isOOS'],
#             print '\t',
#             print x['product.selling_price'],
#             print '\t',
#             print x['marketplace.seller.shippingCharge'],
#             print '\t',
#             print x['seller.displayName']
            
            if not x['product.availabilityDetails']['product.isOOS']:
                if lowestSp == 0 or lowestSp > x['product.selling_price']:
                    lowestSp = x['product.selling_price']
                if buyBoxPrice ==0:
                    if x['marketplace.listId'] == preferred_seller:
                        buyBoxPrice = x['product.selling_price']
        
        if buyBoxPrice < lowestSp:
            lowestSp = buyBoxPrice            
        return {'lowestSp':lowestSp,'inStock':int(inStock),'buyBoxPrice':buyBoxPrice} 
            

def main():
    print datetime.datetime.now()
    scraper = FlipkartProductPageScraper()
    print scraper.read('TABE2FNWDAXHEQTA')
    print datetime.datetime.now()


if __name__ == '__main__':
    main()