def account(request, account_id): app_name = request.build_absolute_uri().split('.')[0][8:] client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) try: settings = Settings.objects.filter()[0] settings.last_used_account = account_id settings.save() except: Settings.objects.create(last_used_account=account_id) try: webhooks = client.get_webhooks(account_id)['webhooks'] print(webhooks) webhook_active = bool(webhooks) accounts = client.get_accounts()['accounts'] print(accounts) tags = Tag.objects.all() except (BadRequestError, UnauthorizedError): return render(request, 'invalid-token.html', {'app_name': app_name}) context = { 'app_name': app_name, 'accounts': accounts, 'account_id': account_id, 'tags': tags, 'webhook_active': webhook_active, 'strftime_codes': strftime_code } return render(request, 'account.html', context)
def tag_apply(request, pk, account_id): """Tag transactions using the Monzo API.""" tag = get_object_or_404(Tag, pk=pk) client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) txns = client.get_transactions(account_id)['transactions'] txns = parse_datetimes(txns) txns_updated = 0 for txn in txns: try: if eval(tag.expression): if tag.label not in txn['notes']: updated_txn = client.update_transaction_notes( txn['id'], txn['notes'] + ' ' + tag.label) txns_updated += 1 except (TypeError, KeyError): pass except Exception as e: raise e messages.warning( request, 'There is a problem with your Python expression: ' + str(e)) return redirect('account', account_id) if txns_updated: messages.info( request, '{} transactions tagged. Android users may need to delete App Cache and Data before changes are visible in the app.' .format(txns_updated)) else: messages.warning(request, 'No transactions match the given criteria') return redirect('account', account_id)
def __init__(self): client = Monzo( 'access_token_goes_here' ) # Replace access token with a valid token found at: https://developers.getmondo.co.uk/ account_id = client.get_first_account()[ 'id'] # Get the ID of the first account linked to the access token balance = client.get_balance(account_id) # Get your balance object self.spend_today = balance['spend_today'] self.balance = balance['balance']
def deactivate_webhook(request, account_id): client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) webhook_id = client.get_first_webhook(account_id)['id'] client.delete_webhook(webhook_id) webhook = Webhook.objects.get(id=account_id) webhook.enabled = False webhook.save() return redirect('index')
def __init__(self, secret_key=None): ''' This method sets up variables to be used be other methods ''' if secret_key is None: with open('/var/monzo/key', 'r') as f: secret_key = f.readline() client = Monzo(secret_key) self.client = client self.account_id = client.get_first_account()['id']
def activate_webhook(request, account_id): webhook, created = Webhook.objects.get_or_create(id=account_id) webhook.enabled = True webhook.save() client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) webhook_url = request.build_absolute_uri('webhook/' + account_id) client.register_webhook(webhook_url=webhook_url, account_id=account_id) print(client.get_webhooks(account_id)) return redirect('index')
def index(request): app_name = request.build_absolute_uri().split('.')[0][8:] client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) try: account = client.get_first_account() if account['closed']: account_id = client.get_accounts()['accounts'][1]['id'] else: account_id = account['id'] except (BadRequestError, UnauthorizedError): return render(request, 'invalid-token.html', {'app_name': app_name}) return redirect('account', account_id)
def tag_by_time(request, account_id, time_period): """Tag all transactions according to time period.""" client = Monzo(os.environ.get('MONZO_ACCESS_TOKEN')) txns = client.get_transactions(account_id)['transactions'] txns = parse_datetimes(txns) txns_updated = 0 for txn in txns: tag = '#{}'.format(txn['created'].strftime(strftime_code[time_period]).lower()) if tag not in txn['notes']: updated_txn = client.update_transaction_notes( txn['id'], txn['notes'] + ' ' + tag ) txns_updated += 1 messages.info(request, 'All {} transactions were tagged. Android users may need to delete App Cache and App Data before changes are visible in the app.'.format(txns_updated)) return redirect('account', account_id)
def unauthorized_client(self): return Monzo('gibberish')
from monzo.monzo import Monzo from settings import get_environment_var client = Monzo(get_environment_var('ACCESS_TOKEN')) account_id = client.get_first_account()['id'] transactions = client.get_transactions(account_id)['transactions'] for transaction in transactions: if transaction['attachments']: for attachment in transaction['attachments']: client.deregister_attachment(attachment['id'])
from monzo.monzo import Monzo # Import Monzo class from pprint import pprint import json import hashlib import numpy as np client = Monzo( 'eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJlYiI6InpZS2pBb3lWV0RjNmNkMkd0TkFmIiwianRpIjoiYWNjdG9rXzAwMDA5ZDZtMDZNVnZyZW91bkVnSFIiLCJ0eXAiOiJhdCIsInYiOiI1In0.SllGxvyF4RO4f093tPg3mgIZCc-Z-tsIxKG7dkX8HspMyKDGF5FQIc-fRf9CHzcE3GQOvI9oqos_jE1xKJBfYw' ) # Replace access token with a valid token found at: https://developers.monzo.com/ # account_id = client.get_first_account()['id'] # Get the ID of the first account linked to the access token account_id = "acc_00009VIJcdBdP5vmFVETYn" balance = client.get_balance(account_id) # Get your balance object #print("My balance is ", balance['balance']) # 100000000000 #print(balance['currency']) # GBP #print(balance['spend_today']) # 2000 transactions = client.get_transactions(account_id) # loaded_json = json.loads(transactions) #for x in transactions: #print("{}: {}".format(x, transactions[x])) # pprint(transactions['transactions'][-1]) trans = transactions['transactions'] import numpy as np def weightingFunction(number): return abs(number) #* 250 def main():
from monzo.monzo import Monzo # Add link to source import pandas as pd from matplotlib import pyplot as plt import seaborn as sns #for the nicer default styles from secret_string import * from dictionary import expenditure_codes def clean_df_for_pie(df): df = df[df.amount < 0] #only take outgoings df = df.groupby('category').sum() #reduce to cat type df = df * -1 #change sign return df ## get data off Monzo client = Monzo(secret_string) account_id = client.get_first_account()['id'] transactions = client.get_transactions(account_id)['transactions'] ## convert the interesting subset into a df col = ['amount', 'include_in_spending', 'notes', 'category'] df = pd.DataFrame(data=transactions)[col] dfx = clean_df_for_pie(df) dfx = dfx[dfx.index != 'entertainment'] # dominate catergory plt.pie(dfx.amount, labels=dfx.index)
def client(self): return Monzo(get_environment_var('ACCESS_TOKEN'))
mismatch -= 1 elif instructions[ipointer] == 5: mismatch += 1 elif instructions[ipointer] == 6: if data[dpointer] != 0: mismatch = 1 while mismatch > 0: ipointer -= 1 if instructions[ipointer] == 6: mismatch += 1 elif instructions[ipointer] == 5: mismatch -= 1 elif instructions[ipointer] == 7: data[dpointer] = ord(input("Enter character: ")) elif instructions[ipointer] == 0: print(chr(data[dpointer])) ipointer += 1 client = Monzo(input('Please enter Monzo API key: ')) account_id = input('Please enter Monzo account_id: ') transactions = client.get_transactions(account_id) instructions = [] pname = input("Please enter name of program: ") for transaction in transactions['transactions']: if pname in transaction['notes']: if transaction['amount'] == 10: instructions = [] else: instructions.append(abs(transaction['amount']) % 8) interpret(instructions)
def importer(self): client = Monzo(self.access_token) params = {'since': 'tx_00009DPIXSQUPUgJh5j7yL'} response = client.get_transactions(self.acc_no, params) self.import_transactions(response['transactions'])