Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdlaird committed Jun 17, 2024
1 parent 8838adc commit ed612f3
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
30 changes: 18 additions & 12 deletions amazonorders/entity/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from bs4 import Tag

from amazonorders import constants, util
from amazonorders import util
from amazonorders.conf import AmazonOrdersConfig
from amazonorders.entity.parsable import Parsable
from amazonorders.entity.seller import Seller
Expand All @@ -22,31 +22,37 @@ class Item(Parsable):

def __init__(self,
parsed: Tag,
config: AmazonOrdersConfig,) -> None:
config: AmazonOrdersConfig, ) -> None:
super().__init__(parsed, config)

#: The Item title.
self.title: str = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_TITLE_SELECTOR, required=True)
self.title: str = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_TITLE_SELECTOR,
required=True)
#: The Item link.
self.link: str = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_LINK_SELECTOR, link=True, required=True)
self.link: str = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_LINK_SELECTOR,
link=True, required=True)
#: The Item price.
self.price: Optional[float] = self.to_currency(
self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_TAG_ITERATOR_SELECTOR,
prefix_split="$"))
#: The Item Seller.
self.seller: Optional[Seller] = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_TAG_ITERATOR_SELECTOR,
text_contains="Sold by:",
wrap_tag=Seller)
self.seller: Optional[Seller] = self.safe_simple_parse(
selector=self.config.selectors.FIELD_ITEM_TAG_ITERATOR_SELECTOR,
text_contains="Sold by:",
wrap_tag=Seller)
#: The Item condition.
self.condition: Optional[str] = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_TAG_ITERATOR_SELECTOR,
prefix_split="Condition:")
self.condition: Optional[str] = self.safe_simple_parse(
selector=self.config.selectors.FIELD_ITEM_TAG_ITERATOR_SELECTOR,
prefix_split="Condition:")
#: The Item return eligible date.
self.return_eligible_date: Optional[date] = self.safe_parse(self._parse_return_eligible_date)
#: The Item image URL.
self.image_link: Optional[str] = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_IMG_LINK_SELECTOR,
link=True)
self.image_link: Optional[str] = self.safe_simple_parse(
selector=self.config.selectors.FIELD_ITEM_IMG_LINK_SELECTOR,
link=True)
#: The Item quantity.
self.quantity: Optional[int] = self.safe_simple_parse(selector=self.config.selectors.FIELD_ITEM_QUANTITY_SELECTOR)
self.quantity: Optional[int] = self.safe_simple_parse(
selector=self.config.selectors.FIELD_ITEM_QUANTITY_SELECTOR)

def __repr__(self) -> str:
return f"<Item: \"{self.title}\">"
Expand Down
14 changes: 9 additions & 5 deletions amazonorders/entity/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from bs4 import BeautifulSoup, Tag

from amazonorders import constants, util
from amazonorders import util
from amazonorders.conf import AmazonOrdersConfig
from amazonorders.entity.item import Item
from amazonorders.entity.parsable import Parsable
Expand Down Expand Up @@ -83,7 +83,8 @@ def __str__(self) -> str: # pragma: no cover
return f"Order #{self.order_number}: {self.items}"

def _parse_shipments(self) -> List[Shipment]:
shipments = [Shipment(x, self.config) for x in util.select(self.parsed, self.config.selectors.SHIPMENT_ENTITY_SELECTOR)]
shipments = [Shipment(x, self.config) for x in util.select(self.parsed,
self.config.selectors.SHIPMENT_ENTITY_SELECTOR)]
shipments.sort()
return shipments

Expand Down Expand Up @@ -160,7 +161,8 @@ def _parse_recipient(self) -> Recipient:
if not value:
# TODO: there are multiple shipToData tags, we should double check we're picking the right one
# associated with the order
parent_tag = util.select_one(self.parsed.find_parent(), self.config.selectors.FIELD_ORDER_ADDRESS_FALLBACK_2_SELECTOR)
parent_tag = util.select_one(self.parsed.find_parent(),
self.config.selectors.FIELD_ORDER_ADDRESS_FALLBACK_2_SELECTOR)
value = BeautifulSoup(str(parent_tag.contents[0]).strip(), "html.parser")

