diff --git a/src/mijnbib/mijnbibliotheek.py b/src/mijnbib/mijnbibliotheek.py index 54ad5e8..8d89c2f 100644 --- a/src/mijnbib/mijnbibliotheek.py +++ b/src/mijnbib/mijnbibliotheek.py @@ -76,6 +76,7 @@ def __init__(self, username: str, password: str, city: str | None = None, login_ self._loans_page_parser = LoansListPageParser() self._accounts_page_parser = AccountsListPageParser() self._reservations_parser = ReservationsPageParser() + self._extend_response_page_parser = ExtendResponsePageParser() # *** PUBLIC METHODS *** @@ -278,7 +279,7 @@ def extend_loans(self, extend_url: str, execute: bool = False) -> tuple[bool, di # On submit, we arrive at "uitleningen" (loans) page, which lists the result html_string = response.read().decode("utf-8") # type:ignore # Path("response.html").write_text("html_string") # for debugging - details = ExtendResponsePageParser(html_string).parse() + details = self._extend_response_page_parser.parse(html_string) if "likely_success" in details and details["likely_success"] is False: # Probably valid page (http=200) but with 'Foutmelding' success = False diff --git a/src/mijnbib/parsers.py b/src/mijnbib/parsers.py index c2783b3..b3f7e90 100644 --- a/src/mijnbib/parsers.py +++ b/src/mijnbib/parsers.py @@ -26,18 +26,12 @@ class Parser(ABC): - @abstractmethod - def parse(self): - pass - - -class ParserNew(ABC): @abstractmethod def parse(self, html: str, *args, **kwargs): pass -class LoansListPageParser(ParserNew): +class LoansListPageParser(Parser): def parse(self, html: str, base_url: str, account_id: str) -> list[Loan]: """Return loans. @@ -227,7 +221,7 @@ def _get_loan_info_from_div( ) -class AccountsListPageParser(ParserNew): +class AccountsListPageParser(Parser): def parse(self, html: str, base_url: str) -> list[Account]: """Return list of accounts. @@ -391,7 +385,7 @@ def _parse_item_count_from_li(acc_div, class_: str) -> int | None: return item_count -class ReservationsPageParser(ParserNew): +class ReservationsPageParser(Parser): def parse(self, html: str) -> list[Reservation]: """Return list of holds. @@ -537,15 +531,13 @@ def parse(self, html: str) -> list[Reservation]: class ExtendResponsePageParser(Parser): - def __init__(self, html: str): - self._html = html - - def parse(self) -> dict: + def parse(self, html) -> dict: """For dict structure, see the called method.""" - html_blob = self._extract_html_from_response_script_tag() + html_blob = self._extract_html_from_response_script_tag(html) return self._parse_extend_response_status_blob(html_blob) - def _extract_html_from_response_script_tag(self): + @classmethod + def _extract_html_from_response_script_tag(cls, html: str): """Return html-encoded data from ajax encoded data. The extending loan response contains the result in a ajax script thingy. @@ -558,7 +550,7 @@ def find_between(s: str, start: str, end: str): return s[s.find(start) + len(start) : s.rfind(end)] # find relevant snippet - soup = BeautifulSoup(self._html, "html.parser") + soup = BeautifulSoup(html, "html.parser") script_txt = soup.find( "script", string=re.compile("(Statusbericht|Foutmelding)") ).get_text() diff --git a/tests/test_mijnbibliotheek.py b/tests/test_mijnbibliotheek.py index 7fff699..d1a919a 100644 --- a/tests/test_mijnbibliotheek.py +++ b/tests/test_mijnbibliotheek.py @@ -207,3 +207,6 @@ def parse(self, _html): # Assert assert mb.get_reservations(account_id="whatever") == [res] + + # def test_extendresponse_parser_can_be_overridden(self): + # Not so easy to write test for diff --git a/tests/test_parsers.py b/tests/test_parsers.py index c1ddc4c..b0018ba 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -59,9 +59,9 @@ def test_extract_html_from_response_script_tag(self): def clean_whitespace(s: str) -> str: return s.replace(" ", "").replace("\n", "") - actual_result = ExtendResponsePageParser( + actual_result = ExtendResponsePageParser._extract_html_from_response_script_tag( raw_html - )._extract_html_from_response_script_tag() + ) assert clean_whitespace(actual_result) == clean_whitespace(expected_result) def test_parse_extend_response_status_blob__empty_case(self):