| 13828 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from dtr.utils.utils import to_java_date
|
| 13915 |
kshitij.so |
3 |
from datetime import datetime, timedelta
|
| 13828 |
kshitij.so |
4 |
from operator import itemgetter
|
| 14123 |
kshitij.so |
5 |
from dtr.utils import FlipkartScraper,NewFlipkartScraper
|
| 13828 |
kshitij.so |
6 |
|
|
|
7 |
con = None
|
|
|
8 |
now = datetime.now()
|
|
|
9 |
print to_java_date(now)
|
|
|
10 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
| 14123 |
kshitij.so |
11 |
scraperProductPage = NewFlipkartScraper.FlipkartProductPageScraper()
|
| 13828 |
kshitij.so |
12 |
|
|
|
13 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
14 |
global con
|
|
|
15 |
if con is None:
|
|
|
16 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
17 |
try:
|
|
|
18 |
con = pymongo.MongoClient(host, port)
|
|
|
19 |
except Exception, e:
|
|
|
20 |
print e
|
|
|
21 |
return None
|
|
|
22 |
return con
|
|
|
23 |
|
|
|
24 |
def scrapeFlipkart():
|
|
|
25 |
flipkartBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':2}))
|
|
|
26 |
for data in flipkartBestSellers:
|
|
|
27 |
inStock = 0
|
| 13841 |
kshitij.so |
28 |
retryCount = 0
|
| 13828 |
kshitij.so |
29 |
print str(data['identifier'])
|
| 13845 |
kshitij.so |
30 |
if data['identifier'] is None or len(data['identifier'].strip())==0:
|
| 13828 |
kshitij.so |
31 |
print "continue"
|
|
|
32 |
continue
|
|
|
33 |
|
| 13915 |
kshitij.so |
34 |
try:
|
| 13979 |
kshitij.so |
35 |
if data['priceUpdatedOn'] > to_java_date(now - timedelta(minutes=5)):
|
| 13915 |
kshitij.so |
36 |
print "sku id is already updated",data['_id']
|
|
|
37 |
continue
|
|
|
38 |
except:
|
|
|
39 |
pass
|
|
|
40 |
|
| 14123 |
kshitij.so |
41 |
|
|
|
42 |
lowestSp = 0
|
|
|
43 |
inStock = 0
|
|
|
44 |
try:
|
|
|
45 |
if data['marketPlaceUrl']!="" or data['marketPlaceUrl'] !="http://www.flipkart.com/ps/%s"%(data['identifier']):
|
|
|
46 |
result = scraperProductPage.read(data['marketPlaceUrl'])
|
|
|
47 |
if result.get('lowestPrice')!=0:
|
|
|
48 |
lowestSp = result.get('lowestPrice')
|
|
|
49 |
inStock = result.get('inStock')
|
|
|
50 |
except:
|
|
|
51 |
print "Unable to scrape product page ",data['identifier']
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
if lowestSp == 0:
|
|
|
55 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'].strip())
|
|
|
56 |
while(retryCount < 3):
|
|
|
57 |
try:
|
|
|
58 |
vendorsData = scraperFk.read(url)
|
|
|
59 |
fetched = True
|
|
|
60 |
break
|
|
|
61 |
except Exception as e:
|
|
|
62 |
print "***Retry count ",retryCount
|
|
|
63 |
retryCount+=1
|
|
|
64 |
if retryCount == 3:
|
|
|
65 |
fetched = False
|
|
|
66 |
print e
|
|
|
67 |
if not fetched:
|
|
|
68 |
print "Unable to fetch data after multiple tries.Continue for ",data['identifier']
|
|
|
69 |
continue
|
|
|
70 |
|
|
|
71 |
sortedVendorsData = []
|
|
|
72 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
|
|
73 |
print "data",sortedVendorsData
|
|
|
74 |
lowestSp, iterator = (0,)*2
|
|
|
75 |
for vData in sortedVendorsData:
|
|
|
76 |
if iterator == 0:
|
|
|
77 |
lowestSp = vData['sellingPrice']
|
| 13848 |
kshitij.so |
78 |
break
|
| 14123 |
kshitij.so |
79 |
if lowestSp > 0:
|
|
|
80 |
inStock = 1
|
| 13828 |
kshitij.so |
81 |
print lowestSp
|
|
|
82 |
print inStock
|
|
|
83 |
if lowestSp > 0:
|
| 13974 |
kshitij.so |
84 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'priceUpdatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
| 13915 |
kshitij.so |
85 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock}}, multi=True)
|
| 13828 |
kshitij.so |
86 |
else:
|
| 13979 |
kshitij.so |
87 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock,'priceUpdatedOn':to_java_date(now)}}, multi=True)
|
| 13915 |
kshitij.so |
88 |
get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock}}, multi=True)
|
| 13828 |
kshitij.so |
89 |
|
| 13915 |
kshitij.so |
90 |
try:
|
|
|
91 |
recomputeDeal(data['skuBundleId'])
|
|
|
92 |
except:
|
|
|
93 |
print "Unable to compute deal for ",data['skuBundleId']
|
| 13828 |
kshitij.so |
94 |
|
| 13915 |
kshitij.so |
95 |
def recomputeDeal(skuBundleId):
|
|
|
96 |
"""Lets recompute deal for this bundle"""
|
|
|
97 |
print "Recomputing for bundleId",skuBundleId
|
|
|
98 |
|
|
|
99 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
100 |
bestPrice = float("inf")
|
|
|
101 |
bestOne = None
|
|
|
102 |
bestSellerPoints = 0
|
|
|
103 |
toUpdate = []
|
|
|
104 |
for similarItem in similarItems:
|
| 13974 |
kshitij.so |
105 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price']:
|
| 13915 |
kshitij.so |
106 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
107 |
continue
|
|
|
108 |
if similarItem['available_price'] < bestPrice:
|
|
|
109 |
bestOne = similarItem
|
|
|
110 |
bestPrice = similarItem['available_price']
|
|
|
111 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
112 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
113 |
bestOne = similarItem
|
|
|
114 |
bestPrice = similarItem['available_price']
|
|
|
115 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
116 |
else:
|
|
|
117 |
pass
|
|
|
118 |
if bestOne is not None:
|
|
|
119 |
for similarItem in similarItems:
|
|
|
120 |
toUpdate.append(similarItem['_id'])
|
|
|
121 |
toUpdate.remove(bestOne['_id'])
|
|
|
122 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
123 |
if len(toUpdate) > 0:
|
|
|
124 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
|
|
125 |
|
| 13828 |
kshitij.so |
126 |
def main():
|
|
|
127 |
scrapeFlipkart()
|
|
|
128 |
|
|
|
129 |
if __name__=='__main__':
|
|
|
130 |
main()
|