archive
This commit is contained in:
BIN
ff3-to-ledger/ff3.csv.tar
Normal file
BIN
ff3-to-ledger/ff3.csv.tar
Normal file
Binary file not shown.
111
ff3-to-ledger/to_ledger.py
Executable file
111
ff3-to-ledger/to_ledger.py
Executable file
@@ -0,0 +1,111 @@
|
||||
#! /bin/python3
|
||||
|
||||
class FireflyIIITransaction(dict) :
|
||||
def __init__(self, keys, line) :
|
||||
for k, v in { keys[i]:line[i] for i in range(len(keys)) }.items() :
|
||||
if not k in dir(self) :
|
||||
setattr(self, k, v)
|
||||
self.keys = keys[:]
|
||||
dict.__init__(self, self.__dict__())
|
||||
|
||||
def __dict__(self) :
|
||||
keys = [
|
||||
"user_id",
|
||||
"group_id",
|
||||
"journal_id",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"group_title",
|
||||
"type",
|
||||
"amount",
|
||||
"foreign_amount",
|
||||
"currency_code",
|
||||
"foreign_currency_code",
|
||||
"description",
|
||||
"date",
|
||||
"source_name",
|
||||
"source_iban",
|
||||
"source_type",
|
||||
"destination_name",
|
||||
"destination_iban",
|
||||
"destination_type",
|
||||
"reconciled",
|
||||
"category",
|
||||
"budget",
|
||||
"bill",
|
||||
"tags",
|
||||
]
|
||||
keys = [
|
||||
"type",
|
||||
"amount",
|
||||
"date",
|
||||
"source_name",
|
||||
"source_type",
|
||||
"destination_name",
|
||||
"destination_type",
|
||||
"category",
|
||||
"description",
|
||||
]
|
||||
return { k:getattr(self, k) for k in keys if k in self.keys }
|
||||
|
||||
def __str__(self) :
|
||||
return self.ledger()
|
||||
|
||||
def ledger(self) :
|
||||
date = self.date.split("T")[0]
|
||||
description = getattr(self, "description" if self.description and self.description != "(null)" else "source_name" if self.type in ["Withdrawl", "Transfer"] else "destination_name")
|
||||
amount = float("{:0.2f}".format(float(self.amount)).strip("-"))
|
||||
local = ""
|
||||
remote = ""
|
||||
if self.type.startswith("Withdraw") :
|
||||
amount = -1.0 * amount
|
||||
local = ":".join([self.source_type, self.source_name]).title().replace(" ", "")
|
||||
remote = ":".join([self.type, self.category, self.destination_name]).title().replace(" ", "")
|
||||
elif self.type == "Transfer" :
|
||||
local = ":".join([self.destination_type, self.destination_name]).title().replace(" ", "")
|
||||
remote = ":".join([self.source_type, self.source_name]).title().replace(" ", "")
|
||||
elif self.type == "Deposit" :
|
||||
local = ":".join([self.destination_type, self.destination_name]).title().replace(" ", "")
|
||||
remote = ":".join([self.type, self.category, self.source_name]).title().replace(" ", "")
|
||||
else :
|
||||
raise Exception("unknown type: "+self.type)
|
||||
return "\n".join([i for i in [ j for j in f'''{date} {description}
|
||||
{"{:100s}".format(remote)} ${"{:0.2f}".format(-1.0 * amount)}
|
||||
{"{:100s}".format(local)} ${"{:0.2f}".format(amount)}
|
||||
'''.split("\n") if j.strip() ]])
|
||||
|
||||
|
||||
class FireflyIII() :
|
||||
def __init__(self, path) :
|
||||
import csv
|
||||
lines = [ i for i in csv.reader(open(path)) ]
|
||||
keys = lines[0]
|
||||
lines = lines[1:]
|
||||
self.transactions = [ FireflyIIITransaction(keys, i) for i in lines ]
|
||||
|
||||
def __str__(self) :
|
||||
import json
|
||||
return json.dumps(self.transactions, indent=" ")
|
||||
|
||||
def __getitem__(self, k) :
|
||||
return self.transactions[k]
|
||||
|
||||
def ledger(self) :
|
||||
return sorted([i.ledger() for i in self.transactions])
|
||||
|
||||
def main(args) :
|
||||
if not args :
|
||||
args.append("./ff3.csv")
|
||||
ff3 = FireflyIII(args[0])
|
||||
print(ff3)
|
||||
print(ff3[0])
|
||||
print("\n".join(ff3.ledger()))
|
||||
f = open("./ledger.dat", "w")
|
||||
for ledger in ff3.ledger() :
|
||||
f.write(ledger)
|
||||
f.write("\n")
|
||||
f.close()
|
||||
|
||||
if __name__ == "__main__" :
|
||||
from sys import argv
|
||||
main(argv[1:])
|
||||
Reference in New Issue
Block a user