| 13828 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from dtr.utils.utils import to_java_date
|
|
|
3 |
from datetime import datetime
|
|
|
4 |
from operator import itemgetter
|
| 13833 |
kshitij.so |
5 |
from dtr.utils import FlipkartScraper
|
| 13828 |
kshitij.so |
6 |
|
|
|
7 |
con = None
|
|
|
8 |
now = datetime.now()
|
|
|
9 |
print to_java_date(now)
|
|
|
10 |
scraperFk = FlipkartScraper.FlipkartScraper()
|
|
|
11 |
|
|
|
12 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
13 |
global con
|
|
|
14 |
if con is None:
|
|
|
15 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
16 |
try:
|
|
|
17 |
con = pymongo.MongoClient(host, port)
|
|
|
18 |
except Exception, e:
|
|
|
19 |
print e
|
|
|
20 |
return None
|
|
|
21 |
return con
|
|
|
22 |
|
|
|
23 |
def scrapeFlipkart():
|
|
|
24 |
flipkartBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':2}))
|
|
|
25 |
for data in flipkartBestSellers:
|
|
|
26 |
inStock = 0
|
| 13841 |
kshitij.so |
27 |
retryCount = 0
|
| 13828 |
kshitij.so |
28 |
print str(data['identifier'])
|
|
|
29 |
if data['identifier'] is None or len(data['identifier'])==0:
|
|
|
30 |
print "continue"
|
|
|
31 |
continue
|
|
|
32 |
|
|
|
33 |
url = "http://www.flipkart.com/ps/%s"%(data['identifier'])
|
| 13841 |
kshitij.so |
34 |
while(retryCount < 3):
|
|
|
35 |
try:
|
|
|
36 |
vendorsData = scraperFk.read(url)
|
|
|
37 |
fetched = True
|
|
|
38 |
except Exception as e:
|
|
|
39 |
retryCount+=1
|
|
|
40 |
if retryCount == 3:
|
|
|
41 |
fetched = False
|
|
|
42 |
print e
|
|
|
43 |
if not fetched:
|
|
|
44 |
print "Unable to fetch data after multiple tries.Continue for ",data['identifier']
|
|
|
45 |
continue
|
| 13828 |
kshitij.so |
46 |
sortedVendorsData = []
|
|
|
47 |
sortedVendorsData = sorted(vendorsData, key=itemgetter('sellingPrice'))
|
|
|
48 |
print "data",sortedVendorsData
|
|
|
49 |
lowestSp, iterator = (0,)*2
|
|
|
50 |
for vData in sortedVendorsData:
|
|
|
51 |
if iterator == 0:
|
|
|
52 |
lowestSp = vData['sellingPrice']
|
|
|
53 |
break
|
|
|
54 |
if lowestSp > 0:
|
|
|
55 |
inStock = 1
|
|
|
56 |
print lowestSp
|
|
|
57 |
print inStock
|
|
|
58 |
if lowestSp > 0:
|
|
|
59 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
|
|
60 |
else:
|
|
|
61 |
get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
|
|
|
62 |
print "+++++++++++++++++++++++++++"
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
def main():
|
|
|
68 |
scrapeFlipkart()
|
|
|
69 |
|
|
|
70 |
if __name__=='__main__':
|
|
|
71 |
main()
|