From 3cf63616310672abf84737869095eef96fd7fefe Mon Sep 17 00:00:00 2001 From: Ward Van Heddeghem Date: Sun, 10 Dec 2023 11:57:45 +0100 Subject: [PATCH] Add account id to loan --- src/mijnbib/mijnbibliotheek.py | 18 +++++++++++------- tests/test_mijnbibliotheek.py | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/mijnbib/mijnbibliotheek.py b/src/mijnbib/mijnbibliotheek.py index 89d9400..239a9e0 100644 --- a/src/mijnbib/mijnbibliotheek.py +++ b/src/mijnbib/mijnbibliotheek.py @@ -87,7 +87,7 @@ def get_loans(self, account_id: str) -> list[Loan]: url = self.BASE_URL + f"/mijn-bibliotheek/lidmaatschappen/{account_id}/uitleningen" html_string = self._open_account_loans_page(url) try: - loans = self._parse_account_loans_page(html_string, self.BASE_URL) + loans = self._parse_account_loans_page(html_string, self.BASE_URL, account_id) except Exception as e: raise IncompatibleSourceError(f"Problem scraping loans ({str(e)})", "") from e return loans @@ -448,7 +448,7 @@ def _parse_item_count_from_li(acc_div, class_: str) -> int | None: return item_count @classmethod - def _parse_account_loans_page(cls, html: str, base_url: str) -> list[Loan]: + def _parse_account_loans_page(cls, html: str, base_url: str, acc_id: str) -> list[Loan]: """Return loans >>> html_string=''' @@ -498,13 +498,14 @@ def _parse_account_loans_page(cls, html: str, base_url: str) -> list[Loan]: ... ... ... ''' - >>> MijnBibliotheek._parse_account_loans_page(html_string,"https://city.bibliotheek.be") # doctest: +NORMALIZE_WHITESPACE + >>> MijnBibliotheek._parse_account_loans_page(html_string,"https://city.bibliotheek.be","123456") # doctest: +NORMALIZE_WHITESPACE [Loan(title='Erebus', loan_from=datetime.date(2023, 11, 25), loan_till=datetime.date(2023, 12, 23), author='Palin, Michael', type='Boek', extendable=True, extend_url='https://city.bibliotheek.be/mijn-bibliotheek/lidmaatschappen/374052/uitleningen/verlengen?loan-ids=6207416', extend_id='6207416', branchname='Gent Hoofdbibiliotheek', id='1324927', url='https://city.bibliotheek.be/resolver.ashx?extid=%7Cwise-oostvlaanderen%7C1324927', - cover_url='https://webservices.bibliotheek.be/index.php?func=cover&ISBN=9789000359325&VLACCnr=10157217&CDR=&EAN=&ISMN=&EBS=&coversize=medium')] + cover_url='https://webservices.bibliotheek.be/index.php?func=cover&ISBN=9789000359325&VLACCnr=10157217&CDR=&EAN=&ISMN=&EBS=&coversize=medium', + account_id='123456')] """ loans = [] soup = BeautifulSoup(html, "html.parser") @@ -535,7 +536,7 @@ def _parse_account_loans_page(cls, html: str, base_url: str) -> list[Loan]: elif child.name == "div": # loan div # we convert child soup object to string, so called function # can be used also easily for unit tests - loan = cls._get_loan_info_from_div(str(child), branch_name, base_url) + loan = cls._get_loan_info_from_div(str(child), branch_name, base_url, acc_id) loans.append(loan) else: # should not happen, fail gracefully for now. @@ -544,7 +545,9 @@ def _parse_account_loans_page(cls, html: str, base_url: str) -> list[Loan]: return loans @classmethod - def _get_loan_info_from_div(cls, loan_div_html: str, branch: str, base_url: str) -> Loan: + def _get_loan_info_from_div( + cls, loan_div_html: str, branch: str, base_url: str, acc_id: str + ) -> Loan: """Return loan from html loan_div blob""" loan_div = BeautifulSoup(loan_div_html, "html.parser") loan = {} @@ -626,6 +629,7 @@ def _get_loan_info_from_div(cls, loan_div_html: str, branch: str, base_url: str) id=loan.get("id", ""), url=loan.get("url", ""), cover_url=loan.get("cover_url", ""), + account_id=acc_id, ) @classmethod @@ -876,7 +880,7 @@ class Loan: id: str = "" url: str = "" cover_url: str = "" - # TODO: add account_id (Also needed for extending multiple loans) + account_id: str = "" @dataclass diff --git a/tests/test_mijnbibliotheek.py b/tests/test_mijnbibliotheek.py index 5c730b8..2b19d01 100644 --- a/tests/test_mijnbibliotheek.py +++ b/tests/test_mijnbibliotheek.py @@ -38,7 +38,7 @@ def test_parse_item_count_from_li(): def test_parse_account_loans_page(): # Happy flow test --> see doctest - assert MijnBibliotheek._parse_account_loans_page("", "") == [] + assert MijnBibliotheek._parse_account_loans_page("", "", "") == [] def test_parse_account_reservations_page():