Rev 17996 | Rev 20286 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pysolrimport pymongoimport jsonimport osimport optparseimport shutilimport tracebackcon=NonefilePath = '/tmp/autosuggest.json'destFilePath = '/home/kshitij/'categoryMap = {3:'Mobiles',5:'Tablets',6:'Accessories'}SOURCE_MAP = {1:'AMAZON',2:'FLIPKART',3:'SNAPDEAL',4:'SAHOLIC',5:"SHOPCLUES.COM",6:"PAYTM.COM",7:"HOMESHOP18.COM"}xstr = lambda s: s or ""parser = optparse.OptionParser()parser.add_option("-m", "--m", dest="mongoHost",default="localhost",type="string", help="The HOST where the mongo server is running",metavar="HOST")parser.add_option("-s", "--s", dest="server",default="localhost",type="string", help="The HOST where the mongo server is running",metavar="HOST")(options, args) = parser.parse_args()synonymsMap = {}modelNameSynonymMap = {}class __SkuInfo:def __init__(self, id, skuBundleId, brand, model_name, source_product_name, category_id, category, available_price, mrp, in_stock, \source_id, store, title, thumbnail, priceUpdatedOn, brand_synonyms, model_name_synonyms):self.id = idself.skuBundleId = skuBundleIdself.brand = brandself.model_name = model_nameself.source_product_name = source_product_nameself.category_id = category_idself.category = categoryself.available_price = available_priceself.mrp = mrpself.in_stock = in_stockself.source_id = source_idself.store = storeself.title= titleself.thumbnail = thumbnailself.priceUpdatedOn = priceUpdatedOnself.brand_synonyms = brand_synonymsself.model_name_synonyms = model_name_synonymssolr = pysolr.Solr("http://104.200.25.40:8080/solr/", timeout=10)def pushData():populateBrandSynonymMap()populateModelNameSynonyms()l = []exception = []catalogMap = {}items = get_mongo_connection(options.mongoHost).Catalog.MasterData.find({'source_id' : { "$in": SOURCE_MAP.keys() } })print items.count()for item in items:try:title = xstr(item['brand'])+" "+xstr(item['model_name'])except:exception.append(item['_id'])continueif not catalogMap.has_key(str(item['skuBundleId'])+" "+title):catalogMap[str(item['skuBundleId'])+" "+title] = [title]try:s_info = __SkuInfo(str(item['_id']),int(item['skuBundleId']),(item['brand']),(item['model_name']),(item['source_product_name']),int(item['category_id']),categoryMap.get(item['category_id']),float(item['available_price']),float(item['mrp']),item['in_stock'], \int(item['source_id']),(item['source']),title,(item['thumbnail']), long(item['priceUpdatedOn']), synonymsMap.get(item['brand'].upper()), modelNameSynonymMap.get(item['skuBundleId']))except:traceback.print_exc()continuel.append(s_info.__dict__)solr.add(l,boost={'source_product_name': '2.0',})for x in exception:print xautoSuggestList = []for v in catalogMap.itervalues():autoSuggestList.append({'val':v[0]})PATH = os.path.expanduser(filePath)if os.path.isfile(PATH):try:os.remove(os.path.expanduser(filePath))except OSError:passoutput = open(filePath, 'ab+')json.dump(autoSuggestList, output)output.close()if options.server !='localhost':try:os.system("scp "+filePath +" root@"+options.server+":"+destFilePath)except:shutil.copy2(filePath, destFilePath)def populateBrandSynonymMap():global synonymsMapbrand_synonyms = get_mongo_connection(options.mongoHost).Catalog.BrandSynonyms.find()for synonym in brand_synonyms:synonymsMap[synonym['brand'].upper()] = synonym['synonyms']def populateModelNameSynonyms():global modelNameSynonymMapmodel_synonyms = get_mongo_connection(options.mongoHost).Catalog.ModelNameSynonyms.find()for synonym in model_synonyms:modelNameSynonymMap[synonym['skuBundleId']] = synonym['synonyms']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 conif __name__=='__main__':pushData()