Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # %%
- #Imports
- import actual as a
- from actual.queries import reconcile_transaction, get_account, match_transaction
- import pandas as pd
- import os, glob
- from dataclasses import dataclass
- from datetime import date
- import datetime
- import decimal
- # %%
- #Configuraiton
- srcDir = "/home/imarvintpa/Downloads"
- dstDir = "/home/imarvintpa/Documents/Budget/Dailies"
- #functions.
- def getFiles(srcDir, pattern) -> list[str]:
- searchPattern = os.path.join(srcDir, pattern)
- files = glob.glob(searchPattern)
- return files
- def getData(fileName, skip) -> pd.DataFrame:
- df = pd.read_csv(fileName, skiprows=skip)
- print(df.dtypes)
- print(df)
- return df
- #x = reconcile_transaction()
- @dataclass
- class ImportAccount:
- acctId: str
- filePattern: str
- skip: int
- date: str
- dateFmt: str
- payee: str
- notes: str
- amount: str
- invertAmount: bool = False
- creditcard = ImportAccount("MasterCard",
- filePattern = "Card_*.csv",
- skip = 4,
- date = "Transaction Date",
- dateFmt = "%m/%d/%Y",
- payee = "Description",
- notes = "",
- amount = "Amount",
- )
- checking = ImportAccount("Checking",
- filePattern = "acct_chk_*.csv",
- skip = 0,
- date = "Transaction Date",
- dateFmt = "%m/%d/%Y",
- payee = "Full description",
- notes = "Category name",
- amount = "Amount",
- )
- visa = ImportAccount("Visa",
- filePattern = "acct_visa_*.csv",
- skip = 0,
- date = "Transaction Date",
- dateFmt = "%m/%d/%y",
- payee = "Description",
- notes = "",
- amount = "Amount",
- invertAmount = True,
- )
- importList = [creditcard, visa, checking]
- def doImport(actual: a.Actual, acct: ImportAccount):
- files = getFiles(srcDir, acct.filePattern)
- account = get_account(actual.session, acct.acctId)
- for file in files:
- print(file)
- df = getData(file, acct.skip)
- dataList = df.to_dict("records")
- for e in dataList:
- print("")
- tDate = datetime.datetime.strptime(e[acct.date], acct.dateFmt).date()
- payee = str(e[acct.payee]).title()
- notes = None
- if acct.notes in df.columns:
- notes = e[acct.notes]
- amount: str = str(e[acct.amount])
- sign = 1
- if acct.invertAmount:
- sign = 0 - sign
- if amount.startswith("(") and amount.endswith(")"):
- sign = 0 - sign
- amount = amount.replace("(", "").replace(")", "")
- amount = amount.replace("$", "")
- amount = amount.replace(",", "")
- #print(amount)
- amount = decimal.Decimal(amount)
- if sign < 0:
- amount = -amount
- print(account.name, tDate, payee, amount, notes)
- et = match_transaction(actual.session, tDate, account, payee, amount)
- print(et)
- if et is not None:
- #print(et.date, et.imported_description)
- if et.imported_description.lower() != payee.lower():
- #print(et.imported_description.lower(), payee.lower())
- #print("Name Not good enough for me.")
- et = None
- elif datetime.datetime.strptime(str(et.date), "%Y%m%d").date() != tDate:
- #print("Date Not good enough for me.")
- et = None
- if et is None:
- t = reconcile_transaction(actual.session, tDate, account, None, notes, amount=amount, cleared=1, imported_payee=payee)
- print(t)
- print(t.changed())
- else:
- print("Found")
- actual.commit()
- os.rename(file, os.path.join(dstDir, os.path.basename(file)))
- # %%
- def doAllImports():
- with a.Actual("https://actual.local/",
- password = "**********",
- file = "My Finances",
- data_dir = "./data",
- ) as actualAcct:
- actualAcct.sync()
- for act in importList:
- doImport(actualAcct, act)
- # %%
- if __name__ == "__main__":
- doAllImports()
Advertisement
Add Comment
Please, Sign In to add comment