Skip to content

Commit

Permalink
fix: getir menu processor logical error fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fmelihh committed May 8, 2024
1 parent 8df1afa commit 37779ee
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
30 changes: 26 additions & 4 deletions src/recommendation_engine/app/domain/menu/entity/getir.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json
import lxml.etree
from loguru import logger

from ...entity import BaseEntity
from ..values import RequestValue, MenuValue
from ...processor import Processor, SyncCallParams
from ..values import RequestValue, MenuValue, MenuStack


class GetirMenu(BaseEntity, Processor):
Expand Down Expand Up @@ -33,6 +31,7 @@ def __init__(self, restaurant_slug: str):
method="GET",
headers=self.HEADERS,
)
self.menu_stack = MenuStack()

def _retrieve_menu_from_api(self) -> list[dict] | None:
request_template = self.filter_and_search_payload.retrieve_formatted_request(
Expand All @@ -52,7 +51,30 @@ def _retrieve_menu_from_api(self) -> list[dict] | None:
logger.info(f"Menu with {self.restaurant_slug} was crawled.")
return menu_list

@staticmethod
def _transform_unstructured_data(category: str, menu_value: dict) -> MenuValue:
values = dict()
values["category"] = category
values["product_id"] = menu_value["id"]
values["name"] = menu_value["name"]
values["price"] = menu_value["priceText"]
values["description"] = menu_value["description"]
values["image_url"] = menu_value["imageURL"]
values["full_screen_image_url"] = menu_value["fullScreenImageURL"]
values["is_available"] = menu_value["isAvailable"]
menu_value = MenuValue(**values)
return menu_value

def process(self, process_limit: int | None = None) -> list[MenuValue]:
menu_list = self._retrieve_menu_from_api()
return []
for entity in menu_list:
category = entity["name"]
category_menu_list = entity["products"]
for category_menu in category_menu_list:
menu = self._transform_unstructured_data(category, category_menu)
self.menu_stack.add_menu(menu)

if process_limit is not None and len(self.menu_stack) >= process_limit:
break

return self.menu_stack.retrieve_menu_list()
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .price import Price
from .menu import MenuValue
from .menu_stack import MenuStack
from .request_value import RequestValue
52 changes: 50 additions & 2 deletions src/recommendation_engine/app/domain/menu/values/menu.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pydantic import HttpUrl
from dataclasses import dataclass

from .price import Price
Expand All @@ -11,6 +12,53 @@ class MenuValue(DataclassValidationMixin):
name: str
price: Price
description: str
image_url: str
full_screen_image_url: str
image_url: str | None
full_screen_image_url: str | None
is_available: bool

def validate_category(self) -> str:
if not isinstance(self.category, str):
raise ValueError("Invalid comment category type expected.")
return self.category

def validate_product_id(self) -> str:
if not isinstance(self.product_id, str):
raise ValueError("Invalid comment product id type expected.")
return self.product_id

def validate_name(self) -> str:
if not isinstance(self.name, str):
raise ValueError("Invalid comment name type expected.")
return self.name

def validate_price(self) -> Price:
if not isinstance(self.price, str):
raise ValueError("Invalid comment price type expected.")

amount = self.price.replace("₺", "").replace(",", ".").strip()
amount = float(amount)
return Price(amount=amount, currency="TL")

def validate_description(self) -> str:
if not isinstance(self.description, str):
raise ValueError("Invalid comment description type expected.")
return self.description

def validate_image_url(self) -> str | None:
if not isinstance(self.image_url, str):
raise ValueError("Invalid comment image url type expected.")
if len(self.image_url) == 0:
return None
return str(HttpUrl(self.image_url))

def validate_full_screen_image_url(self) -> str | None:
if not isinstance(self.full_screen_image_url, str):
raise ValueError("Invalid comment full_screen_image_url type expected.")
if len(self.full_screen_image_url) == 0:
return None
return str(HttpUrl(self.full_screen_image_url))

def validate_is_available(self) -> bool:
if not isinstance(self.is_available, bool):
raise ValueError("Invalid comment is available type expected.")
return self.is_available
19 changes: 19 additions & 0 deletions src/recommendation_engine/app/domain/menu/values/menu_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import dataclass
from .menu import MenuValue


@dataclass(frozen=True)
class MenuStack:
_menu_list = []

def retrieve_menu_list(self) -> list[MenuValue]:
return self._menu_list

def add_menu(self, menu_value: MenuValue):
self._menu_list.append(menu_value)

def __len__(self) -> int:
return len(self._menu_list)

def clean_menu_list(self):
self._menu_list.clear()
3 changes: 2 additions & 1 deletion src/recommendation_engine/app/domain/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
from typing import TypeVar, List, Literal
from pydantic import BaseModel, Field, AnyUrl

from .menu.values import MenuValue
from .comments.values import CommentValue
from .restaurants.values import RestaurantValue


T = TypeVar("T", *[List[RestaurantValue], List[CommentValue]])
T = TypeVar("T", *[List[RestaurantValue], List[CommentValue], List[MenuValue]])


class SyncCallParams(BaseModel):
Expand Down

0 comments on commit 37779ee

Please sign in to comment.