def getProductDimension(self, instance_of, instance_val): if self.htmlObject is None: return '' if instance_val is None: return '' content = None dimension = '' if instance_of == "DOCUMENT" and len(instance_val) > 0: content = self.getElementValue(instance_val) elif instance_of == "STRING" and len(instance_val) > 0: content = instance_val content = StringUtil.remove_html_tags(content) content = StringUtil.str_cleaner(content, r'\\([a-z0-9]{3})', '') content = StringUtil.str_cleaner(content, r'[^0-9a-zA-Z\s\-\(\).,"\'&]+', '') if content is not None and type( content) is not None and len(content) > 0: # for pat in self.dimensionPatternsToRemove: # content = StringUtil.str_cleaner(content, pat, "") for sptf in self.dimensionPatterns: if StringUtil.str_find_str(str(content), sptf): dimension = StringUtil.str_search_str(str(content), sptf) break if dimension and len(dimension) == 0 or len(dimension) > 35: dimension = self.getProductWeight(content) return dimension
def getProductWeight(self, instance_val): # self.writeToFile('content_raw.txt',instance_val) # self.writeToFile('content.txt',content.strip()) content = '' if instance_val is not None: content = StringUtil.str_cleaner(instance_val, r'<[^>]*>', '') content = StringUtil.str_cleaner(content, r'\s\s', '') content = StringUtil.str_search_str( content, r"(item weight|Shipping Weight)(:|:\s|\s:)(\d+(\.\d{1,2})?)(\s|\S)(ounce|pound|lb\s|lbs)" ) # self.writeToFile('content.txt',content.strip()) return content
def getVolumetricWeight(self, dimension, ut): if len(dimension) == 0 or " x " not in dimension: return 0 volumetric_weight = 0 try: di_arr = dimension.split(' x ') dimensions = [] if len(di_arr) == 3: for index in range(len(di_arr)): di = di_arr[index] for pat in self.dimensionPatternsToRemove: di = StringUtil.str_cleaner(di, pat, "").strip() if StringUtil.try_parse('float', di) is True: if ut == UnitTypes.MM: di = math.ceil(float(di) * 0.0393701) elif ut == UnitTypes.CM: di = math.ceil(float(di) * 0.393701) dimensions.append(float(di)) else: # Set value to 1 if the dimension value is invalid to convert to float. # This idea is wrong. Temp solution only. dimensions.append(1) if len(dimensions) == 3: volumetric_weight = reduce(lambda x, y: x * y, dimensions) volumetric_weight = volumetric_weight / 130 volumetric_weight = math.ceil(volumetric_weight) else: volumetric_weight = 0 except: volumetric_weight = 0 return volumetric_weight