def update_price_quantity(self, cb_sku_no): CB = ChinaBrandResource() cb_skus = CB.updated_product_by_encryptsku(cb_sku_no) product_list = etree.XML("""<Request><Product> <Skus> </Skus> </Product> </Request>""") for cb_sku in cb_skus: self.__mongodb.cb_sku.update({'sku': cb_sku['sku']}, cb_sku) cb_sku['special_price'] = get_special_price( cb_sku['price'], cb_sku['ship_weight']) cb_sku['price'] = get_sale_price(cb_sku['special_price']) cb_sku['special_from_date'], cb_sku['special_to_date'] = \ self.get_special_price_data() # quantity cb_sku['quantity'] = 0 if cb_sku.get('is_on_sale', 0) in (1, '1'): cb_sku['quantity'] = random.randrange(30, 666) product_sku = etree.XML("""<Sku> <SellerSku>{encrypted_sku}</SellerSku> <Quantity>{quantity}</Quantity> <Price>{price}</Price> <SalePrice>{special_price}</SalePrice> <SaleStartDate>{special_from_date}</SaleStartDate> <SaleEndDate>{special_to_date}</SaleEndDate> </Sku>""".format(**cb_sku)) product_list.xpath('//Skus')[0].append(product_sku) result = self._post("UpdatePriceQuantity", data=etree.tostring(product_list)) print(result) if result: return True
def adjust_price_quantity(self, list_product): for cb_product in list_product: # time.sleep(1) for sinfo in cb_product['Skus']: encrypted_sku = sinfo['SellerSku'] print("special sku: %s" % encrypted_sku) if "RA06" not in encrypted_sku or "CM08" not in encrypted_sku: print("no need adjust") return cb_sku = {} cb_sku['SellerSku'] = encrypted_sku cb_sku['special_price'] = round(sinfo['special_price'] * 1.5, 2) cb_sku['price'] = get_sale_price(cb_sku['special_price'] * 1.8, 0.6) cb_sku['quantity'] = random.randrange(10, 666) self.adjust_price(cb_sku)
def cb_product_adpter(self, cb_product_info, category, CB): cb_sku = cb_product = cb_product_info 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>Its main color is %s!</strong></li>" % cb_product[ 'color'] lz_short_description = lz_short_description.replace( '<ul>', short_desc_extra) cb_sku['title'] = cb_sku[ 'title'] + "(%s)" % cb_sku['color'].replace("&", "and") if cb_sku['size']: cb_sku['title'] = cb_sku[ 'title'] + "(Size:%s)" % cb_sku['size'].replace("&", "and") print(cb_sku['title']) 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) lz_product = { "category": category, "name": cb_product['title'], "brand": lz_brand, "associateSku": cb_product['associateSku'], "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> <AssociatedSku>{associateSku}</AssociatedSku> <Attributes> <name>{name}</name> <name_ms>{name}</name_ms> <short_description>{short_description}</short_description> <brand>{brand}</brand> <model>RA{associateSku}</model> <warranty_type>No Warranty</warranty_type> <description>{description}</description> </Attributes> <Skus id="skus"> </Skus> </Product> </Request> """.format(**lz_product)) # sku add # package content cb_sku['special_price'] = get_special_price(cb_sku['price'], cb_sku['ship_weight']) 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=+5).strftime("%Y-%m-%d") cb_sku['package_content'] = lz_sku_spec.get('Package Content', 'see product detail') # quantity cb_sku['quantity'] = 0 if cb_sku.get('is_on_sale', 0) in (1, '1'): cb_sku['quantity'] = random.randrange(30, 666) 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>RA{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> <Images> </Images> <Image></Image> </Sku>""".format(**cb_sku)) # extra SKU attrs extra_attr_str = get_extra_attrs(category) if 'size' in extra_attr_str: if cb_sku['size']: format_size = re.sub('2', 'X', cb_sku['size']).upper() sku_size = 'Int:' + format_size if format_size in self.__size else "Int: One size" else: sku_size = 'Not Specified' extra_attr_str = extra_attr_str.format(size=sku_size) extra_attr_element = etree.XML(extra_attr_str) for eae in extra_attr_element.getchildren(): if 'size' in eae.tag: product_sku.append(eae) else: product_schema.xpath("//Attributes")[0].append(eae) # 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) product_schema.xpath("//Skus")[0].append(product_sku) result = self._post("CreateProduct", data=etree.tostring(product_schema)) if result: return True
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 get_price_quantity(self, sinfo, cucode=None, ajax=False): encrypted_sku = sinfo['SellerSku'] country = cucode or self.get_account_id()[0:2] print("special sku: %s" % encrypted_sku) cb_sku = {} mongo = pymongo.MongoClient("mongodb://mongo.server:55088/") if "RA01" in encrypted_sku or "CM01" in encrypted_sku: sku = encrypted_sku.split('-')[-1] cb_sku = mongo.cb_info.all_cb_sku.find_one({'sku': sku}) level = 'CB' elif "CM02" in encrypted_sku: sku = encrypted_sku.replace("CM02-", "") cb_sku = mongo.pf_info.pf_sku.find_one({"SKU": sku}) try: cb_sku['price'] = round((float(cb_sku['CostPrice']) + 5) / 7) cb_sku['ship_weight'] = round( (float(cb_sku['Weight']) + 10) / 1000, 2) level = 'pfh' except: self.remove_product([ encrypted_sku, ]) print("Remove it") return False elif "CM00" in encrypted_sku: cb_sku = mongo.lazada.findOne( {"associateSku": { "$regex": encrypted_sku + "*" }}) cb_sku['price'] = sinfo['special_price'] cb_sku['ship_weight'] = 0.1 level = 'CM00' if not cb_sku.get('price'): print('have offline %s' % cb_sku['sku']) # self.remove_product([encrypted_sku]) return False cb_sku['encrypted_sku'] = encrypted_sku cb_sku['special_price'] = get_special_price(cb_sku['price'], cb_sku['ship_weight'], country=country, level=level) print(encrypted_sku, cb_sku['price'], cb_sku['ship_weight'], cb_sku['special_price']) 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") # quantity cb_sku['quantity'] = random.randrange(30, 666) # use for requests if ajax: return cb_sku product_sku = etree.XML("""<Sku> <SellerSku>{encrypted_sku}</SellerSku> <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> </Sku>""".format(**cb_sku)) return product_sku, cb_sku
def pfh_sku_adpter(self, pfh_sku): pfh_sku["short_description"] = html.escape( pfh_sku['short_description']) product_schema = etree.XML("""<Request> <Product> <PrimaryCategory>{category}</PrimaryCategory> <SPUId></SPUId> <AssociatedSku>{associateSku}</AssociatedSku> <Attributes> <name>{name}</name> <name_ms>{name}</name_ms> <color_family>{color}</color_family> <short_description>{short_description}</short_description> <brand>{brand}</brand> <model>RA{associateSku}</model> <warranty_type>No Warranty</warranty_type> <description>{description}</description> </Attributes> <Skus id="skus"> </Skus> </Product> </Request> """.format(**pfh_sku)) # sku add # package content pfh_sku['special_price'] = get_special_price(pfh_sku['price'], pfh_sku['ship_weight'], 9, level='pfh') pfh_sku['price'] = get_sale_price(pfh_sku['special_price']) start_date = arrow.now() pfh_sku['special_from_date'] = start_date.strftime("%Y-%m-%d") pfh_sku['special_to_date'] = start_date.replace( days=+365).strftime("%Y-%m-%d") 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>RA{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> <Images> </Images> <Image></Image> </Sku>""".format(**pfh_sku)) # extra SKU attrs extra_attr_str = get_extra_attrs(pfh_sku['category']) if 'ring_size' in extra_attr_str: if pfh_sku['size'] and pfh_sku['size'].isdigit(): sku_size = pfh_sku['size'] else: sku_size = 'Not Specified' # do not associated product if no size self.remove_elements_by_tag(product_schema, "AssociatedSku") extra_attr_str = extra_attr_str.format(size=sku_size) elif 'size' in extra_attr_str: if pfh_sku['size']: format_size = re.sub('2', 'X', pfh_sku['size']).upper() if format_size in self.get_size(): sku_size = 'Int:' + format_size else: pfh_sku['title'] = pfh_sku[ 'title'] + "(Size:%s)" % pfh_sku['size'].replace( "&", "and") product_schema.xpath("//name")[0].text = pfh_sku['title'] sku_size = "Int: One size" # do not associated product if no size self.remove_elements_by_tag(product_schema, "AssociatedSku") else: sku_size = 'Not Specified' self.remove_elements_by_tag(product_schema, "AssociatedSku") extra_attr_str = extra_attr_str.format(size=sku_size) extra_attr_element = etree.XML(extra_attr_str) for eae in extra_attr_element.getchildren(): if 'size' in eae.tag: product_sku.append(eae) else: product_schema.xpath("//Attributes")[0].append(eae) # image image_srcs = list(set(pfh_sku['SKUImages'].split(';')[:8])) for image_src in image_srcs[1:]: # image_src = 'http://omuczm3im.bkt.gdipper.com/%s' % image_src.split('com/')[-1] image_src = image_src.replace('/img', '/cdnimg') print(image_src) print(requests.get(image_src)) 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) product_schema.xpath("//Skus")[0].append(product_sku) print(etree.tostring(product_schema)) result = self._post("CreateProduct", data=etree.tostring(product_schema)) if result: return True raise ("Something need adjust")
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