Subversion Repositories SmartDukaan

Rev

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

from elixir import *
import pymongo
import MySQLdb
import codecs
from shop2020.config.client.ConfigClient import ConfigClient
from shop2020.model.v1.catalog.impl import DataService
from shop2020.model.v1.catalog.impl.DataService import Item, Category
from shop2020.thriftpy.model.v1.catalog.ttypes import status

config_client = ConfigClient()
host = config_client.get_property('catalog_service_db_hostname')
host_mongo = config_client.get_property('staging_hostname')
host_warehouse = config_client.get_property('warehouse_service_server_host')

DataService.initialize(db_hostname=host)

categoryMap ={}
itemMap = {} 

con = None
BASE_URL = "http://saholic.com/"
AFF_TRACK = "?afid=109&utm_source=Idealo&utm_medium=affiliate_CPC"
xstr = lambda s: s or ""

db = MySQLdb.connect(host_warehouse,"root","shop2020","warehouse" )
cursor = db.cursor()

class __ItemInfo:
    
    def __init__(self, itemId, price, url, imageUrl, ean, shortDesc, brand, modelName, color, productGroup, catalogItemId, modelNumber):
        self.itemId = itemId
        self.price = price
        self.url = url
        self.imageUrl = imageUrl
        self.ean = ean
        self.shortDesc = shortDesc
        self.modelName = modelName
        self.color = color
        self.productGroup = productGroup
        self.catalogItemId = catalogItemId
        self.modelNumber = modelNumber
        self.brand = brand

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 generateCategoryMap():
    global categoryMap
    result = session.query(Category).all()
    for cat in result:
        categoryMap[cat.id] = [cat.parent_category_id ,cat.display_name]    
        
def populateActiveProducts():
    generateCategoryMap()
    items = session.query(Item).filter(Item.status == status.ACTIVE).all()
    try:
        for item in items:
            info = __ItemInfo(item.id, item.sellingPrice, None, None, None, None, item.brand, item.model_name, item.color, None, item.catalog_item_id, item.model_number)
            if item.category == 10006:
                info.productGroup = "Mobile Phones > "+item.brand
            elif item.category == 10010:
                info.productGroup = "Tablets > "+item.brand
            else:
                info.productGroup = (categoryMap.get(categoryMap.get(item.category)[0])[1]+" > "+categoryMap.get(item.category)[1]+" > "+item.brand).replace(","," ")
            
            itemMap[item.id] = info
    finally:
        session.close()

def populateContent():
    global itemMap
    for v in itemMap.itervalues():
        try:
            print "MOngo ",v.catalogItemId
            col = list(get_mongo_connection(host_mongo).CONTENT.siteContent.find({'_id':v.catalogItemId}))
            if len(col) ==0:
                continue
            else:
                v.url = col[0]['url']
                v.imageUrl = col[0]['defaultImageUrl']
                v.shortDesc = col[0]['keySpecs']
        except Exception as e:
            print e
     
def getEanInformation():
    global itemMap
    for v in itemMap.itervalues():
        sql = '''select itemNumber  from inventoryItem where itemId=%d limit 1''' % \
            (v.itemId)
        print sql
        cursor.execute(sql)
        data = cursor.fetchone()
        print data
        if (data is None or data[0] is None or len(data[0])==0):
            v.ean = ""
        else:
            v.ean = data[0]

def writeFile():
    f = codecs.open('IdealoProductFeed-V1','w','utf-8')
    f.write('Article number\tEAN (European article number) / GTIN (Global Trade Item Number)\tManufacturers code / number\t \
    Manufacturer\tProduct Name\tDescription\tProduct Group\tPrice INR\tDelivery status\tProduct URL\tPicture URL\tDelivery Costs\n')
    for v in itemMap.itervalues():
        if v.url is None:
            continue
        try:
            f.write(str(v.itemId)+'\t'+str(v.ean)+'\t'+""'\t'+v.brand+'\t'+getProductName(v)+'\t'+getShortDesc(v.shortDesc)+'\t'+v.productGroup+'\t'+str(v.price)+'\t'+'In Stock'+'\t'+BASE_URL+v.url+AFF_TRACK+'\t'+v.imageUrl+'\t'+'0.00'+'\n')
        except Exception as e:
            print e
            print "Exception in writing ",v.itemId
    f.close()

def getShortDesc(descArr):
    description = "" 
    if descArr is None or len(descArr) == 0:
        return description
    else:
        for desc in descArr:
            description += desc.replace(',',' ')
        return description
        
    
def getProductName(item):
    return (xstr(item.brand)+" "+xstr(item.modelName)+" "+xstr(item.modelNumber)+" "+xstr(item.color)).replace(",","")

def main():
    populateActiveProducts()
    populateContent()
    try:
        getEanInformation()
    finally:
        db.close()
        print "Db connection closed"
    writeFile()
    

if __name__=='__main__':
    main()