-
-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DO NOT MERGE!] [New feature]: Incorporate Delivery/Livraison bags #549
base: main
Are you sure you want to change the base?
Changes from all commits
5240088
6449ec7
34d6571
6776c66
b94e15b
b457bec
49d34b2
d2f9666
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,9 @@ def __init__(self, data: dict, location: Union[Location, None] = None, locale: s | |
store: dict = data.get("store", {}) | ||
self.store_name: str = store.get("store_name", "-") | ||
|
||
self.manufacturer_properties: dict = data.get("manufacturer_properties", {}) | ||
self.tags: list = data.get("tags", []) | ||
|
||
self.scanned_on: str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
self.location = location | ||
self.locale = locale | ||
|
@@ -221,3 +224,16 @@ def __getattribute__(self, __name: str) -> Any: | |
if _type == "duration": | ||
return self._get_duration(_mode) | ||
raise | ||
|
||
@classmethod | ||
def delivery_item_conversion(cls): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed because delivery bags have not the same attributes in the API call than Items |
||
""" | ||
Returns a mapping of "DeliveryItem" keys to Item keys. | ||
""" | ||
return { | ||
"subtitle": "description", | ||
"item_type": "item_category", | ||
"name": "display_name", | ||
"available_stock": "items_available", | ||
"cover_picture": "item_cover", | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import copy | ||
import logging | ||
import sys | ||
from random import random | ||
|
@@ -52,6 +53,7 @@ def __init__(self, config: Config): | |
self.item_ids = set(self.config.item_ids) | ||
self.cron = self.config.schedule_cron | ||
self.state: Dict[str, Item] = {} | ||
self.delivery_state: Dict[str, Item] = {} | ||
self.notifiers: Union[Notifiers, None] = None | ||
self.location: Union[Location, None] = None | ||
self.tgtg_client = TgtgClient( | ||
|
@@ -104,6 +106,15 @@ def _job(self) -> None: | |
except TgtgAPIError as err: | ||
log.error(err) | ||
items += self._get_favorites() | ||
|
||
# if state is empty (first scanning iteration), initialize it with the current favorite items | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SEE PR #548 |
||
# and set `items_available` property to 0. | ||
# It allows to be able to receive notifications at start if some magic bags are already available. | ||
if not self.state: | ||
self.state = {item.item_id: copy.deepcopy(item) for item in items} | ||
for item in self.state.values(): | ||
item.items_available = 0 | ||
|
||
for item in items: | ||
self._check_item(item) | ||
|
||
|
@@ -114,6 +125,9 @@ def _job(self) -> None: | |
if len(self.state) == 0: | ||
log.warning("No items in observation! Did you add any favorites?") | ||
|
||
if not self.config.disable_delivery_items: | ||
self._check_delivery_items() | ||
|
||
self.config.save_tokens( | ||
self.tgtg_client.access_token, | ||
self.tgtg_client.refresh_token, | ||
|
@@ -135,6 +149,41 @@ def _get_favorites(self) -> list[Item]: | |
return [] | ||
return [Item(item, self.location, self.config.locale) for item in items] | ||
|
||
def convert_raw_delivery_item(self, raw_delivery_item: dict, mapping: dict) -> Item: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conversion of delivery data, not in the same format as item data. Adding some logic to have correct format to build the Item object |
||
"""Converts a raw delivery item to an Item object using a mapping dictionary.""" | ||
item_data = raw_delivery_item.get("item", {}) | ||
|
||
# Create a new dictionary to hold the modified item data | ||
modified_item_data = {} | ||
|
||
# Update item data keys based on the mapping | ||
for key, value in item_data.items(): | ||
new_key = mapping.get(key, key) | ||
modified_item_data[new_key] = value | ||
|
||
# Flattening the original data and integrating the modified item data | ||
flattened_data = {**raw_delivery_item} | ||
flattened_data.update(modified_item_data) | ||
|
||
return Item(flattened_data, self.location, self.config.locale) | ||
|
||
def get_delivery_items(self) -> List[Item]: | ||
"""Returns delivery items available in the delivery panel. | ||
|
||
Returns: | ||
List: List of delivery items, still available in the delivery panel (not Out of stock) | ||
""" | ||
raw_delivery_items = self.tgtg_client.get_raw_delivery_items() | ||
|
||
delivery_items = [ | ||
self.convert_raw_delivery_item(raw_delivery_item, Item.delivery_item_conversion()) | ||
for raw_delivery_item in raw_delivery_items | ||
] | ||
|
||
in_stock_delivery_items = [item for item in delivery_items if item.items_available > 0] | ||
|
||
return in_stock_delivery_items | ||
|
||
def _check_item(self, item: Item) -> None: | ||
""" | ||
Checks if the available item amount raised from zero to something | ||
|
@@ -151,6 +200,28 @@ def _check_item(self, item: Item) -> None: | |
self.metrics.update(item) | ||
self.state[item.item_id] = item | ||
|
||
def _check_delivery_items(self) -> None: | ||
""" | ||
Check for new delivery items and send notifications if new items are available | ||
""" | ||
# 1. Retrieve the current delivery items data available on TGTG | ||
delivery_items: list[Item] = self.get_delivery_items() | ||
delivery_items_ids: list[str] = [item.item_id for item in delivery_items] | ||
|
||
# 2. Compare the delivery items with the current state | ||
for item in delivery_items: | ||
item_id = item.item_id | ||
if item_id not in self.delivery_state: | ||
# New item, send notification | ||
self._send_messages(item) | ||
self.metrics.send_notifications.labels(item_id, item.display_name).inc() | ||
self.delivery_state[item_id] = item | ||
|
||
# 3. Remove items that are no longer available - out of stock | ||
for item_id in list(self.delivery_state.keys()): | ||
if item_id not in delivery_items_ids: | ||
self.delivery_state.pop(item_id) | ||
|
||
def _send_messages(self, item: Item) -> None: | ||
""" | ||
Send notifications for Item | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
CREATE_ORDER_ENDPOINT = "order/v7/create/" | ||
ABORT_ORDER_ENDPOINT = "order/v7/{}/abort" | ||
ORDER_STATUS_ENDPOINT = "order/v7/{}/status" | ||
MANUFACTURERITEM_ENDPOINT = "manufactureritem/v2/" | ||
USER_AGENTS = [ | ||
"TGTG/{} Dalvik/2.1.0 (Linux; U; Android 9; Nexus 5 Build/M4B30Z)", | ||
"TGTG/{} Dalvik/2.1.0 (Linux; U; Android 10; SM-G935F Build/NRD90M)", | ||
|
@@ -403,3 +404,46 @@ def abort_order(self, order_id: str) -> None: | |
response = self._post(ABORT_ORDER_ENDPOINT.format(order_id), json={"cancel_reason_id": 1}) | ||
if response.json().get("state") != "SUCCESS": | ||
raise TgtgAPIError(response.status_code, response.content) | ||
|
||
def _extract_delivery_items(self, delivery_items_response_data: dict) -> List[dict]: | ||
""" | ||
Extracts all items from the delivery items response data. | ||
|
||
Args: | ||
delivery_items_response_data (dict): The response data from the TGTG API. | ||
|
||
Returns: | ||
List[dict]: List of all items in the response. | ||
""" | ||
all_delivery_items = [] | ||
for group in delivery_items_response_data.get("groups", []): | ||
all_delivery_items.extend(group.get("elements", [])) | ||
|
||
return [item for item in all_delivery_items] | ||
|
||
def get_raw_delivery_items(self): | ||
"""Returns all raw items in delivery from the TGTG API | ||
|
||
Returns: | ||
List: List of raw items | ||
""" | ||
# Commented element types have not been tested yet. | ||
response = self._post( | ||
MANUFACTURERITEM_ENDPOINT, | ||
json={ | ||
"action_types_accepted": ["QUERY"], | ||
"display_types_accepted": ["LIST"], | ||
"element_types_accepted": [ | ||
"ITEM", # All items/products in delivery | ||
"HIGHLIGHTED_ITEM", # Item with a special highlight on the top of the delivery pannel | ||
# "DUO_ITEMS", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not tested yet so still there but commented |
||
# "DUO_ITEMS_V2", | ||
# "TEXT", | ||
# "PARCEL_TEXT", | ||
# "NPS", | ||
], | ||
}, | ||
) | ||
|
||
items_data = self._extract_delivery_items(response.json()) | ||
return items_data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New attributes for delivery items