| 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)
|
| 18326 |
kshitij.so |
25 |
print url
|
| 14122 |
kshitij.so |
26 |
try:
|
| 14538 |
kshitij.so |
27 |
|
|
|
28 |
"""quick fix,need to add it conf"""
|
|
|
29 |
|
| 18327 |
kshitij.so |
30 |
response_data = fetchResponseUsingProxy(url, headers, livePricing=None, proxy=True, flipkart=False)
|
| 14122 |
kshitij.so |
31 |
print "Fetched response from flipkart for %s" %(url)
|
| 14538 |
kshitij.so |
32 |
#redirect_url = response.url
|
| 14122 |
kshitij.so |
33 |
|
|
|
34 |
except Exception as e:
|
| 14746 |
kshitij.so |
35 |
traceback.print_exc()
|
| 14122 |
kshitij.so |
36 |
print 'ERROR: ', e
|
|
|
37 |
print 'Retrying'
|
|
|
38 |
self.count_trials += 1
|
|
|
39 |
|
|
|
40 |
if self.count_trials < 3:
|
|
|
41 |
return self.read(url)
|
|
|
42 |
|
|
|
43 |
self.response_data=response_data
|
| 17263 |
kshitij.so |
44 |
return self.parse()
|
|
|
45 |
|
|
|
46 |
def parse(self):
|
|
|
47 |
input_json = json.loads(self.response_data)
|
|
|
48 |
inStock = not input_json['RESPONSE']['productInfo'][self.fsn]['availabilityDetails']['product.isOOS']
|
|
|
49 |
preferred_seller = input_json['RESPONSE']['productInfo'][self.fsn]['preferredListingId']
|
|
|
50 |
buyBoxPrice = 0
|
|
|
51 |
lowestSp = 0
|
| 18326 |
kshitij.so |
52 |
try:
|
|
|
53 |
for priceMap in input_json['RESPONSE']['productInfo'][self.fsn]['priceWidget']['prices']:
|
|
|
54 |
if priceMap['isFinal']:
|
|
|
55 |
buyBoxPrice = float(priceMap['price'])
|
|
|
56 |
except:
|
| 18327 |
kshitij.so |
57 |
print "Exception in %s "%(self.fsn)
|
| 18326 |
kshitij.so |
58 |
traceback.print_exc()
|
| 17263 |
kshitij.so |
59 |
for x in (input_json['RESPONSE']['productInfo'][self.fsn]['marketplace']):
|
|
|
60 |
# print x['marketplace.listId'],
|
|
|
61 |
# print '\t',
|
|
|
62 |
# print x['product.availabilityDetails']['product.isOOS'],
|
|
|
63 |
# print '\t',
|
|
|
64 |
# print x['product.selling_price'],
|
|
|
65 |
# print '\t',
|
|
|
66 |
# print x['marketplace.seller.shippingCharge'],
|
|
|
67 |
# print '\t',
|
|
|
68 |
# print x['seller.displayName']
|
| 15950 |
kshitij.so |
69 |
|
| 17263 |
kshitij.so |
70 |
if not x['product.availabilityDetails']['product.isOOS']:
|
|
|
71 |
if lowestSp == 0 or lowestSp > x['product.selling_price']:
|
|
|
72 |
lowestSp = x['product.selling_price']
|
| 18326 |
kshitij.so |
73 |
if buyBoxPrice ==0:
|
|
|
74 |
if x['marketplace.listId'] == preferred_seller:
|
|
|
75 |
buyBoxPrice = x['product.selling_price']
|
|
|
76 |
|
|
|
77 |
if buyBoxPrice < lowestSp:
|
|
|
78 |
lowestSp = buyBoxPrice
|
| 17263 |
kshitij.so |
79 |
return {'lowestSp':lowestSp,'inStock':int(inStock),'buyBoxPrice':buyBoxPrice}
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
def main():
|
| 15950 |
kshitij.so |
83 |
print datetime.datetime.now()
|
| 14122 |
kshitij.so |
84 |
scraper = FlipkartProductPageScraper()
|
| 18327 |
kshitij.so |
85 |
print scraper.read('TABE2FNWDAXHEQTA')
|
| 15950 |
kshitij.so |
86 |
print datetime.datetime.now()
|
| 17263 |
kshitij.so |
87 |
|
|
|
88 |
|
|
|
89 |
if __name__ == '__main__':
|
|
|
90 |
main()
|