Rev 13735 | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *import pymongoimport MySQLdbimport codecsfrom shop2020.config.client.ConfigClient import ConfigClientfrom shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import Item, Categoryfrom shop2020.thriftpy.model.v1.catalog.ttypes import statusconfig_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 = NoneBASE_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 = itemIdself.price = priceself.url = urlself.imageUrl = imageUrlself.ean = eanself.shortDesc = shortDescself.modelName = modelNameself.color = colorself.productGroup = productGroupself.catalogItemId = catalogItemIdself.modelNumber = modelNumberself.brand = branddef 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 generateCategoryMap():global categoryMapresult = 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.brandelif item.category == 10010:info.productGroup = "Tablets > "+item.brandelse:info.productGroup = (categoryMap.get(categoryMap.get(item.category)[0])[1]+" > "+categoryMap.get(item.category)[1]+" > "+item.brand).replace(","," ")itemMap[item.id] = infofinally:session.close()def populateContent():global itemMapfor v in itemMap.itervalues():try:print "MOngo ",v.catalogItemIdcol = list(get_mongo_connection(host_mongo).CONTENT.siteContent.find({'_id':v.catalogItemId}))if len(col) ==0:continueelse:v.url = col[0]['url']v.imageUrl = col[0]['defaultImageUrl']v.shortDesc = col[0]['keySpecs']except Exception as e:print edef getEanInformation():global itemMapfor v in itemMap.itervalues():sql = '''select itemNumber from inventoryItem where itemId=%d limit 1''' % \(v.itemId)print sqlcursor.execute(sql)data = cursor.fetchone()print dataif (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:continuetry: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 eprint "Exception in writing ",v.itemIdf.close()def getShortDesc(descArr):description = ""if descArr is None or len(descArr) == 0:return descriptionelse:for desc in descArr:description += desc.replace(',',' ')return descriptiondef 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()