forked from alexdlaird/amazon-orders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add transaction fetching support alexdlaird#20
- Loading branch information
1 parent
e366daf
commit c1de1e6
Showing
17 changed files
with
630 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
__copyright__ = "Copyright (c) 2024 Jeff Sawatzky" | ||
__license__ = "MIT" | ||
|
||
import logging | ||
import re | ||
from datetime import date | ||
|
||
from bs4 import Tag | ||
|
||
from amazonorders.conf import AmazonOrdersConfig | ||
from amazonorders.entity.parsable import Parsable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Transaction(Parsable): | ||
""" | ||
An Amazon Transaction | ||
""" | ||
|
||
def __init__(self, parsed: Tag, config: AmazonOrdersConfig, completed_date: date) -> None: | ||
super().__init__(parsed, config) | ||
|
||
#: The Transaction completed date. | ||
self.completed_date: date = completed_date | ||
#: The Transaction payment method. | ||
self.payment_method: str = self.safe_simple_parse( | ||
selector=self.config.selectors.FIELD_TRANSACTION_PAYMENT_METHOD_SELECTOR | ||
) | ||
#: The Transaction grand total. | ||
self.grand_total: float = self.safe_parse(self._parse_grand_total) | ||
#: The Transaction was a refund or not. | ||
self.is_refund: bool = self.grand_total > 0 | ||
#: The Transaction order number. | ||
self.order_number: str = self.safe_parse(self._parse_order_number) | ||
#: The Transaction seller name. | ||
self.seller: str = self.safe_simple_parse( | ||
selector=self.config.selectors.FIELD_TRANSACTION_SELLER_NAME_SELECTOR | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return f'<Transaction {self.completed_date}: "Order #{self.order_number}", "Grand Total {self.grand_total}">' | ||
|
||
def __str__(self) -> str: # pragma: no cover | ||
return f"Transaction {self.completed_date}: Order #{self.order_number}, Grand Total {self.grand_total}" | ||
|
||
def _parse_grand_total(self) -> float: | ||
value = self.simple_parse(self.config.selectors.FIELD_TRANSACTION_GRAND_TOTAL_SELECTOR) | ||
value = self.to_currency(value) | ||
|
||
return value | ||
|
||
def _parse_order_number(self) -> str: | ||
value = self.simple_parse(self.config.selectors.FIELD_TRANSACTION_ORDER_NUMBER_SELECTOR) | ||
match = re.match(".*#([0-9-]+)$", value) | ||
value = match.group(1) if match else "" | ||
|
||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
__copyright__ = "Copyright (c) 2024 Jeff Sawatzky" | ||
__license__ = "MIT" | ||
|
||
import datetime | ||
from typing import Dict, List, Optional, Tuple | ||
|
||
from bs4 import Tag | ||
|
||
from amazonorders.conf import AmazonOrdersConfig | ||
from amazonorders.entity.transaction import Transaction | ||
|
||
|
||
def parse_transaction_form_tag( | ||
form_tag: Tag, config: AmazonOrdersConfig | ||
) -> Tuple[List[Transaction], Optional[str], Optional[Dict[str, str]]]: | ||
transactions = [] | ||
date_container_tags = form_tag.select( | ||
config.selectors.TRANSACTION_DATE_CONTAINERS_SELECTOR | ||
) | ||
for date_container_tag in date_container_tags: | ||
date_tag = date_container_tag.select_one( | ||
config.selectors.FIELD_TRANSACTION_COMPLETED_DATE_SELECTOR | ||
) | ||
assert date_tag is not None | ||
|
||
date_str = date_tag.text | ||
date = datetime.datetime.strptime( | ||
date_str, config.constants.TRANSACTION_DATE_FORMAT | ||
).date() | ||
|
||
transactions_container_tag = date_container_tag.find_next_sibling( | ||
config.selectors.TRANSACTIONS_CONTAINER_SELECTOR | ||
) | ||
assert isinstance(transactions_container_tag, Tag) | ||
|
||
transaction_tags = transactions_container_tag.select( | ||
config.selectors.TRANSACTIONS_SELECTOR | ||
) | ||
for transaction_tag in transaction_tags: | ||
transaction = Transaction(transaction_tag, config, date) | ||
transactions.append(transaction) | ||
|
||
form_state_input = form_tag.select_one( | ||
config.selectors.TRANSACTIONS_NEXT_PAGE_INPUT_STATE_SELECTOR | ||
) | ||
form_ie_input = form_tag.select_one( | ||
config.selectors.TRANSACTIONS_NEXT_PAGE_INPUT_IE_SELECTOR | ||
) | ||
next_page_input = form_tag.select_one( | ||
config.selectors.TRANSACTIONS_NEXT_PAGE_INPUT_SELECTOR | ||
) | ||
if not next_page_input or not form_state_input or not form_ie_input: | ||
return (transactions, None, None) | ||
|
||
next_page_post_url = str(form_tag["action"]) | ||
next_page_post_data = { | ||
"ppw-widgetState": str(form_state_input["value"]), | ||
"ie": str(form_ie_input["value"]), | ||
str(next_page_input["name"]): "", | ||
} | ||
|
||
return (transactions, next_page_post_url, next_page_post_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.