Rev 13847 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import urllib2import simplejson as jsonimport pymongofrom dtr.utils.utils import to_java_datefrom datetime import datetimeimport timecon = Nonenow = datetime.now()print to_java_date(now)headers = {'User-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11','Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language' : 'en-US,en;q=0.8','Accept-Charset' : 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'}def get_mongo_connection(host='localhost', port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef updatePrices():snapdealBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0},'source_id':3}))for data in snapdealBestSellers:print data['identifier']if data['identifier'] is None or len(data['identifier'])==0:print "continue"continueurl="http://www.snapdeal.com/acors/json/gvbps?supc=%s&catId=175&sort=sellingPrice"%(data['identifier'])print urltime.sleep(1)req = urllib2.Request(url,headers=headers)response = urllib2.urlopen(req)json_input = response.read()vendorInfo = json.loads(json_input)lowestOfferPrice = 0instock = 0for vendor in vendorInfo:lowestOfferPrice = float(vendor['sellingPrice'])stock = vendor['buyableInventory']if stock > 0 and lowestOfferPrice > 0:instock = 1breakprint lowestOfferPriceprint instockprint stockprint "*************"if instock == 1:get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'available_price':lowestOfferPrice,'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)else:get_mongo_connection().Catalog.MasterData.update({'identifier':data['identifier'].strip()}, {'$set' : {'updatedOn':to_java_date(now),'in_stock':instock}}, multi=True)def main():updatePrices()if __name__=='__main__':main()