-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an importer for Swisscard Cashback cards CSV exports.
This importer tries to parse the CSV that swisscard exports (which can be retrieved from https://app.swisscard.ch). It doesn't support currency conversion, as I haven't found a single conversion transaction in my exports.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import csv | ||
from io import StringIO | ||
|
||
from beancount.core import amount, data | ||
from beancount.core.number import D | ||
from beancount.ingest import importer | ||
from beancount.ingest.importers.mixins import identifier | ||
from dateutil.parser import parse | ||
|
||
|
||
class SwisscardImporter(identifier.IdentifyMixin, importer.ImporterProtocol): | ||
"""An importer for Swisscard's cashback CSV files.""" | ||
|
||
def __init__(self, regexps, account): | ||
identifier.IdentifyMixin.__init__( | ||
self, matchers=[("filename", regexps)]) | ||
self.account = account | ||
|
||
def name(self): | ||
return super().name() + self.account | ||
|
||
def file_account(self, file): | ||
return self.account | ||
|
||
def extract(self, file, existing_entries): | ||
entries = [] | ||
with StringIO(file.contents()) as csvfile: | ||
reader = csv.DictReader( | ||
csvfile, | ||
delimiter=",", | ||
skipinitialspace=True, | ||
) | ||
for row in reader: | ||
book_date = parse( | ||
row["Transaction date"].strip(), dayfirst=True).date() | ||
amt = amount.Amount(-D(row["Amount"]), row["Currency"]) | ||
metakv = { | ||
"category": row["Category"], | ||
} | ||
meta = data.new_metadata(file.name, 0, metakv) | ||
description = row["Description"].strip() | ||
entry = data.Transaction( | ||
meta, | ||
book_date, | ||
"*", | ||
"", | ||
description, | ||
data.EMPTY_SET, | ||
data.EMPTY_SET, | ||
[ | ||
data.Posting(self.account, amt, | ||
None, None, None, None), | ||
], | ||
) | ||
entries.append(entry) | ||
|
||
return entries |