Coverage for amazonorders/orders.py: 97.73%

44 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-18 14:34 +0000

1import datetime 

2import logging 

3from typing import List, Optional 

4 

5from amazonorders.entity.order import Order 

6from amazonorders.exception import AmazonOrdersError 

7from amazonorders.session import BASE_URL, AmazonSession 

8 

9__author__ = "Alex Laird" 

10__copyright__ = "Copyright 2024, Alex Laird" 

11__version__ = "1.0.2" 

12 

13logger = logging.getLogger(__name__) 

14 

15 

16class AmazonOrders: 

17 """ 

18 

19 """ 

20 

21 def __init__(self, 

22 amazon_session: AmazonSession, 

23 debug: bool = False) -> None: 

24 #: The AmazonSession to use for requests. 

25 self.amazon_session: AmazonSession = amazon_session 

26 

27 #: Set logger ``DEBUG`` and send output to ``stderr``. 

28 self.debug: bool = debug 

29 if self.debug: 

30 logger.setLevel(logging.DEBUG) 

31 

32 def get_order_history(self, 

33 year: int = datetime.date.today().year, 

34 start_index: Optional[int] = None, 

35 full_details: bool = False) -> List[Order]: 

36 """ 

37 Get the Amazon order history for the given year. 

38 

39 :param year: The year for which to get history. 

40 :param start_index: The index to start at within the history. 

41 :param full_details: Will execute an additional request per Order in the retrieved history to fully populate it. 

42 :return: A list of the requested Orders. 

43 """ 

44 if not self.amazon_session.is_authenticated: 

45 raise AmazonOrdersError("Call AmazonSession.login() to authenticate first.") 

46 

47 orders = [] 

48 next_page = "{}/your-orders/orders?timeFilter=year-{}{}".format(BASE_URL, 

49 year, 

50 "&startIndex={}".format( 

51 start_index) if start_index else "") 

52 while next_page: 

53 self.amazon_session.get(next_page) 

54 response_parsed = self.amazon_session.last_response_parsed 

55 

56 for order_tag in response_parsed.find_all("div", {"class": "order-card"}): 

57 order = Order(order_tag) 

58 

59 if full_details: 

60 self.amazon_session.get(order.order_details_link) 

61 order_details_tag = self.amazon_session.last_response_parsed.find("div", id="orderDetails") 

62 order = Order(order_details_tag, full_details=True, clone=order) 

63 

64 orders.append(order) 

65 

66 next_page = None 

67 if start_index is None: 

68 try: 

69 next_page = "{}{}".format(BASE_URL, 

70 response_parsed.find("ul", {"class", "a-pagination"}).find( 

71 "li", {"class": "a-last"}).find("a").attrs["href"]) 

72 except AttributeError: 

73 logger.debug("No next page") 

74 else: 

75 logger.debug("start_index is given, not paging") 

76 

77 return orders 

78 

79 def get_order(self, 

80 order_id: str) -> Order: 

81 """ 

82 Get the Amazon order represented by the ID. 

83 

84 :param order_id: The Amazon Order ID to lookup. 

85 :return: The requested Order. 

86 """ 

87 if not self.amazon_session.is_authenticated: 

88 raise AmazonOrdersError("Call AmazonSession.login() to authenticate first.") 

89 

90 self.amazon_session.get("{}/gp/your-account/order-details?orderID={}".format(BASE_URL, order_id)) 

91 

92 order_details_tag = self.amazon_session.last_response_parsed.find("div", id="orderDetails") 

93 order = Order(order_details_tag, full_details=True) 

94 

95 return order