|
| 1 | +import configparser |
| 2 | +import os |
| 3 | +import click |
| 4 | +import time |
| 5 | +from rich.console import Console |
| 6 | +from rich.panel import Panel |
| 7 | +from rich import box |
| 8 | +from KrogerAPI import * |
| 9 | + |
| 10 | + |
| 11 | +class KrogerCLI: |
| 12 | + config_file = 'config.ini' |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.config = configparser.ConfigParser() |
| 16 | + self.username = None |
| 17 | + self.password = None |
| 18 | + self.console = Console() |
| 19 | + self.api = KrogerAPI(self) |
| 20 | + if not os.path.exists(self.config_file): |
| 21 | + self._init_config_file() |
| 22 | + self.config.read(self.config_file) |
| 23 | + self.init() |
| 24 | + |
| 25 | + def init(self): |
| 26 | + if self.config['profile']['first_name'] != '': |
| 27 | + self.console.print(Panel('[bold]Welcome Back, ' + self.config['profile']['first_name'] + '! :smiley:\n' |
| 28 | + '[dark_blue]Kroger[/dark_blue] CLI[/bold]', box=box.ASCII)) |
| 29 | + else: |
| 30 | + self.console.print(Panel('[bold]Welcome to [dark_blue]Kroger[/dark_blue] CLI[/bold] (unofficial command ' |
| 31 | + 'line interface)', box=box.ASCII)) |
| 32 | + |
| 33 | + self.prompt_store_selection() |
| 34 | + |
| 35 | + if self.username is None and self.config['main']['username'] != '': |
| 36 | + self.username = self.config['main']['username'] |
| 37 | + self.password = self.config['main']['password'] |
| 38 | + else: |
| 39 | + self.prompt_credentials() |
| 40 | + |
| 41 | + self.prompt_options() |
| 42 | + |
| 43 | + def prompt_store_selection(self): |
| 44 | + pass |
| 45 | + # TODO: |
| 46 | + # self.console.print('Please select preferred store') |
| 47 | + |
| 48 | + def prompt_credentials(self): |
| 49 | + self.console.print('In order to continue, please enter your username (email) and password for kroger.com ' |
| 50 | + '(also works with Ralphs, Dillons, Smith’s and other Kroger’s Chains)') |
| 51 | + username = click.prompt('Username (email)') |
| 52 | + password = click.prompt('Password') |
| 53 | + self._set_credentials(username, password) |
| 54 | + |
| 55 | + def prompt_options(self): |
| 56 | + while True: |
| 57 | + self.console.print('[bold]1[/bold] - Display account info') |
| 58 | + self.console.print('[bold]2[/bold] - Clip all digital coupons') |
| 59 | + self.console.print('[bold]8[/bold] - Re-Enter username/password') |
| 60 | + self.console.print('[bold]9[/bold] - Exit') |
| 61 | + option = click.prompt('Please select from one of the options', type=int) |
| 62 | + |
| 63 | + if option == 1: |
| 64 | + self._option_account_info() |
| 65 | + elif option == 2: |
| 66 | + self._option_clip_coupons() |
| 67 | + elif option == 8: |
| 68 | + self.prompt_credentials() |
| 69 | + elif option == 9: |
| 70 | + return |
| 71 | + |
| 72 | + self.console.rule() |
| 73 | + time.sleep(2) |
| 74 | + |
| 75 | + def _write_config_file(self): |
| 76 | + with open(self.config_file, 'w') as f: |
| 77 | + self.config.write(f) |
| 78 | + |
| 79 | + def _init_config_file(self): |
| 80 | + self.config.add_section('main') |
| 81 | + self.config['main']['username'] = '' |
| 82 | + self.config['main']['password'] = '' |
| 83 | + self.config['main']['domain'] = 'kroger.com' |
| 84 | + self.config.add_section('profile') |
| 85 | + self.config['profile']['first_name'] = '' |
| 86 | + self._write_config_file() |
| 87 | + |
| 88 | + def _set_credentials(self, username, password): |
| 89 | + self.username = username |
| 90 | + self.password = password |
| 91 | + self.config['main']['username'] = self.username |
| 92 | + self.config['main']['password'] = self.password |
| 93 | + self._write_config_file() |
| 94 | + |
| 95 | + def _option_account_info(self): |
| 96 | + info = self.api.get_account_info() |
| 97 | + if info is None: |
| 98 | + self.console.print('[bold red]Couldn\'t retrieve the account info.[/bold red]') |
| 99 | + else: |
| 100 | + self.config['profile']['first_name'] = info['firstName'] |
| 101 | + self.config['profile']['last_name'] = info['lastName'] |
| 102 | + self.config['profile']['email_address'] = info['emailAddress'] |
| 103 | + self.config['profile']['loyalty_card_number'] = info['loyaltyCardNumber'] |
| 104 | + self.config['profile']['mobile_phone'] = info['mobilePhoneNumber'] |
| 105 | + self.config['profile']['address_line1'] = info['address']['addressLine1'] |
| 106 | + self.config['profile']['address_line2'] = info['address']['addressLine2'] |
| 107 | + self.config['profile']['city'] = info['address']['city'] |
| 108 | + self.config['profile']['state'] = info['address']['stateCode'] |
| 109 | + self.config['profile']['zip'] = info['address']['zip'] |
| 110 | + self._write_config_file() |
| 111 | + self.console.print(self.config.items(section='profile')) |
| 112 | + |
| 113 | + def _option_clip_coupons(self): |
| 114 | + self.api.clip_coupons() |
0 commit comments