From a629d5b29db1d87568ba86e349764283e5f87691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= Date: Tue, 1 Oct 2024 15:00:33 +0100 Subject: [PATCH] chore: more code simplification Fixed issue with the download and parsing of invoice date. --- CHANGELOG.md | 3 ++- src/weby_pilot/__main__.py | 5 ----- src/weby_pilot/base.py | 29 ++++++++++++++++++++++++++++- src/weby_pilot/bpi.py | 34 +++++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77f3e7c..9eadf86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,11 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -* +* Simplified code in the select elements ### Fixed * Issue with investment extracts +* Problem with the download and parsing of invoice date ## [0.1.4] - 2024-10-01 diff --git a/src/weby_pilot/__main__.py b/src/weby_pilot/__main__.py index d819f49..ee60f15 100644 --- a/src/weby_pilot/__main__.py +++ b/src/weby_pilot/__main__.py @@ -4,8 +4,3 @@ from .bpi import BpiAPI print(BpiAPI().download_account_report(report_indexes=range(0, 2))) -# print(BpiAPI().download_account_report(report_indexes=range(0, 2), account_index=1)) -# BpiAPI().download_report(section="Extrato Investimento", report_indexes=range(0, 8)) -# BpiAPI().download_invoice() -# print(BpiAPI().get_balance()) -# BpiAPI().download_card_report(report_indexes=range(0, 8)) diff --git a/src/weby_pilot/base.py b/src/weby_pilot/base.py index 3990c25..474cb90 100644 --- a/src/weby_pilot/base.py +++ b/src/weby_pilot/base.py @@ -89,9 +89,36 @@ def get_elements(self, by: str, value: str) -> List[WebElement]: expected_conditions.presence_of_all_elements_located((by, value)) ) - def select_item(self, element: WebElement, text: str) -> Select: + def select_item( + self, + element: WebElement, + text: str, + force: bool = False, + timeout: float | None = None, + ) -> Select: select = Select(element) + selected_text = select.first_selected_option.text + if not force and selected_text == text: + return select select.select_by_visible_text(text) + if timeout is not None: + sleep(timeout) + return select + + def select_item_index( + self, + element: WebElement, + index: int, + force: bool = False, + timeout: float | None = None, + ) -> Select: + select = Select(element) + selected_index = select.options.index(select.first_selected_option) + if not force and selected_index == index: + return select + select.select_by_index(index) + if timeout is not None: + sleep(timeout) return select def wait_download( diff --git a/src/weby_pilot/bpi.py b/src/weby_pilot/bpi.py index 3f5f487..fcd3909 100644 --- a/src/weby_pilot/bpi.py +++ b/src/weby_pilot/bpi.py @@ -105,13 +105,13 @@ def download_invoice( self.click_extract(row_index=invoice_index) docs.append( BpiDocument( - BpiDocumentType.from_section(document_type), + BpiDocumentType.INVOICE, basename(self._last_download_path), self._last_download_buffer(), file_type=FileType.PDF, account=self.username, date=datetime.strptime( - self._last_download_path[-14:-4], "%Y-%m-%d" + basename(self._last_download_path)[:10], "%Y-%m-%d" ), ) ) @@ -140,7 +140,7 @@ def download_report( file_type=FileType.PDF, account=self.username, date=datetime.strptime( - self._last_download_path[-14:-4], "%Y-%m-%d" + basename(self._last_download_path)[-14:-4], "%Y-%m-%d" ), ) ) @@ -181,7 +181,7 @@ def download_card_report( file_type=FileType.PDF, account=self.username, date=datetime.strptime( - self._last_download_path[-14:-4], "%Y-%m-%d" + basename(self._last_download_path)[-14:-4], "%Y-%m-%d" ), ) ) @@ -226,13 +226,7 @@ def select_account(self, index: int = 0, timeout=5.0): account_element = self.get_element( By.XPATH, "//div[text()='Conta']/following-sibling::div/*/select" ) - account_select = Select(account_element) - selected_index = account_select.options.index( - account_select.first_selected_option - ) - if selected_index != index: - account_select.select_by_index(index) - sleep(timeout) + self.select_item_index(account_element, index, timeout=timeout) def click_extract(self, row_index=0, wait_download: bool = True): open_extract = self.get_element( @@ -307,6 +301,24 @@ def year(self) -> int: raise Exception("Date is not set") return self.date.year + @property + def month(self) -> int: + if self.date is None: + raise Exception("Date is not set") + return self.date.month + + @property + def day(self) -> int: + if self.date is None: + raise Exception("Date is not set") + return self.date.day + + @property + def month_filename(self) -> str: + if self.date is None: + raise Exception("Date is not set") + return f"{self.date.strftime('%m')}.{self.file_type.extension}" + @property def month_filename(self) -> str: if self.date is None: