Skip to content

Instantly share code, notes, and snippets.

@Michcioperz
Created May 31, 2017 11:05
Show Gist options
  • Save Michcioperz/295f553b4fced88d78299d781405c11a to your computer and use it in GitHub Desktop.
Save Michcioperz/295f553b4fced88d78299d781405c11a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import yaml, requests, os, argparse
with open(os.path.expanduser("~/budget.yaml")) as f:
data = yaml.load(f)
currency = data.get('settings', dict()).get('currency', None)
parser = argparse.ArgumentParser()
parser.add_argument("currency", default=currency, nargs=('?' if currency is not None else 1))
args = parser.parse_args()
for x in data.get("patreons", []):
if x.get('amount', 0):
if 'expenses' not in data:
data['expenses'] = []
data["expenses"].append(dict(amount=x["amount"], tax=23, currency="USD", description=x["name"], medium='Patreon'))
currencies = {x["currency"] for x in data["expenses"] if x.get("enabled",True)} | {x["currency"] for x in data["incomes"] if x.get("enabled", True)}
if 'patreons' in data:
currencies.add('USD')
rates = requests.get("https://api.fixer.io/latest?base={base}&symbols={symbols}".format(base=args.currency, symbols=",".join(currencies))).json()["rates"]
print("1 {base}".format(base=args.currency)+"".join([" = {rate} {currency}".format(currency=x, rate=rates[x]) for x in rates]))
print()
for x in data["incomes"]:
if x.get("enabled", True):
x["taxed_amount"] = x["amount"]*(1+(x.get("tax", 0)/100.00))
x["base_amount"] = x["taxed_amount"] if x["currency"] == args.currency else x["taxed_amount"]/rates[x["currency"]]
print("+[{medium}] {description} - {amount:.2f} {currency} - {base_amount:.2f} {base_currency}".format(base_currency=args.currency, **x))
print()
for x in data["expenses"]:
if x.get("enabled", True):
x["taxed_amount"] = x["amount"]*(1+(x.get("tax", 0)/100.00))
x["base_amount"] = x["taxed_amount"] if x["currency"] == args.currency else x["taxed_amount"]/rates[x["currency"]]
print("-[{medium}] {description} - {amount:.2f} {currency} - {base_amount:.2f} {base_currency}".format(base_currency=args.currency, **x))
print()
total_in, total_out = sum([x["base_amount"] for x in data["incomes"] if x.get("enabled", True)]), sum([x["base_amount"] for x in data["expenses"] if x.get("enabled", True)])
print("+ {total_in:.2f} {currency}".format(total_in=total_in, currency=args.currency))
print("- {total_out:.2f} {currency}".format(total_out=total_out, currency=args.currency))
total = total_in - total_out
print("= {total:.2f} {currency}".format(total=total, currency=args.currency))
settings:
currency: PLN
patreons:
- name: Vic Nightfall
amount: 1.00
expenses:
- amount: 4.00
tax: 23
currency: PLN
medium: Aruba
description: Aruba server
incomes:
- amount: 5.00
currency: PLN
medium: dad
description: pocket money
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment