diff --git a/nlbcli/__main__.py b/nlbcli/__main__.py index ed850b6..e8544c6 100644 --- a/nlbcli/__main__.py +++ b/nlbcli/__main__.py @@ -4,6 +4,7 @@ import sys import os import re +from . import formatter from datetime import date, timedelta # local files @@ -41,15 +42,8 @@ def main(): url = 'https://www.nlbklik.com.mk/Retail/Account' _, soup = nlb_post(url, data) table_cells = soup.select('.dps-content') - print('Account Id:', parsed_args.account_id) - print('Account owner:', table_cells[0].text) - print('Status:', table_cells[1].text) - print('Current balance:', table_cells[3].text) - print('Available balance:', table_cells[5].text) - print('Allowed overdraft:', table_cells[7].text) - print('Reserved funds:', table_cells[9].text) - print('Last change:', table_cells[13].text) - print('Last interest:', table_cells[15].text) + # Print the data in the desired output format + formatter.OutputResult(parsed_args, table_cells, "accounts") elif parsed_args.accounts_subparser_name == 'transactions': # direction explanation: @@ -70,7 +64,7 @@ def main(): "SaveFilter": "False", "RemoveFilter": "False", "PageNumber": "", - "PageSize": "", + "PageSize": "100", "DetailsView": "0", "SelectedItem": "", "PageId": "", @@ -95,9 +89,7 @@ def main(): # we print all cells except the "Details" link/button # todo: parse the 0th column to extract the unique transaction id - for tr in soup.select('tbody > tr'): - tds = tr.select('td')[1:] - print('\t'.join(td.text.strip() for td in tds)) + formatter.OutputResult(parsed_args, soup.select('tbody > tr'), "transactions") elif parsed_args.accounts_subparser_name == 'reservations': url = 'https://www.nlbklik.com.mk/Retail/ReservationList' diff --git a/nlbcli/args_parser.py b/nlbcli/args_parser.py index c0d3940..4855ca8 100644 --- a/nlbcli/args_parser.py +++ b/nlbcli/args_parser.py @@ -7,6 +7,8 @@ month_ago = today - timedelta(days=30) parser = argparse.ArgumentParser() +# FIXME: this would need to be enabled so that every command has the --format argument, instead of just nlbcli accounts balance +#parser.add_argument('--format', nargs="?", choices=['tab','csv','json'], required=False, default='tab') main_subparsers = parser.add_subparsers(dest='subparser_name') login_parser = main_subparsers.add_parser( 'login', help='Log in and save your credentials.') @@ -30,10 +32,12 @@ transactions_parser.add_argument( '--type', choices=['in', 'out'], required=False) transactions_parser.add_argument('--name', required=False) +transactions_parser.add_argument('--format', nargs="?", choices=['tab','csv','json'], required=False, default='tab') # ACCOUNTS [id] balance balance_parser = accounts_subparsers.add_parser( 'balance', help="Show the balance on the specified account") +balance_parser.add_argument('--format', nargs="?", choices=['tab','csv','json'], required=False, default='tab') # ACCOUNtS [id] reservations reservations_parser = accounts_subparsers.add_parser( diff --git a/nlbcli/formatter.py b/nlbcli/formatter.py new file mode 100644 index 0000000..06c6162 --- /dev/null +++ b/nlbcli/formatter.py @@ -0,0 +1,98 @@ +import re +import csv +import json +import sys + + +def OutputResult(parsed_args, table_cells, input_type): + output_format = parsed_args.format + if output_format == 'tab': + if input_type == "accounts": + print('Account Id:', parsed_args.account_id) + print('Account owner:', table_cells[0].text) + print('Status:', table_cells[1].text) + print('Current balance:', table_cells[3].text) + print('Available balance:', table_cells[5].text) + print('Allowed overdraft:', table_cells[7].text) + print('Reserved funds:', table_cells[9].text) + print('Last change:', table_cells[13].text) + print('Last interest:', table_cells[15].text) + elif input_type == "transactions": + for tr in table_cells: + tds = tr.select('td')[1:] + print('\t'.join(td.text.strip() for td in tds)) + elif output_format == 'json': + if input_type == "accounts": + json_output = json.dumps( + { + 'account-id': parsed_args.account_id, + 'account-owner': table_cells[0].text, + 'account-status': table_cells[1].text, + 'current-balance': table_cells[3].text, + 'available-balance': table_cells[5].text, + 'allowed-overdraft': table_cells[7].text, + 'reserved-funds': table_cells[9].text, + 'last-change': table_cells[13].text, + 'last-interest': table_cells[15].text + },indent=4 + ) + elif input_type == "transactions": + txns = [] + for tr in table_cells: + tds = tr.select('td')[1:] + dict_txn = {} + dict_txn['date'] = tds[0].text.strip() + dict_txn['recipient_name'] = tds[1].text.strip() + dict_txn['txn_description'] = tds[2].text.strip() + dict_txn['status'] = tds[3].text.strip() + dict_txn['amount'] = tds[4].text.strip() + dict_txn['txn_fee'] = tds[5].text.strip() + dict_txn['balance'] = tds[6].text.strip() + txns.append(dict_txn) + json_output = json.dumps(txns,indent=4) + print(json_output) + elif output_format == 'csv': + if input_type == "accounts": + field_names = ['account-id', + 'account-owner', + 'account-status', + 'current-balance', + 'available-balance', + 'allowed-overdraft', + 'reserved-funds', + 'last-change', + 'last-interest'] + csv_writer = csv.DictWriter(sys.stdout, field_names) + csv_writer.writeheader() + csv_writer.writerow({ + 'account-id': parsed_args.account_id, + 'account-owner': table_cells[0].text, + 'account-status': table_cells[1].text, + 'current-balance': table_cells[3].text, + 'available-balance': table_cells[5].text, + 'allowed-overdraft': table_cells[7].text, + 'reserved-funds': table_cells[9].text, + 'last-change': table_cells[13].text, + 'last-interest': table_cells[15].text + }) + elif input_type == "transactions": + field_names = ['date', + 'recipient_name', + 'txn_description', + 'status', + 'amount', + 'txn_fee', + 'balance'] + csv_writer = csv.DictWriter(sys.stdout, field_names) + csv_writer.writeheader() + for tr in table_cells: + tds = tr.select('td')[1:] + csv_writer.writerow({ + 'date': tds[0].text.strip(), + 'recipient_name': tds[1].text.strip(), + 'txn_description': tds[2].text.strip(), + 'status': tds[3].text.strip(), + 'amount': tds[4].text.strip(), + 'txn_fee': tds[5].text.strip(), + 'balance': tds[6].text.strip(), + }) diff --git a/setup.py b/setup.py index e1df798..da3bc57 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ 'console_scripts': ['nlbcli=nlbcli.__main__:main'] }, install_requires=[ + 'setuptools', 'requests>=2.25.0', 'beautifulsoup4>=4.9.3' ],