Skip to content

Commit

Permalink
Adding mapping for Amazon purchases.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jun 4, 2023
1 parent 445b76e commit ec25f2a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
43 changes: 43 additions & 0 deletions csv2ofx/mappings/amazon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Import transactions from Amazon Order History
as exported by Amazon Order History Reporter
(https://chrome.google.com/webstore/detail/amazon-order-history-repo/mgkilgclilajckgnedgjgnfdokkgnibi).
Honors a couple of environment variables:
- ``AMAZON_EXCLUDE_CARDS``: comma-separated last-four digits
of cards or payment methods to exclude in the output.
- ``AMAZON_PURCHASES_ACCOUNT``: The OFX "account id" to use.
Financial tools will use this account ID to associated with an
account. If unspecified, defaults to "100000001".
"""

import os
import functools
from operator import itemgetter


@functools.lru_cache()
def cards():
setting = os.environ.get('AMAZON_EXCLUDE_CARDS', None)
return setting.split(',') if setting else []


def exclude_cards(row):
return not any(card in row['payments'] for card in cards())


mapping = {
'has_header': True,
'delimiter': ',',
'bank': 'Amazon Purchases',
'account_id': os.environ.get('AMAZON_PURCHASES_ACCOUNT', '100000001'),
'date': itemgetter('date'),
'amount': itemgetter('total'),
'payee': 'Amazon',
'desc': itemgetter('items'),
'id': itemgetter('order id'),
'type': 'DEBIT',
'last_row': -1,
'filter': exclude_cards,
}
51 changes: 51 additions & 0 deletions data/converted/amazon.ofx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
DATA:OFXSGML
ENCODING:UTF-8
<OFX>
<SIGNONMSGSRSV1>
<SONRS>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<DTSERVER>20161031112908</DTSERVER>
<LANGUAGE>ENG</LANGUAGE>
</SONRS>
</SIGNONMSGSRSV1>
<BANKMSGSRSV1>
<STMTTRNRS>
<TRNUID></TRNUID>
<STATUS>
<CODE>0</CODE>
<SEVERITY>INFO</SEVERITY>
</STATUS>
<STMTRS>
<CURDEF>USD</CURDEF>
<BANKACCTFROM>
<BANKID>83f2779c120eb1531fe646f32245de40</BANKID>
<ACCTID>100000001</ACCTID>
<ACCTTYPE>CHECKING</ACCTTYPE>
</BANKACCTFROM>
<BANKTRANLIST>
<DTSTART>19700101</DTSTART>
<DTEND>20230604</DTEND>
<STMTTRN>
<TRNTYPE>DEBIT</TRNTYPE>
<DTPOSTED>20221220000000</DTPOSTED>
<TRNAMT>-4.22</TRNAMT>
<FITID>112-1635210-7125801</FITID>
<NAME>Amazon</NAME>
<MEMO>Goof Off Household Heavy Duty Remover, 4 fl. oz. Spray, For Spots, Stains, Marks, and Messes; </MEMO>
</STMTTRN>
<STMTTRN>
<TRNTYPE>DEBIT</TRNTYPE>
<DTPOSTED>20221220000000</DTPOSTED>
<TRNAMT>-7.42</TRNAMT>
<FITID>111-3273904-8117030</FITID>
<NAME>Amazon</NAME>
<MEMO>Darksteve - Violet Decorative Light Bulb - Edison Light Bulb, Antique Vintage Style Light, G80 Size, E26 Base, Non-Dimmable (3w/110v); </MEMO>
</STMTTRN>
</BANKTRANLIST>
</STMTRS>
</STMTTRNRS>
</BANKMSGSRSV1>
</OFX>
5 changes: 5 additions & 0 deletions data/test/amazon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
order id,items,to,date,total,shipping,shipping_refund,gift,tax,refund,payments
112-1635210-7125801,"Goof Off Household Heavy Duty Remover, 4 fl. oz. Spray, For Spots, Stains, Marks, and Messes; ",John Doe,2022-12-20,4.22,0,0,0,0.24,0,"Visa ending in 1234: December 24, 2022: $4.22; "
111-3273904-8117030,"Darksteve - Violet Decorative Light Bulb - Edison Light Bulb, Antique Vintage Style Light, G80 Size, E26 Base, Non-Dimmable (3w/110v); ",John Doe,2022-12-20,7.42,0,0,0,0.42,0,"Visa ending in 1234: December 28, 2022: $7.42; "
114-5269613-6941034,"TOPGREENER Smart Wi-Fi In-Wall Tamper Resistant Dual USB Charger Outlet, Energy Monitoring, Compatible with Amazon Alexa and Google Assistant, Outlet; ",John Doe,2022-10-11,34.12,0,0,0,1.93,0,"Visa ending in 9876: October 12, 2022: $34.12; "
order id,items,to,date,total,shipping,shipping_refund,gift,tax,refund,payments
8 changes: 8 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

import sys
import os

from difflib import unified_diff
from os import path as p
Expand Down Expand Up @@ -157,6 +158,13 @@ def gen_test(raw):
"ingesp.csv",
"ingesp.ofx",
),
(["-o", "-m amazon", "-e 20230604", SERVER_DATE], "amazon.csv", "amazon.ofx",),
]

# for Amazon import; excludes transaction 3/3
os.environ['AMAZON_EXCLUDE_CARDS'] = '9876'
# clear the purchases account if set
os.environ.pop('AMAZON_PURCHASES_ACCOUNT', None)
assert 'AMAZON_PURCHASES_ACCOUNT' not in os.environ

main(csv2ofx, gen_test(PRE_TESTS))

0 comments on commit ec25f2a

Please sign in to comment.