83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
import datetime
|
|
import pprint
|
|
import logging
|
|
import ofxtools
|
|
import io
|
|
import requests
|
|
import sys
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
def parse(content):
|
|
tree = ofxtools.Parser.OFXTree()
|
|
tree.parse(io.StringIO(content))
|
|
return tree
|
|
|
|
def get_transactions(tree):
|
|
return [parse_transaction(transaction) for transaction in tree.findall('.//STMTTRN')]
|
|
|
|
def _parse_datetime(timestamp):
|
|
return datetime.datetime.strptime(timestamp, '%Y%m%d%H%M%S.000')
|
|
|
|
def parse_transaction(transaction):
|
|
return {
|
|
'available' : _parse_datetime(transaction.find('./DTAVAIL').text),
|
|
'amount' : float(transaction.find('./TRNAMT').text),
|
|
'id' : transaction.find('./FITID').text,
|
|
'memo' : transaction.find('./MEMO').text,
|
|
'name' : transaction.find('./NAME').text,
|
|
'posted' : _parse_datetime(transaction.find('./DTPOSTED').text),
|
|
'type' : transaction.find('./TRNTYPE').text,
|
|
}
|
|
|
|
def main():
|
|
logging.basicConfig()
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
with open(sys.argv[1], 'r') as f:
|
|
content = f.read()
|
|
tree = parse(content)
|
|
transactions = get_transactions(tree)
|
|
for transaction in transactions[:10]:
|
|
if transaction['amount'] > 0:
|
|
continue
|
|
pprint.pprint(transaction)
|
|
payload = {
|
|
'amount' : transaction['amount'],
|
|
'currency' : {
|
|
'code' : 'USD',
|
|
'rate' : 1.0,
|
|
'fixed' : False,
|
|
},
|
|
'date' : transaction['posted'].date().isoformat(),
|
|
'desc' : transaction['name'],
|
|
'account' : '2553488',
|
|
'category' : '50448856',
|
|
'tags' : [],
|
|
'extra' : {
|
|
'available' : transaction['available'].date().isoformat(),
|
|
'fid' : transaction['id'],
|
|
'memo' : transaction['memo'],
|
|
'type' : transaction['type'],
|
|
},
|
|
}
|
|
continue
|
|
response = requests.post('https://api.toshl.com/entries', json=payload, auth=('4cc0e2c6-7b91-4198-9759-df7f873c4d0d4f44474d-e0e8-4e40-b755-7dfb71955cb2', ''))
|
|
if not response.ok:
|
|
print(response.status_code)
|
|
print(response.text)
|
|
return
|
|
else:
|
|
print("uploaded")
|
|
pprint.pprint(transactions[:10])
|
|
return
|
|
root = tree.getroot()
|
|
pprint(root)
|
|
|
|
def print_tree(root, indent=0):
|
|
print("{}{}: {}".format('\t'*indent, root.tag, root.text if root.text else ''))
|
|
for child in root.getchildren():
|
|
pprint(child, indent=indent+1)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|