-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.py
145 lines (113 loc) · 4.82 KB
/
client.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
import logging
from typing import List, Dict, Union
import requests
from .const import DEFAULT_USERAGENT, LOGIN_URL, ACCOUNTS_URL, DEFAULT_TIMEOUT
from .exceptions import LoginFailedException, BadResponseException
from .models import Account
from .parser import parse_form, parse_api_transactions
_LOGGER = logging.getLogger(__name__)
class Client:
def __init__(
self,
timeout=DEFAULT_TIMEOUT,
useragent=DEFAULT_USERAGENT,
):
self._session = requests.Session()
self._session.headers.update({"User-Agent": useragent})
self._timeout = timeout
self._accounts: List[Account] = []
def login(self, client_number, password):
"""Login to netbank using client number and password."""
try:
# get login page.
response = self._session.get(LOGIN_URL, timeout=self._timeout)
data = parse_form(response.text)["data"]
data.update(
{
"txtMyClientNumber$field": str(client_number),
"txtMyPassword$field": password,
"btnLogon$field": "Log on",
"chkRemember$field": "on",
"JS": "E",
}
)
response = self._session.post(LOGIN_URL, data=data, timeout=self._timeout)
form = parse_form(response.text)
if form["action"].startswith("/netbank/Logon/Logon.aspx"):
raise LoginFailedException("Credentials were likely invalid")
# response =
self._session.post(
form["action"],
data=form["data"],
timeout=self._timeout,
)
# nbid cookie only exists when logged in.
if self._session.cookies.get("nbid"):
_LOGGER.info("Login Successful!")
# self.load_accounts(response.text)
return
except Exception as ex:
_LOGGER.exception("Login Failed!")
raise LoginFailedException(ex)
raise LoginFailedException("Credentials were likely invalid")
def accounts(self) -> List[Account]:
"""Returns a list of the users accounts."""
response = self._session.get(ACCOUNTS_URL, timeout=self._timeout)
try:
accounts: List = response.json()["accounts"]
self._accounts = [Account(self, account) for account in accounts]
except ValueError as ex:
_LOGGER.exception("Failed to parse accounts")
raise BadResponseException(ex)
return self._accounts
def account(self, account_number: Union[str, int]) -> Account:
"""Returns a single account by its account number."""
account_number = str(account_number)
# fetch accounts
if not self._accounts:
self.accounts()
for account in self._accounts:
if account.number == account_number:
return account
# def fetch_accounts(self, html):
# """Fetch a list of accounts."""
#
# try:
# self.accounts = [
# self.Account(account, account)
# for account in parser.parse_accounts_table(html)
# ]
# _LOGGER.debug("ACCOUNTS: %s", self.accounts)
# return self.accounts
# except:
# _LOGGER.exception("Updating accounts, failed to parse accounts.")
# return None
def transactions(self, account: Account) -> List[Dict]:
"""Retrieve recent transactions for an account."""
url = "https://www.commbank.com.au" + account.link.replace(
"/retail/netbank/accounts/", "/retail/netbank/accounts/api/transactions"
)
response = self._session.get(url, timeout=self._timeout)
# if "retail/digitalidentityprovider/connect/authorize" in response.url:
# # we need to submit a form to continue
# form = parser.parse_form(response.text)
# print(f"ACTION: {form['action']}")
# response = self._session.post(
# "https://www.my.commbank.com.au" + form["action"],
# data=form["data"],
# timeout=self._timeout,
# )
try:
return parse_api_transactions(response.json())
except ValueError as ex:
_LOGGER.exception("Failed to parse transactions")
raise BadResponseException(ex)
# def to_dict(self) -> List[dict]:
# """Retrieve a dictionary of the users accounts."""
# return [dict(account) for account in self._accounts]
# def __str__(self):
# """Returns a string representation of the users accounts."""
# return json.dumps([dict(account) for account in self._accounts], indent=4)
# def __iter__(self):
# for account in self._accounts:
# yield account