Coverage for amazonorders/cli.py: 74.39%

82 statements  

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

1import datetime 

2import logging 

3from typing import Any 

4 

5import click 

6from click.core import Context 

7 

8from amazonorders.exception import AmazonOrdersError 

9from amazonorders.orders import AmazonOrders 

10from amazonorders.session import AmazonSession, IODefault 

11 

12__author__ = "Alex Laird" 

13__copyright__ = "Copyright 2024, Alex Laird" 

14__version__ = "1.0.2" 

15 

16logger = logging.getLogger("amazonorders") 

17 

18 

19class IOClick(IODefault): 

20 def echo(self, msg): 

21 click.echo(msg) 

22 

23 def prompt(self, msg, type=None): 

24 return click.prompt(msg, type=type) 

25 

26 

27@click.group() 

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

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

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

31@click.pass_context 

32def amazon_orders_cli(ctx, 

33 **kwargs: Any): 

34 """ 

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

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

37 

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

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

40 package only supports the English version of the website. 

41 

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

43 

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

45 after a successful login attempt. 

46 """ 

47 ctx.ensure_object(dict) 

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

49 if value: 

50 ctx.obj[key] = value 

51 

52 if kwargs["debug"]: 

53 logger.setLevel(logging.DEBUG) 

54 logger.addHandler(logging.StreamHandler()) 

55 

56 username = kwargs.get("username") 

57 password = kwargs.get("password") 

58 

59 amazon_session = AmazonSession(username, 

60 password, 

61 debug=kwargs["debug"], 

62 io=IOClick()) 

63 

64 if amazon_session.auth_cookies_stored(): 

65 if username or password: 

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

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

68 elif not username and not password: 

69 click.echo(ctx.get_help()) 

70 

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

72 

73 ctx.obj["amazon_session"] = amazon_session 

74 

75 

76@amazon_orders_cli.command() 

77@click.pass_context 

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

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

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

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

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

83def history(ctx: Context, 

84 **kwargs: Any): 

85 """ 

86 Retrieve Amazon order history for a given year. 

87 """ 

88 amazon_session = ctx.obj["amazon_session"] 

89 

90 try: 

91 amazon_session.login() 

92 

93 amazon_orders = AmazonOrders(amazon_session, 

94 debug=amazon_session.debug) 

95 

96 orders = amazon_orders.get_order_history(year=kwargs["year"], 

97 start_index=kwargs["start_index"], 

98 full_details=kwargs["full_details"]) 

99 

100 for o in orders: 

101 click.echo(o) 

102 except AmazonOrdersError as e: 

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

104 ctx.fail(str(e)) 

105 

106 

107@amazon_orders_cli.command() 

108@click.pass_context 

109@click.argument("order_id") 

110def order(ctx: Context, 

111 order_id: str): 

112 """ 

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

114 """ 

115 amazon_session = ctx.obj["amazon_session"] 

116 

117 try: 

118 amazon_session.login() 

119 

120 amazon_orders = AmazonOrders(amazon_session, 

121 debug=amazon_session.debug) 

122 

123 o = amazon_orders.get_order(order_id) 

124 

125 click.echo(o) 

126 except AmazonOrdersError as e: 

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

128 ctx.fail(str(e)) 

129 

130 

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

132@click.pass_context 

133def check_session(ctx: Context): 

134 """ 

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

136 """ 

137 amazon_session = ctx.obj["amazon_session"] 

138 if amazon_session.auth_cookies_stored(): 

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

140 else: 

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

142 

143 

144@amazon_orders_cli.command() 

145@click.pass_context 

146def logout(ctx: Context): 

147 """ 

148 Logout of existing Amazon sessions and clear cookies. 

149 """ 

150 amazon_session = ctx.obj["amazon_session"] 

151 amazon_session.logout() 

152 

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

154 

155 

156if __name__ == "__main__": 

157 amazon_orders_cli(obj={})