Skip to content

Commit

Permalink
Merge pull request #101 from jaraco/feature/amazon-mapping
Browse files Browse the repository at this point in the history
Adding mapping for Amazon purchases.
  • Loading branch information
jaraco authored Feb 19, 2024
2 parents e7509e9 + c9f200b commit 634aea9
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
61 changes: 61 additions & 0 deletions csv2ofx/mappings/amazon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
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 some environment variables:
- ``AMAZON_INCLUDE_CARDS``: comma-separated last-four digits
of cards or payment methods to include in the output. If not
supplied, defaults to all cards.
- ``AMAZON_EXCLUDE_CARDS``: comma-separated last-four digits
of cards or payment methods to exclude in the output (supersedes
include).
- ``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


All = ['']
"""
A special value matching all cards.
"""


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


@functools.lru_cache()
def include_cards():
setting = os.environ.get('AMAZON_INCLUDE_CARDS', None)
return setting.split(',') if setting else All


def filter_payment(row):
include = any(card in row['payments'] for card in include_cards())
exclude = any(card in row['payments'] for card in exclude_cards())
return include and not exclude


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': filter_payment,
}
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 5566: 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 @@ -202,6 +203,13 @@ def gen_test(raw):
"schwab-checking-baltest-case7.csv",
"schwab-checking-baltest-case7.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 634aea9

Please sign in to comment.