diff --git a/csv2ofx/mappings/amazon.py b/csv2ofx/mappings/amazon.py new file mode 100644 index 0000000..49e5199 --- /dev/null +++ b/csv2ofx/mappings/amazon.py @@ -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, +} diff --git a/data/converted/amazon.ofx b/data/converted/amazon.ofx new file mode 100644 index 0000000..29547ea --- /dev/null +++ b/data/converted/amazon.ofx @@ -0,0 +1,51 @@ +DATA:OFXSGML +ENCODING:UTF-8 + + + + + 0 + INFO + + 20161031112908 + ENG + + + + + + + 0 + INFO + + + USD + + 83f2779c120eb1531fe646f32245de40 + 100000001 + CHECKING + + + 19700101 + 20230604 + + DEBIT + 20221220000000 + -4.22 + 112-1635210-7125801 + Amazon + Goof Off Household Heavy Duty Remover, 4 fl. oz. Spray, For Spots, Stains, Marks, and Messes; + + + DEBIT + 20221220000000 + -7.42 + 111-3273904-8117030 + Amazon + Darksteve - Violet Decorative Light Bulb - Edison Light Bulb, Antique Vintage Style Light, G80 Size, E26 Base, Non-Dimmable (3w/110v); + + + + + + diff --git a/data/test/amazon.csv b/data/test/amazon.csv new file mode 100644 index 0000000..d7e2e55 --- /dev/null +++ b/data/test/amazon.csv @@ -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 diff --git a/tests/test.py b/tests/test.py index cef45c2..15b5c92 100755 --- a/tests/test.py +++ b/tests/test.py @@ -10,6 +10,7 @@ """ import sys +import os from difflib import unified_diff from os import path as p @@ -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))