Skip to content

Instantly share code, notes, and snippets.

@seberm
Last active January 21, 2022 12:44
Show Gist options
  • Save seberm/3dd75ed718892d5e3db8c68109b63795 to your computer and use it in GitHub Desktop.
Save seberm/3dd75ed718892d5e3db8c68109b63795 to your computer and use it in GitHub Desktop.
Beancount CSV exporter/importer for anycoin.cz
#!/usr/bin/env python3
# Usage: $ bean-extract extractors/anycoin.import ./anycoin.csv > transactions.beancount
import csv
from beancount.ingest.importers.csv import (
Importer,
Col,
)
from beancount.core.data import Posting
from beancount.core.number import D
from beancount.core.amount import Amount
from beancount.core.position import CostSpec
def categorize_second_posting(txn, row):
price = round(D(row[5]) / D(row[4]), ndigits=4)
res_postings = [
Posting("Assets:Wallet", Amount(D(row[4]), "BTC"), CostSpec(number_per=price, number_total=None, currency="CZK", date=None, label=None, merge=None), None, None, None),
]
for p in res_postings:
txn.postings.append(p)
return txn
class CSVImporter(Importer):
...
class excel_semicolon(csv.excel):
delimiter = ","
anycoin_importer = CSVImporter(
config={
Col.REFERENCE_ID: "UID",
Col.DATE: "DATE",
Col.NARRATION1: "UID",
Col.NARRATION2: "SYMBOL",
Col.AMOUNT: "VOLUME",
},
account="Assets:Checking",
currency="CZK",
csv_dialect=excel_semicolon,
categorizer=categorize_second_posting,
invert_sign=True,
)
# Setting this variable provides a list of importer instances.
CONFIG = [
anycoin_importer,
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment