Coverage for amazonorders/orders.py: 98.00%

50 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-17 01:52 +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.0" 

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, 

24 print_output: bool = False) -> None: 

25 #: 

26 self.amazon_session: AmazonSession = amazon_session 

27 

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

29 self.debug: bool = debug 

30 if self.debug: 

31 logger.setLevel(logging.DEBUG) 

32 #: 

33 self.print_output: bool = print_output 

34 

35 def get_order_history(self, 

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

37 start_index: Optional[int] = None, 

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

39 """ 

40 

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

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

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

44 :return: A list of the requested Orders. 

45 """ 

46 if not self.amazon_session.is_authenticated: 

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

48 

49 orders = [] 

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

51 year, 

52 "&startIndex={}".format( 

53 start_index) if start_index else "") 

54 while next_page: 

55 self.amazon_session.get(next_page) 

56 response_parsed = self.amazon_session.last_response_parsed 

57 

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

59 order = Order(order_tag) 

60 

61 if full_details: 

62 self.amazon_session.get(order.order_details_link) 

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

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

65 

66 orders.append(order) 

67 

68 next_page = None 

69 if start_index is None: 

70 try: 

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

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

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

74 except AttributeError: 

75 logger.debug("No next page") 

76 else: 

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

78 

79 if self.print_output: 

80 for order in orders: 

81 print(order) 

82 

83 return orders 

84 

85 def get_order(self, 

86 order_id: str) -> Order: 

87 """ 

88 

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

90 :return: The requested Order. 

91 """ 

92 if not self.amazon_session.is_authenticated: 

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

94 

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

96 

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

98 order = Order(order_details_tag, full_details=True) 

99 

100 if self.print_output: 

101 print(order) 

102 

103 return order