tab2ledger/tab

88 lines
2 KiB
Text
Raw Normal View History

2019-06-21 12:58:16 +02:00
#!/usr/bin/env python3
import itertools
import sys
import os
import requests
ME = os.getenv("TAB_ME")
TOKEN = os.getenv("TAB_TOKEN")
TAP_ACCOUNT = os.getenv("LEDGER_TAP_ACCOUNT", "Expenses:Zeuskelder:Drinks and snacks")
FOOD_ACCOUNT = os.getenv("LEDGER_FOOD_ACCOUNT", "Expenses:Zeuskelder:Food")
def formatted_price(cents):
return f"€ {cents / 100}"
def balance_assertion(balance):
return \
f"= {formatted_price(balance)}" \
if balance is not None else ""
def formatted_transaction(transaction, me, balance_after=None):
issuer = transaction["issuer"]
debtor = transaction["debtor"]
date = transaction["time"][:10]
amount = formatted_price(transaction["amount"])
message = (
transaction["message"].replace("1 ", "").replace(" and ", " en ")
if issuer == "Tap" else
transaction["message"]
).strip()
header = f"{date} {message}"
assertion = balance_assertion(balance_after)
if debtor == me:
# If the issuer wasn't Tap, we assume the money we gave was for food manual change to the
# output required if it wasn't
to_account = TAP_ACCOUNT if issuer == "Tap" else FOOD_ACCOUNT
to_line = f"{to_account} {amount}"
from_line = f"tab {assertion}".rstrip()
else:
to_line = f"tab {amount} {assertion}"
from_line = f"; {debtor}"
return "\n".join([
header,
"\t" + to_line,
"\t" + from_line
])
def formatted_transactions(transactions, final_balance, me):
all_but_last = itertools.islice(transactions, len(transactions) - 1)
return [
*(formatted_transaction(t, me) for t in all_but_last),
formatted_transaction(transactions[-1], me, final_balance)
]
def main(me, token, file=sys.stdout):
def get(path):
return requests.get(
f"https://tab.zeus.gent{path}",
headers={"Accept": "application/json", "Authorization": f"Token token={token}"}
).json()
balance = get(f"/users/{me}")["balance"]
transactions = get(f"/users/{me}/transactions")
print(
"\n\n".join(formatted_transactions(transactions, balance, me)),
file=file
)
if __name__ == "__main__":
main(ME, TOKEN)