Skip to content

Commit

Permalink
[fix] address code-smells
Browse files Browse the repository at this point in the history
  • Loading branch information
grindsa committed Dec 28, 2024
1 parent 07a2ae3 commit 4906ee0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
8 changes: 4 additions & 4 deletions dkb_robo/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


JSON_CONTENT_TYPE = 'application/vnd.api+json'

BASE_URL = 'https://banking.dkb.de/api'

class Authentication:
""" Authentication class """

account_dic = {}
base_url = 'https://banking.dkb.de/api'
base_url = BASE_URL
chip_tan = False
client = None
dkb_user = None
Expand Down Expand Up @@ -349,7 +349,7 @@ def logout(self):
class APPAuthentication:
""" APPAuthentication class """

def __init__(self, client: requests.Session, logger: logging.Logger, base_url: str = 'https://banking.dkb.de/api'):
def __init__(self, client: requests.Session, logger: logging.Logger, base_url: str = BASE_URL):
self.client = client
self.logger = logger
self.base_url = base_url
Expand Down Expand Up @@ -416,7 +416,7 @@ def finalize(self, challenge_id: str, _challenge_dic: Dict[str, str], devicename
class TANAuthentication:
""" TANAuthentication class """

def __init__(self, client: requests.Session, logger: logging.Logger, base_url: str = 'https://banking.dkb.de/api', mfa_method: str = 'chip_tan_manual'):
def __init__(self, client: requests.Session, logger: logging.Logger, base_url: str = BASE_URL, mfa_method: str = 'chip_tan_manual'):
self.client = client
self.logger = logger
self.base_url = base_url
Expand Down
33 changes: 19 additions & 14 deletions dkb_robo/dkb_robo.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ def scan_postbox(self, path=None, download_all=False, _archive=False, prepend_da
self.logger.debug('DKBRobo.scan_postbox()\n')
return self.download(Path(path) if path is not None else None, download_all, prepend_date)

def download_doc(self, path: Path, doc, prepend_date: bool = False, mark_read: bool = True, use_account_folders: bool = False, list_only: bool = False, accounts_by_id: dict = None):
""" download a single document """
target = path / doc.category()

if use_account_folders:
target = target / doc.account(card_lookup=accounts_by_id)

filename = f"{doc.date()}_{doc.filename()}" if prepend_date else doc.filename()

if not list_only:
self.logger.info("Downloading %s to %s...", doc.subject(), target)
if doc.download(self.wrapper.client, target / filename):
if mark_read:
doc.mark_read(self.wrapper.client, True)
time.sleep(.5)
else:
self.logger.info("File already exists. Skipping %s.", filename)

def download(self, path: Path, download_all: bool, prepend_date: bool = False, mark_read: bool = True, use_account_folders: bool = False, list_only: bool = False):
""" download postbox documents """
if path is None:
Expand All @@ -116,19 +134,6 @@ def download(self, path: Path, download_all: bool, prepend_date: bool = False, m

accounts_by_id = {acc['id']: acc['account'] for acc in self.wrapper.account_dic.values()}
for doc in documents.values():
target = path / doc.category()

if use_account_folders:
target = target / doc.account(card_lookup=accounts_by_id)

filename = f"{doc.date()}_{doc.filename()}" if prepend_date else doc.filename()
self.download_doc(path=path, doc=doc, prepend_date=prepend_date, mark_read=mark_read, use_account_folders=use_account_folders, list_only=list_only, accounts_by_id=accounts_by_id)

if not list_only:
self.logger.info("Downloading %s to %s...", doc.subject(), target)
if doc.download(self.wrapper.client, target / filename):
if mark_read:
doc.mark_read(self.wrapper.client, True)
time.sleep(.5)
else:
self.logger.info("File already exists. Skipping %s.", filename)
return documents
17 changes: 13 additions & 4 deletions dkb_robo/portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def __init__(self, client: requests.Session, logger: logging.Logger, base_url: s
self.logger = logger
self.base_url = base_url

def _add_remaining(self, data_dic: Dict[str, str], account_dic: Dict[str, str], account_cnt: int) -> Dict[str, str]:
""" add remaining products """
self.logger.debug('portfolio.Overview._add_remaining()\n')

for product_data in data_dic.values():
account_dic[account_cnt] = product_data
account_dic[account_cnt]['productgroup'] = None
account_cnt += 1

self.logger.debug('portfolio.Overview._add_remaining() ended\n')
return account_dic

def _fetch(self, url_path) -> Dict[str, str]:
""" fetch data via API """
self.logger.debug('portfolio.Overview._fetch()\n')
Expand Down Expand Up @@ -111,10 +123,7 @@ def _sort(self, portfolio_dic: Dict[str, str]) -> Dict[str, str]:
account_cnt += 1

# add products without productgroup
for product_data in data_dic.values():
account_dic[account_cnt] = product_data
account_dic[account_cnt]['productgroup'] = None
account_cnt += 1
account_dic = self._add_remaining(data_dic, account_dic, account_cnt)

self.logger.debug('portfolio.Overview._sort() ended\n')
return account_dic
Expand Down

0 comments on commit 4906ee0

Please sign in to comment.