-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbank_funtions Original.py
192 lines (164 loc) · 7.54 KB
/
usbank_funtions Original.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
180
181
182
183
184
185
186
187
188
189
190
191
192
import requests
import sys
import os
import math
def main():
testing_account= "913996201744144603"
url_dict = {
'autoloan_url':'https://alpha-api.usbank.com/innovation-rate/v1/GetAutoLoanRates?application=RIB&output=json&branchnumber=1&zipcode=80130®ionid=1&loanamount=24000&loantermmonths=12&loanproduct=NEW',
'atm_url':'https://alpha-api.usbank.com/innovation-locations/v1/StringQuery?application=parasoft&transactionid=afae903d-8946-4f88-a958-4bdbcf0bed6f&output=json&searchtype=A&stringquery=55403&branchfeatures=BOP'
}
# apiKey = os.environ['USBANK_APIKEY'] # TODO set this environment variable in PyCharm or for your OS
header = {'apiKey': os.environ['USBANK_APIKEY']}
base_url = 'https://alpha-api.usbank.com/innovations/v1'
try:
if sys.argv[2]:
if sys.argv[1] == 'transactions' and sys.argv[2].isnumeric():
transactionsList = getTransactions(sys.argv[2],base_url,header)
dates, individualPinches, cumulativePinches, descriptions = getAccountTransactions(transactionsList)
save('account_date_list',dates)
save('individual_pinches_list',individualPinches)
save('cumulative_pinches_list',cumulativePinches)
save('tranaction_descriptions_list',descriptions)
except:
pass
if sys.argv[1]:
if sys.argv[1] == 'names':
users_list = get_user_list(base_url,header)
namesDict= getNamesandIdentifiers(users_list,base_url,header)
save('namesDict',namesDict)
elif sys.argv[1] == 'accounts':
users_list = get_user_list(base_url,header)
account_info_dict = get_accounts(base_url,users_list,header)
save('account_dict',account_info_dict)
# users_list = get_user_list(base_url,header)
# namesDict= getNamesandIdentifiers(users_list,base_url,header)
# # save('namesDict',namesDict)
# users_list = get_user_list(base_url,header)
# account_info_dict = get_accounts(base_url,users_list,header)
# # save('account_dict',account_info_dict)
# print(account_info_dict)
def get_atm_list(url_dict,header):
response = requests.get(url_dict['atm_url'], headers=header).json()
reply = response['GetListATMorBranchReply']
atmList = reply['ATMList'] # Todo extract specific location data needed
return atmList
def get_auto_rate(url_dict,header):
rateResponse = requests.get(url_dict['autoloan_url'], headers=header).json()
rate = rateResponse['AutoLoanRates']['RateTier']['Rate']
return rate
def getTransactions(LPI, baseUrl, header):
allAccountsUrl = baseUrl + '/user/accounts'
params = {'LegalParticipantIdentifier': LPI}
AADL = requests.post(allAccountsUrl, data=params, headers=header).json()['AccessibleAccountDetailList']
for item in AADL:
if item['ProductCode'] == 'CCD' or item['ProductCode'] == 'BCD':
pi = item['PrimaryIdentifier']
pc = item['ProductCode']
oci = item['OperatingCompanyIdentifier']
data = {'PrimaryIdentifier': pi,
'ProductCode': pc,
'OperatingCompanyIdentifier': oci}
transactionsUrl = baseUrl + '/account/transactions'
transactions = requests.post(transactionsUrl, data=data, headers=header).json()
if transactions['Status']['StatusCode'] == '404':
continue
else:
# if len(transactions['TransactionList']) >= 20:
# print(transactions['TransactionList'])
return transactions['TransactionList']
#
# def getAccountTransactions(transactions):
# dates = []
# individualPinches = []
# cumulativePinches = []
# descriptions = []
# for transaction in transactions:
# try:
# # print(transaction)
# initial = float(transaction['PostedAmount'])
# rounded = math.ceil(initial)
# pinch = rounded - initial
# descriptions.append(transaction['Description1'])
# dates.append(transaction['PostedDate'])
# individualPinches.append(pinch)
# cumulativePinches.append(sum(individualPinches))
# print(dates, '\n', individualPinches, '\n', cumulativePinches, '\n', descriptions)
# except KeyError:
# continue
# for index in range(len(dates)):
# print('{0:<15}{1:<50}${2:<6.2f}${3:<10.2f}'.format(dates[index], descriptions[index], individualPinches[index], cumulativePinches[index]))
# print('\nNEW')
# # print(dates,'\n',individualPinches,'\n',cumulativePinches,'\n',descriptions)
# return dates,individualPinches,cumulativePinches,descriptions
#
def getAccountTransactions(transactions):
dates = []
individualPinches = []
cumulativePinches = []
descriptions = []
for transaction in transactions:
try:
initial = float(transaction['PostedAmount'])
rounded = math.ceil(initial)
pinch = rounded - initial
descriptions.append(transaction['Description1'])
dates.append(transaction['PostedDate'])
individualPinches.append(pinch)
cumulativePinches.append(sum(individualPinches))
except KeyError:
continue
# for index in range(len(dates)):
# print('{0:<15}{1:<50}${2:<6.2f}${3:<10.2f}'.format(dates[index], descriptions[index], individualPinches[index], cumulativePinches[index]))
# print('\nNEW')
return dates, individualPinches, cumulativePinches, descriptions
def get_user_list(base_url,header):
users_url = base_url + '/users'
ids = requests.get(users_url, headers=header).json()
users=ids['UserList']
users_list = []
for item in users:
users_list.append(item['LegalParticipantIdentifier'])
return users_list
def getNamesandIdentifiers(LPI, baseUrl, header):
namesAndIdentifiers = {}
allAccountsUrl = baseUrl + '/user/accounts'
params = {'LegalParticipantIdentifier': LPI}
AADL = requests.post(allAccountsUrl, data=params, headers=header).json()['AccessibleAccountDetailList']
pi = AADL[0]['PrimaryIdentifier']
pc = AADL[0]['ProductCode']
oci = AADL[0]['OperatingCompanyIdentifier']
data = {'PrimaryIdentifier': pi,
'ProductCode': pc,
'OperatingCompanyIdentifier': oci}
accountDetailsUrl = baseUrl + '/account/details'
accountDetails = requests.post(accountDetailsUrl, data=data, headers=header).json()
detail = accountDetails['Account']['AccountDetail']
try:
name = detail['AddressAndTitle']['AccountTitle']
except KeyError:
name = 'Unknown Name ' + str(LPIList.index(LPI) + 1)
namesAndIdentifiers[name] = LPI
return namesAndIdentifiers
def save(file_name,data):
file = open(file_name,'w')
file.write(str(data))
file.close()
def get_user_list_arg(base_url,header):
users_url = base_url + 'users'
ids = requests.get(users_url, headers=header).json()
print(ids)
users=ids['UserList']
users_id_list = []
for item in users:
users_id_list.append(item['LegalParticipantIdentifier'])
file = open('user_list.txt',"w")
return users_id_list
def get_accounts(base_url,users_list,header):
account_info_dict = {}
for ident in users_list:
data = {'LegalParticipantIdentifier': f'{ident}'} # find this out from query to /users endpoint
account_info = requests.post(base_url + '/user/accounts', headers=header, data=data).json()
account_info_dict[ident] = account_info
return account_info_dict
main()