-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscratch.py
179 lines (158 loc) · 5.58 KB
/
scratch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import io
import sys
import uuid
import json
from optparse import OptionParser
from ofxparse import OfxParser
__doc__ = \
"""Convert a valid OFX 2.0 data into a valid Open Bank Project
import json payload. Reads either from file, or from stdin.
"""
dummyEmail = ''.join([str(uuid.uuid4()), '@example.com'])
dummyBank = {
'website': 'https://www.example.com',
'logo': 'https://static.openbankproject.com/images/sandbox/bank_x.png',
'id':'psd201-bank-y--uk',
'short_name': 'Bank Z',
'full_name':'The Bank of Y'
}
dummyAccount = {
'owners': ['[email protected]'],
'generate_auditors_view': True,
'number': '13759969699',
'label': 'Robert XUk X - M35 ..699',
'IBAN': 'BA12 1234 5123 4513 7599 6969 977',
'generate_public_view': False,
'generate_accountants_view': True,
'balance': {'currency' : 'GBP',
'amount': '8084.32'},
'type': 'CURRENT PLUS',
'id': '05237266-b334-4704-a087-5b460a2ecf04',
'bank': 'psd201-bank-y--uk'
}
dummyUser = {
'display_name': dummyEmail[0:32],
'password': '5232e7',
'user_name': dummyEmail[0:32],
'email': dummyEmail
}
dummyTransaction = {
'id': 'b52a3465-d484-4b9a-97da-308188af7c6a',
'counterparty': { 'name': 'British Gas' },
'this_account': { 'id': '05237266-b334-4704-a087-5b460a2ecf04',
'bank': 'psd201-bank-y--uk'
},
'details': {
'description': 'Gas/Elec',
'completed': '2015-07-01T00:00:00.000Z',
'value': '-114.55',
'new_balance': '114.55',
'type': '10219',
'posted': '2015-07-01T00:00:00.000Z'
}
}
def convert(text):
fp = io.StringIO(str(rawtext))
# Parse OFX
ofx = OfxParser.parse(fp)
account = ofx.account
institution = account.institution
statement = account.statement
transactions = statement.transactions
# Build bank object
bankObj = {
'website' : 'https://example.com',
'logo' : 'https://static.openbankproject.com/images/sandbox/bank_x.png',
'id' : institution.organization,
'short_name' : institution.organization,
'full_name' : institution.organization
}
# Build account object
accountObj = {
'owners': [dummyEmail[0:32]],
'generate_auditors_view': True,
'number': account.number,
'label': account.account_type,
'IBAN': account.routing_number, #Wrong
'generate_public_view': False,
'generate_accountants_view': True,
'balance': {'currency' : account.curdef,
'amount': float(statement.balance)},
'type': account.account_type,
'id': account.account_id,
'bank': institution.organization
}
# Build transaction object
transactionsObj = [] # list of transactions
for transaction in transactions:
try:
transactionId = transaction.id
except AttributeError:
transactionId = ''
try:
payee = transaction.payee
except AttributeError:
payee = ''
try:
amount = transaction.amount
except AttributeError:
amount = 0
try:
transactionType = transaction.type
except AttributeError:
transactionType = ''
transactionObj = {
'id': transactionId,
'counterparty': { 'name': payee },
'this_account': { 'id': account.account_id,
'bank': institution.organization
},
'details': {
'description': payee,
'completed': transaction.date.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
'value': float(amount),
'new_balance': '114.55', #Wrong
'type': transactionType,
'posted': transaction.date.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
}
}
transactionsObj.append(transactionObj)
payload = {'banks': [bankObj], 'accounts': [accountObj],
'users': [dummyUser], 'transactions': [transactionObj]}
print(json.dumps(payload))
parser = OptionParser(description=__doc__)
parser.add_option("-f", "--file", dest="filename", default=None,
help="source file to convert (writes to STDOUT)")
(options, args) = parser.parse_args()
#
# Load up the raw text to be converted.
#
if options.filename:
if os.path.isfile(options.filename):
try:
srcfile = open(options.filename, 'rU')
rawtext = srcfile.read()
srcfile.close()
except Exception as detail:
print("Exception during file read:\n%s" % detail)
print("Exiting.")
sys.stderr.write("ofx2obp failed with error code 1\n")
sys.exit(1)
else:
stdin_universal = os.fdopen(os.dup(sys.stdin.fileno()), "rU")
rawtext = stdin_universal.read()
print("No input. Pipe a file to convert to the script,\n" + \
"or call with -f. ")
sys.stderr.write("ofx2obp failed with error code 3\n")
sys.exit(3)
#
# Convert the raw text to Open Bank Project (OBP) json import format.
#
try:
converted = convert(rawtext)
except Exception as detail:
print("Exception during file read:\n%s" % detail)
print ("Exiting.")
sys.stderr.write("ofx2obp failed with error code 1\n")
sys.exit(1)