Coverage for amazonorders/cli.py: 74.67%

75 statements  

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

1import datetime 

2import logging 

3import os 

4from typing import Any 

5 

6import click 

7from click.core import Context 

8 

9from amazonorders.exception import AmazonOrdersError 

10from amazonorders.orders import AmazonOrders 

11from amazonorders.session import AmazonSession 

12 

13__author__ = "Alex Laird" 

14__copyright__ = "Copyright 2024, Alex Laird" 

15__version__ = "1.0.1" 

16 

17logger = logging.getLogger("amazonorders") 

18 

19 

20@click.group() 

21@click.option('--username', help="An Amazon username.") 

22@click.option('--password', help="An Amazon password.") 

23@click.option('--debug', is_flag=True, default=False, help="Enable debugging and send output to command line.") 

24@click.pass_context 

25def amazon_orders_cli(ctx, 

26 **kwargs: Any): 

27 """ 

28 amazon-orders is an unofficial library that provides a command line interface alongside a programmatic API that 

29 can be used to interact with Amazon.com's consumer-facing website. 

30 

31 This works by parsing website data from Amazon.com. A nightly build validates functionality to ensure its 

32 stability, but as Amazon provides no official API to use, this package may break at any time. This 

33 package only supports the English version of the website. 

34 

35 Documentation can be found at https://amazon-orders.readthedocs.io. 

36 

37 Session data is persisted between requests and interactions with the CLI, minimizing the need to reauthenticate 

38 after a successful login attempt. 

39 """ 

40 ctx.ensure_object(dict) 

41 for key, value in kwargs.items(): 

42 if value: 

43 ctx.obj[key] = value 

44 

45 if kwargs["debug"]: 

46 logger.setLevel(logging.DEBUG) 

47 logger.addHandler(logging.StreamHandler()) 

48 

49 username = kwargs.get("username") 

50 password = kwargs.get("password") 

51 

52 amazon_session = AmazonSession(username, 

53 password, 

54 debug=kwargs["debug"]) 

55 

56 if amazon_session.auth_cookies_stored(): 

57 if username or password: 

58 click.echo("Previous session persisted, ignoring the provided --username and --password. If you " 

59 "would like to reauthenticate, call the `logout` command first to reauthenticate.") 

60 elif not username and not password: 

61 click.echo(ctx.get_help()) 

62 

63 ctx.fail("No previous sessions persisted, Amazon --username and --password must be provided.") 

64 

65 ctx.obj["amazon_session"] = amazon_session 

66 

67 

68@amazon_orders_cli.command() 

69@click.pass_context 

70@click.option('--year', default=datetime.date.today().year, 

71 help="The year for which to get order history, defaults to the current year.") 

72@click.option('--start-index', help="Retrieve the single page of history at the given index.") 

73@click.option('--full-details', is_flag=True, default=False, 

74 help="Retrieve the full details for each order in the history.") 

75def history(ctx: Context, 

76 **kwargs: Any): 

77 """ 

78 Retrieve Amazon order history for a given year. 

79 """ 

80 amazon_session = ctx.obj["amazon_session"] 

81 

82 try: 

83 amazon_session.login() 

84 

85 amazon_orders = AmazonOrders(amazon_session, 

86 debug=amazon_session.debug, 

87 print_output=True) 

88 

89 amazon_orders.get_order_history(year=kwargs["year"], 

90 start_index=kwargs["start_index"], 

91 full_details=kwargs["full_details"]) 

92 except AmazonOrdersError as e: 

93 logger.debug("An error occurred.", exc_info=True) 

94 ctx.fail(str(e)) 

95 

96 

97@amazon_orders_cli.command() 

98@click.pass_context 

99@click.argument("order_id") 

100def order(ctx: Context, 

101 order_id: str): 

102 """ 

103 Retrieve the full details for the given Amazon order ID. 

104 """ 

105 amazon_session = ctx.obj["amazon_session"] 

106 

107 try: 

108 amazon_session.login() 

109 

110 amazon_orders = AmazonOrders(amazon_session, 

111 debug=amazon_session.debug, 

112 print_output=True) 

113 

114 amazon_orders.get_order(order_id) 

115 except AmazonOrdersError as e: 

116 logger.debug("An error occurred.", exc_info=True) 

117 ctx.fail(str(e)) 

118 

119 

120@amazon_orders_cli.command(short_help="Check if persisted session exists.") 

121@click.pass_context 

122def check_session(ctx: Context): 

123 """ 

124 Check if persisted session exists, meaning commands can be called without needing to provide credentials. 

125 """ 

126 amazon_session = ctx.obj["amazon_session"] 

127 if amazon_session.auth_cookies_stored(): 

128 click.echo("A persisted session exists.") 

129 else: 

130 click.echo("No persisted session exists.") 

131 

132 

133@amazon_orders_cli.command() 

134@click.pass_context 

135def logout(ctx: Context): 

136 """ 

137 Logout of existing Amazon sessions and clear cookies. 

138 """ 

139 amazon_session = ctx.obj["amazon_session"] 

140 amazon_session.logout() 

141 

142 click.echo("Successfully logged out of the Amazon session.") 

143 

144 

145if __name__ == "__main__": 

146 amazon_orders_cli(obj={})