ofx-processor/ofx_processor/revolut_processor/main.py

111 lines
2.9 KiB
Python
Raw Normal View History

2020-01-16 19:02:17 +01:00
import csv
import os
2020-02-08 19:07:05 +01:00
from collections import defaultdict
2020-01-16 19:02:17 +01:00
import click
import dateparser
2020-02-12 00:00:05 +01:00
from ofx_processor.utils import ynab
2020-01-16 19:02:17 +01:00
def process_amount(amount):
if amount:
return float(amount.replace(",", "."))
return ""
def process_memo(line):
return " - ".join(
2020-01-17 16:18:47 +01:00
filter(
None,
map(str.strip, [line.get("Category", ""), line.get("Exchange Rate", "")]),
)
2020-01-16 19:02:17 +01:00
)
2020-01-17 16:18:47 +01:00
def process_date(line):
return dateparser.parse(line.get("Completed Date")).strftime("%Y-%m-%d")
def process_inflow(line):
return process_amount(line.get("Paid In (EUR)"))
def process_outflow(line):
return process_amount(line.get("Paid Out (EUR)"))
2020-01-16 19:02:17 +01:00
@click.command()
2020-02-13 18:21:02 +01:00
@click.version_option()
2020-01-16 19:02:17 +01:00
@click.argument("csv_filename")
2020-02-08 19:07:05 +01:00
@click.option(
"--ynab/--no-ynab",
"push_to_ynab",
default=True,
help="Push data directly to YNAB.",
show_default=True,
)
@click.option(
"--file/--no-file",
"output_file",
2020-02-08 19:07:05 +01:00
default=False,
help="Write a processed file.",
show_default=True,
2020-02-08 19:07:05 +01:00
)
def cli(csv_filename, push_to_ynab, output_file):
2020-01-16 19:02:17 +01:00
formatted_data = []
2020-02-08 19:07:05 +01:00
ynab_transactions = []
transaction_ids = defaultdict(int)
2020-01-16 19:02:17 +01:00
with open(csv_filename) as f:
reader = csv.DictReader(f, delimiter=";")
for line in reader:
2020-02-08 19:07:05 +01:00
date = process_date(line)
payee = line["Reference"]
memo = process_memo(line)
outflow = process_outflow(line)
inflow = process_inflow(line)
2020-01-16 19:02:17 +01:00
formatted_data.append(
{
2020-02-08 19:07:05 +01:00
"Date": date,
"Payee": payee,
"Memo": memo,
"Outflow": outflow,
"Inflow": inflow,
}
)
2020-02-13 18:06:34 +01:00
amount = -outflow if outflow else inflow
amount = int(amount * 1000)
2020-02-08 19:07:05 +01:00
import_id = f"YNAB:{amount}:{date}"
transaction_ids[import_id] += 1
occurrence = transaction_ids[import_id]
import_id = f"{import_id}:{occurrence}"
ynab_transactions.append(
{
"date": date,
"amount": amount,
"payee_name": payee,
"memo": memo,
"import_id": import_id,
2020-01-16 19:02:17 +01:00
}
)
2020-02-13 18:06:34 +01:00
click.secho(f"Processed {len(ynab_transactions)} transactions total.", fg="blue")
2020-01-16 19:02:17 +01:00
if output_file and formatted_data:
2020-02-08 19:07:05 +01:00
processed_file = os.path.join(os.path.dirname(csv_filename), "processed.csv")
with open(processed_file, "w") as f:
writer = csv.DictWriter(
f, delimiter=",", quotechar='"', fieldnames=formatted_data[0].keys()
)
writer.writeheader()
writer.writerows(formatted_data)
2020-01-16 19:02:17 +01:00
2020-02-08 19:07:05 +01:00
click.secho("{} written".format(processed_file), fg="green")
2020-01-16 19:02:17 +01:00
2020-02-12 00:00:05 +01:00
if push_to_ynab and ynab_transactions:
2020-02-08 19:07:05 +01:00
ynab.push_transactions(ynab_transactions, "revolut")
2020-01-16 19:02:17 +01:00
if __name__ == "__main__":
cli()