Skip to content

Commit

Permalink
Add product characteristics and add new html lines on description (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
elitonzky authored Jun 26, 2024
1 parent 85df6f1 commit 7720cee
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
10 changes: 10 additions & 0 deletions marketplace/clients/vtex/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,13 @@ def list_all_active_products(self, domain):
current_from += step

return list(unique_skus)

# API throttling
@rate_limit_and_retry_on_exception(
get_domain_from_args, calls_per_period=VTEX_CALLS_PER_PERIOD, period=VTEX_PERIOD
)
def get_product_specification(self, product_id, domain):
url = f"https://{domain}/api/catalog_system/pvt/products/{product_id}/specification"
headers = self._get_headers()
response = self.make_request(url, method="GET", headers=headers)
return response.json()
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@ class CategoriesBySeller(Rule):

def apply(self, product: FacebookProductDTO, **kwargs) -> bool:
seller_id = kwargs.get("seller_id")
service = kwargs.get("service")
domain = kwargs.get("domain")

if self._is_home_appliance(product):
# Only seller gbarbosab101 can have appliances
if seller_id != "gbarbosab101":
return False

# current_description = product.description
specifications = self._product_specification(product, service, domain)
combined_description = f"{product.description}\n{specifications}"

# Ensure the combined description does not exceed 9999 characters
if len(combined_description) > 9999:
combined_description = combined_description[:9999]

product.description = combined_description

# If the product is not an appliance, it can be added by any seller
return True

Expand All @@ -25,3 +39,19 @@ def _get_categories(self, product: FacebookProductDTO) -> set:
def _is_home_appliance(self, product: FacebookProductDTO) -> bool:
product_categories = self._get_categories(product)
return bool(self.HOME_APPLIANCES_CATEGORIES.intersection(product_categories))

def _product_specification(
self, product: FacebookProductDTO, service, domain
) -> str:
product_id = product.product_details.get("ProductId")
specification_text = "Características: "
specifications = service.get_product_specification(product_id, domain)

specification_parts = []
for specification in specifications:
name = specification.get("Name")
value = ", ".join(specification.get("Value", []))
specification_parts.append(f"{name} - {value}")

specification_text += "\n ".join(specification_parts) + "."
return specification_text
3 changes: 3 additions & 0 deletions marketplace/services/vtex/private/products/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ def update_webhook_product_info(

return updated_products_dto

def get_product_specification(self, product_id, domain):
return self.client.get_product_specification(product_id, domain)

# ================================
# Private Methods
# ================================
Expand Down
15 changes: 11 additions & 4 deletions marketplace/services/vtex/utils/data_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ def __init__(self):
@staticmethod
def clean_text(text: str) -> str:
"""Cleans up text by removing HTML tags, replacing quotes with empty space,
replacing commas with semicolons, and normalizing whitespace."""
replacing commas with semicolons, and normalizing whitespace but keeping new lines.
"""
# Remove HTML tags
text = re.sub(r"<[^>]*>", "", text)
# Replace double and single quotes with empty space
text = text.replace('"', "").replace("'", " ")
# Replace commas with semicolons
text = text.replace(",", ";")
# Normalize new lines and carriage returns to space and remove excessive whitespace
text = re.sub(r"\s+", " ", text.strip())
# Normalize new lines and carriage returns to a single newline
text = re.sub(r"\r\n|\r|\n", "\n", text)
# Remove excessive whitespace but keep new lines
text = re.sub(r"[ \t]+", " ", text.strip())
# Remove bullet points
text = text.replace("•", "")
return text
Expand Down Expand Up @@ -224,7 +227,11 @@ def process_single_sku(self, sku_id):
if not self._validate_product_dto(product_dto):
continue

params = {"seller_id": seller_id}
params = {
"seller_id": seller_id,
"service": self.service,
"domain": self.domain,
}
all_rules_applied = True
for rule in self.rules:
if not rule.apply(product_dto, **params):
Expand Down

0 comments on commit 7720cee

Please sign in to comment.