Subversion Repositories SmartDukaan

Rev

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

import pymongo
from dtr.utils.utils import to_java_date
from datetime import datetime
from operator import itemgetter
from shop2020.model.v1.catalog.script import FlipkartScraper
from dtr.utils import AmazonPriceOnlyScraper

con = None
now = datetime.now()
scraperAmazon = AmazonPriceOnlyScraper.AmazonScraper()
print to_java_date(now)


def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

def scrapeAmazon():
    amazonBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':1}))
    for data in amazonBestSellers:
        inStock = 0
        print str(data['identifier'])
        if data['identifier'] is None or len(data['identifier'].strip())==0:
            print "continue"
            continue
        
        url = "http://www.amazon.in/gp/offer-listing/%s/ref=olp_sort_ps"%(data['identifier'].strip())
        print url
        lowestPrice = 0.0
        lowestPrice = scraperAmazon.read(url)
        print lowestPrice
        if lowestPrice > 0:
            inStock = 1
        print lowestPrice
        print inStock
        if lowestPrice > 0:
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':1}, {'$set' : {'available_price':lowestPrice,'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
        else:
            get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip(),'source_id':1}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':inStock}}, multi=True)
        print "+++++++++++++++++++++++++++"

def main():
    scrapeAmazon()
            
if __name__=='__main__':
    main()