return Recipient(value, self.config)
Expand Down Expand Up @@ -259,7 +261,8 @@ def _parse_refund_total(self) -> Optional[float]:
return value

def _parse_order_shipping_date(self) -> Optional[date]:
value = self.simple_parse(self.config.selectors.FIELD_ORDER_SHIPPED_DATE_SELECTOR, prefix_split="Items shipped:")
value = self.simple_parse(self.config.selectors.FIELD_ORDER_SHIPPED_DATE_SELECTOR,
prefix_split="Items shipped:")

if value:
date_str = value.split("-")[0].strip()
Expand All @@ -268,7 +271,8 @@ def _parse_order_shipping_date(self) -> Optional[date]:
return value

def _parse_refund_completed_date(self) -> Optional[date]:
value = self.simple_parse(self.config.selectors.FIELD_ORDER_REFUND_COMPLETED_DATE, prefix_split="Refund: Completed")
value = self.simple_parse(self.config.selectors.FIELD_ORDER_REFUND_COMPLETED_DATE,
prefix_split="Refund: Completed")

if value:
date_str = value.split("-")[0].strip()
Expand Down
6 changes: 4 additions & 2 deletions amazonorders/entity/seller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ def __init__(self,
super().__init__(parsed, config)

#: The Seller name.
self.name: str = self.safe_simple_parse(self.config.selectors.FIELD_SELLER_NAME_SELECTOR, prefix_split="Sold by:")
self.name: str = self.safe_simple_parse(self.config.selectors.FIELD_SELLER_NAME_SELECTOR,
prefix_split="Sold by:")
#: The Seller link.
self.link: Optional[str] = self.safe_simple_parse(selector=self.config.selectors.FIELD_SELLER_LINK_SELECTOR, link=True)
self.link: Optional[str] = self.safe_simple_parse(selector=self.config.selectors.FIELD_SELLER_LINK_SELECTOR,
link=True)

def __repr__(self) -> str:
return f"<Seller: \"{self.name}\">"
Expand Down
2 changes: 1 addition & 1 deletion amazonorders/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from amazoncaptcha import AmazonCaptcha
from bs4 import Tag

from amazonorders import constants, util
from amazonorders import util
from amazonorders.conf import AmazonOrdersConfig
from amazonorders.exception import AmazonOrdersAuthError, AmazonOrdersError

Expand Down
6 changes: 3 additions & 3 deletions amazonorders/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging
import os
from typing import Any, Optional, List
from typing import Any, List, Optional
from urllib.parse import urlparse

import requests
Expand Down Expand Up @@ -201,8 +201,8 @@ def login(self) -> None:
self.get(self.config.constants.SIGN_IN_URL)

# If our local session data is stale, Amazon will redirect us to the signin page
if self.auth_cookies_stored() and self.last_response.url.split("?")[
0] == self.config.constants.SIGN_IN_REDIRECT_URL:
if (self.auth_cookies_stored() and
self.last_response.url.split("?")[0] == self.config.constants.SIGN_IN_REDIRECT_URL):
self.logout()
self.get(self.config.constants.SIGN_IN_URL)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def test_get_order_history_paginated(self):
encoding="utf-8") as f:
resp3 = responses.add(
responses.GET,
f"{self.test_config.constants.ORDER_HISTORY_URL}?timeFilter=year-{year}&startIndex=10&ref_=ppx_yo2ov_dt_b_pagination_1_2",
f"{self.test_config.constants.ORDER_HISTORY_URL}?timeFilter=year-{year}"
"&startIndex=10&ref_=ppx_yo2ov_dt_b_pagination_1_2",
body=f.read(),
status=200,
)
Expand Down

0 comments on commit ed612f3

Please sign in to comment.