Skip to content

Commit

Permalink
CLI commands support added
Browse files Browse the repository at this point in the history
  • Loading branch information
Shmakov committed May 31, 2020
1 parent 06904d2 commit cdd6834
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ You can download the latest version from the GitHub's [releases tab](https://git
* Install the requirements: `pip install -r requirements.txt`
* And you should be able to launch the project: `python -m kroger_cli`

CLI Commands
------------

The application support non-interactive mode by passing a name of a command. An example on how to complete Kroger's Survey: `kroger-cli survey`.

Please use `kroger-cli --help` to see list of all available commands. Alternatively you can run the application without any command to launch the interactive mode (you can see the screenshot of it below).

Screenshots
-----------

Expand Down Expand Up @@ -60,6 +67,5 @@ The initial plan was to use plain HTTP (and `requests` package), however I could

### TODO

* Command Line Arguments, to allow something like that: `kroger-cli --clip-digital-coupons`
* Purchased items (receipt data) Excel export, which could be useful for budgeting/categorization/filtering
* Notification on when certain items go on sale
50 changes: 48 additions & 2 deletions kroger_cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
import click
from kroger_cli.cli import KrogerCLI

kroger_cli = KrogerCLI()


@click.group(invoke_without_command=True)
@click.pass_context
@click.option('--disable-headless', is_flag=True, help='Disable chromium\'s headless mode (useful for debug).')
def cli(ctx, disable_headless):
if disable_headless:
kroger_cli.api.browser_options['headless'] = False

# CLI call without a command
if ctx.invoked_subcommand is None:
kroger_cli.prompt_options()


@click.command('account-info', help='Display account info.')
def account_info():
kroger_cli.option_account_info()


@click.command('clip-coupons', help='Clip all digital coupons.')
def clip_coupons():
kroger_cli.option_clip_coupons()


@click.command('purchases-summary', help='Purchases Summary.')
def purchases_summary():
kroger_cli.option_purchases_summary()


@click.command('points-balance', help='Retrieve Points Balance.')
def points_balance():
kroger_cli.option_points_balance()


@click.command('survey', help='Complete Kroger’s Survey (to earn 50 points).')
def survey():
kroger_cli.option_survey()


if __name__ == '__main__':
cli = KrogerCLI()
cli.prompt_options()
cli.add_command(account_info)
cli.add_command(clip_coupons)
cli.add_command(purchases_summary)
cli.add_command(points_balance)
cli.add_command(survey)

cli()
36 changes: 18 additions & 18 deletions kroger_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ def __init__(self, config_file='config.ini'):
self.init()

def init(self):
if self.config['profile']['first_name'] != '':
self.console.print(Panel('[bold]Welcome Back, ' + self.config['profile']['first_name'] + '! :smiley:\n'
'[dark_blue]Kroger[/dark_blue] CLI[/bold]', box=box.ASCII))
else:
self.console.print(Panel('[bold]Welcome to [dark_blue]Kroger[/dark_blue] CLI[/bold] (unofficial command '
'line interface)', box=box.ASCII))

self.prompt_store_selection()

if self.username is None and self.config['main']['username'] != '':
Expand Down Expand Up @@ -65,6 +58,13 @@ def prompt_credentials(self):
self._set_credentials(username, password)

def prompt_options(self):
if self.config['profile']['first_name'] != '':
self.console.print(Panel('[bold]Welcome Back, ' + self.config['profile']['first_name'] + '! :smiley:\n'
'[dark_blue]Kroger[/dark_blue] CLI[/bold]', box=box.ASCII))
else:
self.console.print(Panel('[bold]Welcome to [dark_blue]Kroger[/dark_blue] CLI[/bold] (unofficial command '
'line interface)', box=box.ASCII))

while True:
self.console.print('[bold]1[/bold] - Display account info')
self.console.print('[bold]2[/bold] - Clip all digital coupons')
Expand All @@ -77,15 +77,15 @@ def prompt_options(self):
self.console.rule()

if option == 1:
self._option_account_info()
self.option_account_info()
elif option == 2:
self._option_clip_coupons()
self.option_clip_coupons()
elif option == 3:
self._option_purchases_summary()
self.option_purchases_summary()
elif option == 4:
self._option_points_balance()
self.option_points_balance()
elif option == 5:
self._option_survey()
self.option_survey()
elif option == 8:
self.prompt_credentials()
elif option == 9:
Expand Down Expand Up @@ -118,7 +118,7 @@ def _get_details_for_survey(self):
if self.config['profile']['first_name'] == '':
self.console.print('[bold]We need to retrieve the account info in order to fill out the survey form. '
'Please wait..[/bold]')
self._option_account_info()
self.option_account_info()

greetings = False
for field in helper.survey_mandatory_fields:
Expand All @@ -133,7 +133,7 @@ def _get_details_for_survey(self):
self.config['profile'][field] = str(inp)
self._write_config_file()

def _option_survey(self):
def option_survey(self):
self._get_details_for_survey()

result = self.api.complete_survey()
Expand All @@ -142,7 +142,7 @@ def _option_survey(self):
else:
self.console.print('[bold red]Couldn\'t complete the feedback form :([/bold red]')

def _option_account_info(self):
def option_account_info(self):
info = self.api.get_account_info()
if info is None:
self.console.print('[bold red]Couldn\'t retrieve the account info.[/bold red]')
Expand All @@ -151,7 +151,7 @@ def _option_account_info(self):
self._write_config_file()
self.console.print(self.config.items(section='profile'))

def _option_points_balance(self):
def option_points_balance(self):
balance = self.api.get_points_balance()
if balance is None:
self.console.print('[bold red]Couldn\'t retrieve the points balance.[/bold red]')
Expand All @@ -165,10 +165,10 @@ def _option_points_balance(self):
self.console.print(item['programDisplayInfo']['loyaltyProgramName'] + ': '
'[bold]' + item['programBalance']['balanceDescription'] + '[/bold]')

def _option_clip_coupons(self):
def option_clip_coupons(self):
self.api.clip_coupons()

def _option_purchases_summary(self):
def option_purchases_summary(self):
purchases = self.api.get_purchases_summary()
if purchases is None:
self.console.print('[bold red]Couldn\'t retrieve the purchases.[/bold red]')
Expand Down

0 comments on commit cdd6834

Please sign in to comment.