def main(): bank = Swedbank() bank.login() accounts = bank.get_accounts() if len(accounts) > 1: for account in accounts: print(u'{id}: {label} ({amount})'.format( id=account['id'], label=account['name'], amount=account['amount'] )) account_id = int(raw_input('Select an account: ')) elif len(accounts) == 1: account_id = 0 else: return u'No accounts found.' pages = int(raw_input( 'How many pages of transactions do you want to fetch?: ')) transactions = bank.get_transactions(account_id, pages) if len(transactions): print(u'Received {0} transactions'.format(len(transactions))) proceed = raw_input( 'Do you want to proceed to write the transactions to' ' the local database? [Y/n]: ') if proceed.lower() == 'y': account_data = accounts[account_id] account = Account.query.filter(Account.label == account_data['name']).first() if account is None: account = Account( label=account_data['name'], amount=account_data['amount']) db.session.add(account) db.session.commit() saved_transactions = save_transactions( account.id, transactions) print('Wrote {0} transactions to the database'.format( saved_transactions))
def update_accounts(user_id, ssn, code): from pengar.swedbank import Swedbank from pengar.util.update import save_transactions user = User.query.filter_by(id=user_id).first() if not user: raise UpdateError('Invalid user id.') if not ssn and code: raise UpdateError('Missing SSN or code') bank = Swedbank(ssn=ssn, code=code) try: bank.login() except AssertionError: raise UpdateError('Could not log in, are the SSN and code valid?') accounts = bank.get_accounts() for bank_account in accounts: transactions = bank.get_transactions(bank_account['id'], 100) account = Account.query.join(AccountsUsers).join(User)\ .filter(Account.label == bank_account['name'])\ .filter(User.id == user).first() if not account: account = Account( label=bank_account['name'], amount=bank_account['amount']) db.session.add(account) db.session.commit() au = AccountsUsers(user_id=user.id, account_id=account.id) db.session.add(au) db.session.commit() _log.info('Updating account {0}'.format(account)) return save_transactions( account.id, transactions) else: raise UpdateError('No accounts could be found.')