|
9 | 9 | import requests
|
10 | 10 |
|
11 | 11 | from tgtg_scanner.errors import MaskConfigurationError
|
12 |
| -from tgtg_scanner.models import Config |
13 | 12 | from tgtg_scanner.models.location import DistanceTime, Location
|
14 | 13 |
|
15 | 14 | ATTRS = [
|
|
19 | 18 | "description",
|
20 | 19 | "price",
|
21 | 20 | "value",
|
22 |
| - "price_localized", |
23 |
| - "value_localized", |
24 | 21 | "currency",
|
25 | 22 | "pickupdate",
|
26 | 23 | "favorite",
|
27 | 24 | "rating",
|
28 |
| - "rating_localized", |
29 | 25 | "buffet",
|
30 | 26 | "item_category",
|
31 | 27 | "item_name",
|
@@ -57,58 +53,64 @@ class Item:
|
57 | 53 | returns well formated data for notifications.
|
58 | 54 | """
|
59 | 55 |
|
60 |
| - def __init__(self, data: dict, config: Union[Config, None] = None, location: Union[Location, None] = None): |
61 |
| - self.items_available = data.get("items_available", 0) |
62 |
| - self.display_name = data.get("display_name", "-") |
63 |
| - self.favorite = "Yes" if data.get("favorite", False) else "No" |
64 |
| - self.pickup_interval_start = data.get("pickup_interval", {}).get("start", None) |
65 |
| - self.pickup_interval_end = data.get("pickup_interval", {}).get("end", None) |
66 |
| - self.pickup_location = data.get("pickup_location", {}).get("address", {}).get("address_line", "-") |
67 |
| - |
68 |
| - item = data.get("item", {}) |
69 |
| - self.item_id = item.get("item_id") |
70 |
| - self.rating = item.get("average_overall_rating", {}).get("average_overall_rating", None) |
71 |
| - self.rating = "-" if not self.rating else f"{self.rating:.1f}" |
72 |
| - self.packaging_option = item.get("packaging_option", "-") |
73 |
| - self.item_name = item.get("name", "-") |
74 |
| - self.buffet = "Yes" if item.get("buffet", False) else "No" |
75 |
| - self.item_category = item.get("item_category", "-") |
76 |
| - self.description = item.get("description", "-") |
77 |
| - item_price = item.get("item_price", {}) |
78 |
| - item_value = item.get("item_value", {}) |
79 |
| - price = item_price.get("minor_units", 0) / 10 ** item_price.get("decimals", 0) |
80 |
| - value = item_value.get("minor_units", 0) / 10 ** item_value.get("decimals", 0) |
81 |
| - self.price = f"{price:.2f}" |
82 |
| - self.value = f"{value:.2f}" |
83 |
| - self.currency = item_price.get("code", "-") |
84 |
| - self.item_logo = item.get("logo_picture", {}).get( |
| 56 | + def __init__(self, data: dict, location: Union[Location, None] = None, locale: str = "en_US"): |
| 57 | + self.items_available: int = data.get("items_available", 0) |
| 58 | + self.display_name: str = data.get("display_name", "-") |
| 59 | + self.favorite: str = "Yes" if data.get("favorite", False) else "No" |
| 60 | + self.pickup_interval_start: Union[str, None] = data.get("pickup_interval", {}).get("start", None) |
| 61 | + self.pickup_interval_end: Union[str, None] = data.get("pickup_interval", {}).get("end", None) |
| 62 | + self.pickup_location: str = data.get("pickup_location", {}).get("address", {}).get("address_line", "-") |
| 63 | + |
| 64 | + item: dict = data.get("item", {}) |
| 65 | + self.item_id: str = item.get("item_id", None) |
| 66 | + self._rating: Union[float, None] = item.get("average_overall_rating", {}).get("average_overall_rating", None) |
| 67 | + self.packaging_option: str = item.get("packaging_option", "-") |
| 68 | + self.item_name: str = item.get("name", "-") |
| 69 | + self.buffet: str = "Yes" if item.get("buffet", False) else "No" |
| 70 | + self.item_category: str = item.get("item_category", "-") |
| 71 | + self.description: str = item.get("description", "-") |
| 72 | + item_price: dict = item.get("item_price", {}) |
| 73 | + item_value: dict = item.get("item_value", {}) |
| 74 | + self._price: float = item_price.get("minor_units", 0) / 10 ** item_price.get("decimals", 0) |
| 75 | + self._value: float = item_value.get("minor_units", 0) / 10 ** item_value.get("decimals", 0) |
| 76 | + self.currency: str = item_price.get("code", "-") |
| 77 | + self.item_logo: str = item.get("logo_picture", {}).get( |
85 | 78 | "current_url",
|
86 | 79 | "https://tgtg-mkt-cms-prod.s3.eu-west-1.amazonaws.com/13512/TGTG_Icon_White_Cirle_1988x1988px_RGB.png",
|
87 | 80 | )
|
88 |
| - self.item_cover = item.get("cover_picture", {}).get( |
| 81 | + self.item_cover: str = item.get("cover_picture", {}).get( |
89 | 82 | "current_url",
|
90 | 83 | "https://images.tgtg.ninja/standard_images/GENERAL/other1.jpg",
|
91 | 84 | )
|
92 | 85 |
|
93 |
| - store = data.get("store", {}) |
94 |
| - self.store_name = store.get("store_name", "-") |
| 86 | + store: dict = data.get("store", {}) |
| 87 | + self.store_name: str = store.get("store_name", "-") |
95 | 88 |
|
96 |
| - self.scanned_on = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 89 | + self.scanned_on: str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
97 | 90 | self.location = location
|
| 91 | + self.locale = locale |
98 | 92 |
|
99 |
| - # Localize attributes, first init with decoded values |
100 |
| - self.price_localized = self.price |
101 |
| - self.rating_localized = self.rating |
102 |
| - self.value_localized = self.value |
103 |
| - if config: |
104 |
| - try: |
105 |
| - self.rating_localized = babel.numbers.format_decimal(self.rating, locale=config.locale) |
| 93 | + @property |
| 94 | + def rating(self) -> str: |
| 95 | + if self._rating is None: |
| 96 | + return "-" |
| 97 | + return self._format_decimal(round(self._rating, 1)) |
| 98 | + |
| 99 | + @property |
| 100 | + def price(self) -> str: |
| 101 | + return self._format_currency(self._price) |
| 102 | + |
| 103 | + @property |
| 104 | + def value(self) -> str: |
| 105 | + return self._format_currency(self._value) |
| 106 | + |
| 107 | + def _format_decimal(self, number: float) -> str: |
| 108 | + return babel.numbers.format_decimal(number, locale=self.locale) |
106 | 109 |
|
107 |
| - if self.currency != "-": |
108 |
| - self.price_localized = babel.numbers.format_currency(self.price, self.currency, locale=config.locale) |
109 |
| - self.value_localized = babel.numbers.format_currency(self.value, self.currency, locale=config.locale) |
110 |
| - except babel.numbers.NumberFormatError: |
111 |
| - pass |
| 110 | + def _format_currency(self, number: float) -> str: |
| 111 | + if self.currency == "-": |
| 112 | + return self._format_decimal(number) |
| 113 | + return babel.numbers.format_currency(number, self.currency, locale=self.locale) |
112 | 114 |
|
113 | 115 | @staticmethod
|
114 | 116 | def _datetimeparse(datestr: str) -> datetime.datetime:
|
@@ -174,18 +176,18 @@ def pickupdate(self) -> str:
|
174 | 176 | """
|
175 | 177 | Returns a well formated string, providing the pickup time range
|
176 | 178 | """
|
177 |
| - if self.pickup_interval_start and self.pickup_interval_end: |
178 |
| - now = datetime.datetime.now() |
179 |
| - pfr = self._datetimeparse(self.pickup_interval_start) |
180 |
| - pto = self._datetimeparse(self.pickup_interval_end) |
181 |
| - prange = f"{pfr.hour:02d}:{pfr.minute:02d} - {pto.hour:02d}:{pto.minute:02d}" |
182 |
| - tommorow = now + datetime.timedelta(days=1) |
183 |
| - if now.date() == pfr.date(): |
184 |
| - return f"{humanize.naturalday(now)}, {prange}" |
185 |
| - if (pfr.date() - now.date()).days == 1: |
186 |
| - return f"{humanize.naturalday(tommorow)}, {prange}" |
187 |
| - return f"{pfr.day}/{pfr.month}, {prange}" |
188 |
| - return "-" |
| 179 | + if self.pickup_interval_start is None or self.pickup_interval_end is None: |
| 180 | + return "-" |
| 181 | + now = datetime.datetime.now() |
| 182 | + pfr = self._datetimeparse(self.pickup_interval_start) |
| 183 | + pto = self._datetimeparse(self.pickup_interval_end) |
| 184 | + prange = f"{pfr.hour:02d}:{pfr.minute:02d} - {pto.hour:02d}:{pto.minute:02d}" |
| 185 | + tommorow = now + datetime.timedelta(days=1) |
| 186 | + if now.date() == pfr.date(): |
| 187 | + return f"{humanize.naturalday(now)}, {prange}" |
| 188 | + if (pfr.date() - now.date()).days == 1: |
| 189 | + return f"{humanize.naturalday(tommorow)}, {prange}" |
| 190 | + return f"{pfr.day}/{pfr.month}, {prange}" |
189 | 191 |
|
190 | 192 | def _get_distance_time(self, travel_mode: str) -> Union[DistanceTime, None]:
|
191 | 193 | if self.location is None:
|
|
0 commit comments