Skip to content

Commit

Permalink
Add extend by ids
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanhed committed Dec 18, 2023
1 parent b9cdfc5 commit 3556fa3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/mijnbib/mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def extend_loans(self, extend_url: str, execute: bool = False) -> tuple[bool, di
IncompatibleSourceError
"""
# TODO: would make more sense to return loan list (since final page is loan page)
# Perhaps retrieving those loans again, and check extendability would also be good idea.
if not self._logged_in:
self.login()

Expand Down Expand Up @@ -223,6 +224,9 @@ def extend_loans(self, extend_url: str, execute: bool = False) -> tuple[bool, di
# disclaimer: not sure if other codes are realistic
success = True if response.code == 200 else False

if success:
_log.debug("Looks like extending the loan(s) was successful")

# Try to add result details, but don't fail if we fail to parse details, it's tricky :-)
try:
# On submit, we arrive at "uitleningen" (loans) page, which lists the result
Expand All @@ -238,6 +242,29 @@ def extend_loans(self, extend_url: str, execute: bool = False) -> tuple[bool, di

return success, details

def extend_loans_by_ids(
self, acc_extids: list[tuple[str, str]], execute: bool = False
) -> tuple[bool, dict]:
"""Extend loan(s) via list of (account, extend_id) tuples. Will login first if needed.
For return value, exceptions thrown and more details, see `extend_loans()`
Args:
acc_eids: List of (account, extend_id) tuples
execute: A development flag; set to True actually perform loan extension
"""
if not acc_extids:
raise ValueError("List must not be empty.")
account_id, _extend_id = acc_extids[0] # use first acc id for general account id
ids = [f"{acc_id}|{ext_id}" for (acc_id, ext_id) in acc_extids]
ids = ",".join(ids)
url = (
self.BASE_URL
+ f"/mijn-bibliotheek/lidmaatschappen/{account_id}/uitleningen/verlengen"
+ f"?loan-ids={ids}"
)
return self.extend_loans(url, execute)

# *** INTERNAL METHODS ***

def _log_in(self, url):
Expand Down Expand Up @@ -520,6 +547,7 @@ def _parse_account_loans_page(cls, html: str, base_url: str, acc_id: str) -> lis
)
# Sometimes, this error is present
if soup.find(string=re.compile(error_msg)) is not None:
# TODO: probably better to thrown an exception instead
_log.warning(
f"Loans or reservations can not be retrieved. Site reports: {error_msg}"
)
Expand Down

0 comments on commit 3556fa3

Please sign in to comment.