def update_product_desc(self, list_product): CB = ChinaBrandResource() for cb_product in list_product: desc = {} lz_description = cb_product['goods_desc'] # replace desc image to lazada lz_short_description = CB.get_li_feature(lz_description) if cb_product['color']: short_desc_extra = "<ul><li><strong>Its main color is %s!</strong></li>" % cb_product[ 'color'] lz_short_description = lz_short_description.replace( '<ul>', short_desc_extra) desc['short_description'] = html.escape(lz_short_description) desc['SellerSku'] = cb_product['encrypted_sku'] try: product_schema = etree.XML("""<Request><Product> <Attributes> <short_description>{short_description}</short_description> </Attributes> <Skus> <Sku> <SellerSku>{SellerSku}</SellerSku> </Sku> </Skus> </Product> </Request>""".format(**desc)) result = self._post("UpdateProduct", data=etree.tostring(product_schema)) print(result) except: traceback.print_exc()
def update_product_desc(self, list_product): CB = ChinaBrandResource() for cb_product in list_product: desc = {} lz_description = cb_product_info['goods_desc'] lz_short_description = CB.get_li_feature(lz_description) if cb_product['color']: short_desc_extra = "<ul><li><strong>Its main color is %s!</strong</li></ul>" % cb_product['color'] if cb_product['size']: short_desc_extra += "<ul><li><strong>Its size is %s!</strong></li></ul>" % cb_product['size'] lz_short_description = lz_short_description.replace('<ul>' , short_desc_extra) desc['short_description'] = html.escape(lz_short_description) desc['SellerSku'] = cb_product['encrypt_sku'] try: pass except: pass
def create_product_from_CB_Sub(self, category, cb_product_info): #为啥category变成了浮点型 category = int(category) CB = ChinaBrandResource() try: #下一句是我注释掉的 #float(cb_product_info[0]['size']) # if size is pure number do not use that if 'pant' in cb_product_info[0]['title'].lower(): return except: pass test_p = self.get_account_id() print(test_p) country = test_p[0:2] #country = self.get_account_id()[0:2] #country == MY? cb_product = cb_product_info[0] #cb_product大体等于从cb抓取出来的一系列数据了 lz_description = cb_product['goods_desc'] # replace desc image to lazada # for desc_image in cb_product['desc_img']: # lz_description = lz_description.replace( # desc_image, self.upload_image(desc_image)) lz_short_description = CB.get_li_feature(lz_description) if cb_product['color']: short_desc_extra = "<ul><li><strong>It is %s!</strong></li>" % cb_product[ 'color'] # 若cb中抓取到color则在title中补充到颜色属性 cb_product['title'] = cb_product[ 'title'] + "(%s)" % cb_product['color'].replace("&", "and") else: short_desc_extra = "there is no color from cb" #新添的 # if cb_product['size']: # short_desc_extra += "<li>Its size is %s!</strong></li>" % cb_product['size'] # 在简短描述里面加上颜色属性 lz_short_description = lz_short_description.replace( '<ul>', short_desc_extra) cb_product['color'] = self.adapt_color(cb_product['color']) lz_brand = cb_product['goods_brand'] if self.check_brand_exist( cb_product['goods_brand']) else 'OEM' # 得到Package Content放入lz_sku_spec中 lz_sku_spec = CB.get_sku_specification(lz_description) cb_product['title'] = self.get_clean_title(cb_product, lz_sku_spec, category) print("----------------------\n\n%s\n\n------------------" % cb_product['title']) if country != "MY": cb_product['title'] += " - intl" # add statics pictrure to description #对html来说,<, > , ?, & 和引号等有特殊意义 , 例如<a>表示连接 ,如果你确实需要在网页显示 '<a>'这一个东东,就必须escape(转义),变成 '& lt;a& gt;' lz_product = { "category": category, "name": cb_product['title'], "brand": lz_brand, "model": int(time.time()), "color": cb_product['color'], "description": html.escape( lz_description.replace('max-width:1000px', 'max-width:100%')), "short_description": html.escape(lz_short_description) } product_schema = etree.XML("""<Request> <Product> <PrimaryCategory>{category}</PrimaryCategory> <SPUId></SPUId> <Attributes> <name>{name}</name> <name_ms>{name}</name_ms> <color_family>{color}</color_family> <short_description>{short_description}</short_description> <brand>{brand}</brand> <model>{model}</model> <warranty_type>No Warranty</warranty_type> <description>{description}</description> </Attributes> <Skus id="skus"> </Skus> </Product> </Request> """.format(**lz_product)) variation = True c_skus = [] for cb_sku in cb_product_info: # c_skus used for complete_sku意思是加上c_skus[]之后表示已经完成sku了? c_skus.append(cb_sku['sku']) cb_sku['encrypted_sku'] = self.encrypt_sku(cb_sku['sku']) # package content cb_sku['special_price'] = get_special_price(cb_sku['price'], cb_sku['ship_weight'], country=country) if float(cb_sku['ship_weight']) > 0.5: return cb_sku['price'] = get_sale_price(cb_sku['special_price']) start_date = arrow.now() cb_sku['special_from_date'] = start_date.strftime("%Y-%m-%d") cb_sku['special_to_date'] = start_date.replace( days=+365).strftime("%Y-%m-%d") cb_sku['package_content'] = lz_sku_spec.get( 'Package Content', '1 x see product description') # quantity cb_sku['quantity'] = random.randrange(3, 50) cb_sku['keywords'] = self.get_key_words(cb_sku, category) product_sku = etree.XML("""<Sku> <SellerSku>{encrypted_sku}</SellerSku> <tax_class>default</tax_class> <quantity>{quantity}</quantity> <price>{price}</price> <special_price>{special_price}</special_price> <special_from_date>{special_from_date}</special_from_date> <special_to_date>{special_to_date}</special_to_date> <color_family>{color}</color_family> <model>LA{encrypted_sku}</model> <package_length>{package_length}</package_length> <package_height>{package_height}</package_height> <package_weight>{ship_weight}</package_weight> <package_width>{package_width}</package_width> <package_width>{package_width}</package_width> <package_content>{package_content}</package_content> <std_search_keywords>{keywords}</std_search_keywords> <Images> </Images> <Image></Image> </Sku>""".format(**cb_sku)) # extra SKU attrs # extra_attr_str = get_extra_attrs(category) cattrs = self.get_mandatory_attributes(category) extra_attr = {} for ck, cv in cattrs.items(): if cb_sku['size'] in cv: extra_attr[ck] = cb_sku['size'] elif ck == 'ring_size': if cb_sku['size'] and cb_sku['size'].isdigit(): sku_size = cb_sku['size'] else: cb_sku['title'] = cb_sku[ 'title'] + "(Size:%s)" % cb_sku['size'].replace( "&", "and") product_schema.xpath( "//name")[0].text = cb_sku['title'] sku_size = 'Not Specified' # do not associated product if no size variation = False extra_attr[ck] = sku_size elif ck == 'size': if cb_sku['size']: format_size = re.sub('2', 'X', cb_sku['size']).upper() if category in self.shoes: sku_size = "EU:" + cb_sku['size'] elif format_size in self.__size and format_size in cv: sku_size = 'Int:' + format_size else: cb_sku['title'] = cb_sku[ 'title'] + "(Size:%s)" % cb_sku[ 'size'].replace("&", "and") product_schema.xpath( "//name")[0].text = cb_sku['title'] sku_size = "Int: One size" # do not associated product if no size variation = False else: sku_size = 'Not Specified' variation = False extra_attr[ck] = sku_size else: if 'One Size' in cv: extra_attr[ck] = 'One Size' elif 'Int:One Size' in cv: extra_attr[ck] = 'Int:One Size' elif 'Int: One Size' in cv: extra_attr[ck] = 'Int: One Size' elif 'color' in ck: extra_attr[ck] = 'Multicolor' else: extra_attr[ck] = 'Not Specified' variation = False for tag, tv in extra_attr.items(): new_tag = etree.Element(tag) new_tag.text = str(tv) attr_type = self.check_attrs_type(category, tag) if attr_type == 'sku': product_sku.xpath("//Sku")[0].append(new_tag) else: product_schema.xpath("//Attributes")[0].append(new_tag) # check if image exist mongodb images = self.__mongodb.all_cb_sku.find_one({ "sku": cb_sku["sku"] }).get("limages") if images: self.remove_elements_by_tag(product_sku, 'Images') product_sku.append(etree.XML(images)) else: # image for image_src in cb_sku['original_img'][:8]: image_src = self.upload_image(image_src, strict=True) if not image_src: continue e_image_src = etree.Element("Image") e_image_src.text = image_src product_sku.find("Images").append(e_image_src) # save image to mongodb self.__mongodb.all_cb_sku.update({'sku': cb_sku['sku']}, { "$set": { "limages": etree.tostring(product_sku.find("Images")) } }) if not variation: self.remove_elements_by_tag(product_schema, "Sku") product_schema.xpath("//Skus")[0].append(product_sku) result = self._post("CreateProduct", data=etree.tostring(product_schema)) # if have been upload to lazada save it. if result: self.complete_sku(cb_sku['sku'], 10) else: product_schema.xpath("//Skus")[0].append(product_sku) print("\n\n\n\nthe API model of CreateProduct: \n%s" % product_schema) #import pdb;pdb.set_trace() result = self._post("CreateProduct", data=etree.tostring(product_schema)) # if have been upload to lazada save it. if result: self.complete_sku(c_skus, 10)
def create_product_from_CB_Sub(self, category, cb_product_info): CB = ChinaBrandResource() try: if 'pant' in cb_product_info[0]['title'].lower: return except: pass country = self.get_account_id() country = country[:2] cb_product = cb_product_info[0] lz_description = cb_product['goods_desc'] lz_short_description = CB.get_li_feature(lz_description) if cb_product['color']: short_desc_extra = "<ul><li><strong>It is %s!</strong></li></ul>" % cb_product['color'] cb_product['title'] = cb_product['title'] + "(%s)" % cb_product['color'].replace("&", "and") else: short_desc_extra = "there is no color from cb" lz_short_description = lz_short_description.replace('<ul>', short_desc_extra) cb_product['color'] = self.adapt_color(cb_product['color']) lz_brand = cb_product['goods_brand'] if self.check_brand_exist(cb_product['goods_brand']) else 'OEM' lz_sku_spec = CB.get_sku_specification(lz_description) cb_product['title'] = self.get_clean_title(cb_product, lz_sku_spec, category) print("-------------------\n\n\n%s\n\n\n----------------" % cb_product['title']) if country != "MY": cb_product['title'] += " - init" lz_product = {"category": category, "name": cb_product['title'], "brand": lz_brand, "model": int(time.time()), "color": cb_product['color'], "description": html.escape(lz_description.replace('max-width: 1000px' 'max-width: 100%')) "short_description": html.escape(lz_short_description)} product_schema = etree.XML("""<Request> <product> <PrimaryCategory>{category}</PrimaryCategory> <SPUId></SPUId> <Attributes> <name>{name}</name> <name_ms>{name}</name_ms> <color_famliy>{color}</color_famliy> <short_description>{short_description}</short_description> <brand>{brand}</brand> <model>{model}</model> <warranty_type>No warrenty</warranty_type> </Attributes> <Skus id="skus"> </SKus> </Product> </Request> """.format(**lz_product)) variation = True c_skus = [] for cb_sku in cb_product_info: c_skus.append(cb_sku['sku']) cb_sku['encrypted'] = self.encrypt_sku(cb_sku['sku']) cb_sku['special_price'] = get_special_price(cb_sku['price'], cb_sku['ship_weight'], country = country) cb_sku['price'] = get_sale_price(cb_sku['special_price']) start_date = arrow.now() cb_sku['special_from_date'] = start_date.strftime("%Y-%m-%d") cb_sku['special_to_date'] = start_date.replace(days=+365).strftime("%Y-%m-%d") cb_sku['package_content'] = lz_sku_spec.get('Package Content', '1 x see product description' ) cb_sku['quantity'] = random.randrange(3,50) cb_sku['keyword'] = self.get_key_words(cb_sku, category) product_sku = etree.XML() cattrs = self.get_mandatory_attributes(category) extra_attr = {} for ck, cv in cattrs.items(): if cb_sku['size'] and cb_sku['size'].isdigit(): sku_size = cb_sku['size'] else: cb_sku['title'] = cb_sku['title'] + "(Size: %s)" % cb_sku['size'].replace("&", "and") product_schema.xpath("//name")[0].text = cb_sku['title'] sku_size = 'Not Specified' variation = False extra_attr[ck] = sku_size pass