From f813486db8c60d9659c99896c8ab49039d489256 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Sat, 13 Jul 2024 16:39:56 -0400 Subject: output the card expiration dates --- libraries/polaris.py | 1 + library_info_cli.py | 26 ++++++++++++++++++++------ library_info_lib.py | 18 ++++++++++++++++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/libraries/polaris.py b/libraries/polaris.py index e945fcc..8ff25d8 100644 --- a/libraries/polaris.py +++ b/libraries/polaris.py @@ -81,6 +81,7 @@ class Library(BaseLibrary): dates = [i.text.replace("(as of ","").replace(")","").replace("(","") for i in item.find_all("span",class_="patron-account__holds-date")] formats = [i["title"] for i in item.find_all("img") if "aria-label" not in i.attrs] img_b64, img_type = self.get_image(images_hrefs[0]) + # it is a happy accident that "date placed" for a ready reservation will indicate "until 7/17/2024". No work is required to capture this separately. obj = { "patron": self.alias, "position": values_dict["Hold Request Position"], diff --git a/library_info_cli.py b/library_info_cli.py index cf80043..2362c85 100755 --- a/library_info_cli.py +++ b/library_info_cli.py @@ -56,14 +56,14 @@ def html_td_img(i, imagepath, imagerelativepath): else: # Just print it inline in the html. img_src = i["img"] - return "" + return f"" else: # No image available. This might happen? return "none" # failsafe return "none" -def html(checkouts, reservations, imagepath, imagerelativepath): +def html(checkouts, reservations, imagepath, imagerelativepath, card_expiration_dates): # WORKHERE: need to revise this to take a list of config entries to load, so we can collect card expiration info """ Make a pretty html page of the items. If imagepath and imagerelativepath are defined, save the images to imagepath, and set the html img tags src attribute to the imagerelativepath. @@ -135,6 +135,13 @@ def html(checkouts, reservations, imagepath, imagerelativepath): prn(f"\n") else: prn("No reservations.\n") + prn(f"

Card expiration dates

\n") + prn(f"\n") + prn(f"\n") + for i in card_expiration_dates: + expires = i["expires"].strftime("%F") + prn(f"\n") + prn(f"
Patrondate
{i['patron']}{expires}
\n") prn(f"\n") prn(f"\n") @@ -200,16 +207,23 @@ if imagerelativepath is not None and imagepath is None: if "__main__" == __name__: if debuglevel >= 1: eprint(args) + card_expiration_dates = [] # will hold objects of {"alias": "aspen 1", "expires": datetime.date(2022,11,4)} if single: - checkouts, reservations = library_info_lib.get_single_configitem(alias = single, full_images = full_images, verbose = debuglevel >= 5) + checkouts, reservations, card_expiration_dates = library_info_lib.get_single_configitem(alias = single, full_images = full_images, verbose = debuglevel >= 5) else: - checkouts, reservations = library_info_lib.get_all_configitems(full_images = full_images, verbose = debuglevel >= 5) + checkouts, reservations, card_expiration_dates = library_info_lib.get_all_configitems(full_images = full_images, verbose = debuglevel >= 5) if "raw" == output: print(checkouts) print(reservations) + print(card_expiration_dates) elif "json" == output: - print(json.dumps(checkouts,default=serialize)) + output_json = { + "checkouts": checkouts, + "reservations": reservations, + "card_expiration_dates": card_expiration_dates, + } + print(json.dumps(output_json,default=serialize)) elif "html" == output: - html(checkouts, reservations, imagepath, imagerelativepath) + html(checkouts, reservations, imagepath, imagerelativepath, card_expiration_dates) else: print(f"Error! Invalid choice for output format {output}.") diff --git a/library_info_lib.py b/library_info_lib.py index ae724ae..c67c44a 100644 --- a/library_info_lib.py +++ b/library_info_lib.py @@ -21,19 +21,27 @@ def get_all_configitems(full_images = True, verbose = False): # get all checked out items checkouts = [] reservations = [] + card_expiration_dates = [] for i in config: #print(f"Found config entry {i}") instance = i["class"](i) checkouts += instance.get_checkouts(verbose = verbose) reservations += instance.get_reservations(verbose=verbose) + card_expiration_dates.append( + { + "patron": instance.alias, + "expires": instance.card_expires + } + ) if not full_images: checkouts = trim_full_images(checkouts) reservations = trim_full_images(reservations) - return checkouts, reservations + return checkouts, reservations, card_expiration_dates def get_single_configitem(alias = None, full_images = True, verbose = False): checkouts = [] reservations = [] + card_expiration_dates = [] this_config = [i for i in config if i["alias"] == alias] if not this_config: raise Exception(f"Alias not found: {alias}") @@ -42,10 +50,16 @@ def get_single_configitem(alias = None, full_images = True, verbose = False): instance = i["class"](i) checkouts += instance.get_checkouts(verbose=verbose) reservations += instance.get_reservations(verbose=verbose) + card_expiration_dates.append( + { + "patron": instance.alias, + "expires": instance.card_expires + } + ) if not full_images: checkouts = trim_full_images(checkouts) reservations = trim_full_images(reservations) - return checkouts, reservations + return checkouts, reservations, card_expiration_dates def trim_full_images(checkouts = []): output = [] -- cgit