Skip to content

Commit

Permalink
chore: more code simplification
Browse files Browse the repository at this point in the history
Fixed issue with the download and parsing of invoice date.
  • Loading branch information
joamag committed Oct 1, 2024
1 parent 5f10132 commit a629d5b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions src/weby_pilot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
29 changes: 28 additions & 1 deletion src/weby_pilot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 23 additions & 11 deletions src/weby_pilot/bpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
)
)
Expand Down Expand Up @@ -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"
),
)
)
Expand Down Expand Up @@ -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"
),
)
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a629d5b

Please sign in to comment.