Subversion Repositories SmartDukaan

Rev

Rev 20663 | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pysolr
import pymongo
import json
import os
import optparse
import shutil
import traceback
from dtr.utils.utils import CATEGORY_MAP, SUB_CATEGORY_MAP

con=None

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="solrPath",
                      default="http://localhost:8983/solr/",
                      type="string", help="Complete solr path",
                      metavar="HOST")

(options, args) = parser.parse_args()

synonymsMap = {}
modelNameSynonymMap = {}


class __SkuInfo:
    
    def __init__(self, id, ids, brand, model_name, category_id, subCategoryId,thumbnail, title, brand_synonyms, model_name_synonyms, source_product_names, \
                 category, subCategory):
        #Skubundle id
        self.id = id
        #_id of skus
        self.ids = ids
        self.brand = brand
        self.model_name = model_name
        self.category_id = category_id
        self.subCategoryId = subCategoryId
        self.thumbnail = thumbnail 
        self.title= title
        self.brand_synonyms = brand_synonyms
        self.model_name_synonyms = model_name_synonyms
        self.source_product_names = source_product_names
        self.category = category
        self.subCategory = subCategory 
        
        
solr = pysolr.Solr(options.solrPath, timeout=10)

def pushData():
    populateBrandSynonymMap()
    populateModelNameSynonyms()
    exception = []
    catalogMap = {}
    items = get_mongo_connection(options.mongoHost).Catalog.MasterData.find({'source_id' : { "$in": SOURCE_MAP.keys() },'category_id':{"$in":[3,5]} })
    print items.count()
    for item in items:
        try:
            title = xstr(item['brand'])+" "+xstr(item['model_name'])
        except:
            exception.append(item['_id'])
            continue
        
        if catalogMap.has_key(int(item['skuBundleId'])):
            skuInfo = catalogMap.get(int(item['skuBundleId']))
            skuInfo.source_product_names.append(item['source_product_name'].strip())
            skuInfo.ids.append(int(item['_id']))
        else:
            syn_brand = synonymsMap.get((item['brand']).upper())
            model_syn = modelNameSynonymMap.get(item['skuBundleId'])
            try:
                if len(str(item['thumbnail']).strip()) == 0:
                    item['thumbnail'] = "http://api.profittill.com/img/no_image_available.png"
            except:
                print "Exception in item thumbnail",item['_id']
                continue
            try:
                skuInfo = __SkuInfo(str(item['skuBundleId']), [int(item['_id'])], str(item['brand']).strip(), str(item['model_name']).strip(), \
                                int(item['category_id']), int(item['subCategoryId']), item['thumbnail'].strip(), title, syn_brand, model_syn, [item['source_product_name'].strip()], \
                                CATEGORY_MAP.get(int(item['category_id'])), SUB_CATEGORY_MAP.get(int(item['subCategoryId'])))
            except:
                print "Exception in item ",item['_id']
                continue
            catalogMap[int(item['skuBundleId'])] = skuInfo
    l=[]        
    for data in catalogMap.values():
        l.append(data.__dict__)
    solr.add(l)

    for x in exception:
        print x

def populateBrandSynonymMap(): 
    global synonymsMap
    brand_synonyms = get_mongo_connection(options.mongoHost).Catalog.BrandSynonyms.find()
    for synonym in brand_synonyms:
        synonymsMap[synonym['brand'].upper()] = synonym['synonyms']

def populateModelNameSynonyms():
    global modelNameSynonymMap
    model_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 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
    
if __name__=='__main__':
    pushData()