-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
194 lines (162 loc) · 5.52 KB
/
api.py
File metadata and controls
194 lines (162 loc) · 5.52 KB
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
193
194
import json
import requests
import urllib3
from time import time
from getpass import getpass
class BankApi(object):
BASE_URL = 'https://bbva.es'
LOGIN_URL = '/ASO/TechArchitecture/grantingTickets/V02'
REFRESH_TSEC = '/ASO/grantingTicketActions/V01/refreshGrantingTicket/?customerId='
CUSTOMER_DATA_URL = '/ASO/contextualData/V02/'
FINANCIAL_DASHBOARD_URL = '/ASO/financialDashBoard/V03/?$customer.id='
WIRE_TRANSFER_SIMULATION_URL = '/ASO/wireTransfers/V02/simulation/'
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0) Gecko/20100101 Firefox/70.0'
def __init__(self):
self.create_session()
urllib3.disable_warnings()
def create_session(self):
self.session = requests.Session()
self.session.headers.update({
'Content-Type': 'application/json',
'User-Agent': self.USER_AGENT,
'Host': 'www.bbva.es'
})
def save_tsec(self, response):
self.session.headers['tsec'] = response.headers['tsec']
self.last_tsec_time = time()
def save_userid(self, response):
self.userid = response['user']['id']
def login(self, user, password):
login_status = False
payload = {
'authentication':{
'consumerID':'00000001',
'authenticationType':'02',
'userID':'0019-0' + user,
'authenticationData':[
{
'authenticationData':[password],
'idAuthenticationData':'password'
}
]
}
}
response = self.session.post(
self.BASE_URL + self.LOGIN_URL,
data=json.dumps(payload),
verify=False
)
response_data = json.loads(response.text)
# If it did not fail save tsec
if 'http-status' not in response_data:
self.save_tsec(response)
self.save_userid(response_data)
return response_data
def refresh_tsec(self):
response = self.session.post(
self.BASE_URL + self.REFRESH_TSEC + self.userid,
data=json.dumps({}),
verify=False
)
self.save_tsec(response)
return True
def request(self, url, data=False):
# Every 4 minutes we need a new tsec
seconds_past = time() - self.last_tsec_time
if seconds_past > 240:
self.refresh_tsec()
if data:
response = self.session.post(
url,
data=json.dumps(data),
verify=False
)
else:
response = self.session.get(
url,
verify=False
)
return json.loads(response.text)
def get_customer_data(self):
'''
Get your name, phone, email etc
'''
response = self.request(
self.BASE_URL + self.CUSTOMER_DATA_URL + self.userid,
)
return response
def get_financial_dashboard(self):
'''
Get balance on all contracted accounts, cards, securities etc
'''
response = self.request(
self.BASE_URL + self.FINANCIAL_DASHBOARD_URL + self.userid,
)
return response
def post_wire_transfer_simulation(self, data):
'''
Test wire transfer, also gives us a boolean if we can make the transfer
without the 2step verification SMS ('regularTransfer': 'C' --> True)
This function takes data in the following format:
data = {
'beneficiary_iban': 'SOMEIBAN1234234',
'beneficiary_name': 'NAME OF BENEFICIARY',
'amount': 100.00,
'description': 'SOME CONCEPT TEXT'
}
'''
# Get the variables we need
financial_dashboard = self.get_financial_dashboard()
customer_data = self.get_customer_data()
sender_account_id = financial_dashboard['positions'][0]['contract']['account']['id']
# Make the post data
transfer_data = {
'sender': {
'account': {
'id': sender_account_id,
'currency': {
'id': 'EUR'
}
},
'customer': {
'id': self.userid,
'name': customer_data['customer']['name']
}
},
'receiver': {
'account': {
'formats': {
'iban': data['beneficiary_iban']
}
},
'beneficiary': {
'name': data['beneficiary_name']
}
},
'amount': {
'amount': data['amount'],
'currency': 'EUR'
},
'description': data['description'],
'expensesType': {
'id': 'A'
},
'executionType': {
'id': 'E'
}
}
# Call the API
response = self.request(
self.BASE_URL + self.WIRE_TRANSFER_SIMULATION_URL,
transfer_data
)
return response
def main():
DNI = input('Introduce tu DNI:')
password = getpass('Introduce password:')
ba = BankApi()
ba.login(DNI, password)
amount = ba.get_financial_dashboard()['assetBalance']['amount']
print(f'Tienes un total de {round(amount, 2)} EUR en tu cuenta.')
if __name__ == '__main__':
main()