Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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
5
from shop2020.model.v1.catalog.script import FlipkartScraper
13832 kshitij.so 6
from dtr.utils import AmazonPriceOnlyScraper
13828 kshitij.so 7
 
8
con = None
9
now = datetime.now()
10
scraperAmazon = AmazonPriceOnlyScraper.AmazonScraper()
11
print to_java_date(now)
12
 
13
 
14
def get_mongo_connection(host='localhost', port=27017):
15
    global con
16
    if con is None:
17
        print "Establishing connection %s host and port %d" %(host,port)
18
        try:
19
            con = pymongo.MongoClient(host, port)
20
        except Exception, e:
21
            print e
22
            return None
23
    return con
24
 
25
def scrapeAmazon():
26
    amazonBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':1}))
27
    for data in amazonBestSellers:
28
        inStock = 0
29
        print str(data['identifier'])
30
        if data['identifier'] is None or len(data['identifier'])==0:
31
            print "continue"
32
            continue
33
 
34
        url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'])
35
        print url
36
        lowestPrice = 0.0
37
        lowestPrice = scraperAmazon.read(url)
38
        print lowestPrice
39
        if lowestPrice > 0:
40
            inStock = 1
41
        print lowestPrice
42
        print inStock
43
        if lowestPrice > 0:
44
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier']}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
45
        else:
46
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier']}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
47
        print "+++++++++++++++++++++++++++"
48
 
49
def main():
50
    scrapeAmazon()
51
 
52
if __name__=='__main__':
53
    main()