aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xlibrary_info_cli.py57
1 files changed, 32 insertions, 25 deletions
diff --git a/library_info_cli.py b/library_info_cli.py
index e2aeaca..1de3cf3 100755
--- a/library_info_cli.py
+++ b/library_info_cli.py
@@ -33,6 +33,36 @@ def serialize(item):
eprint(f"WARNING: unknown type {type(item)} for json-serializing object {item}")
return item
+def html_td_img(i, imagepath, imagerelativepath):
+ """
+ Given the item, imagepath, and imagerelativepath, prepare the table data object in html.
+ It is possible for imagepath and imagerelativepath to be None, in which case the image will be stored inline in the html.
+ """
+ if "img" in i:
+ if imagepath and imagerelativepath:
+ ext = i["img_type"].split("/")[-1]
+ img_contents = base64.b64decode(i["img"])
+ # use chars -25 to -5 (so 20 characters long, 5 from the right end)
+ # to avoid the == at the end
+ # also img50 was annoying.
+ filename = f"{i['img'][-25:-5:]}.{ext}".replace("/","_")
+ filepath = os.path.join(imagepath, filename)
+ relativefilepath = os.path.join(imagerelativepath, filename)
+ if debuglevel >= 3:
+ eprint(f"Writing to file {filepath} for {i['title']}")
+ with open(filepath,"wb") as w:
+ w.write(img_contents)
+ return f"<td><img class='thumb' src='{relativefilepath}' /></td>"
+ else:
+ # Just print it inline in the html.
+ img_src = i["img"]
+ return "<td><img class='thumb' src='data:{i['img_type']};base64, {img_src}' /></td>"
+ else:
+ # No image available. This might happen?
+ return "<td>none</td>"
+ # failsafe
+ return "<td>none</td>"
+
def html(checkouts, reservations, imagepath, imagerelativepath):
"""
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.
@@ -78,26 +108,7 @@ def html(checkouts, reservations, imagepath, imagerelativepath):
due = i["due"].strftime("%F")
prn(f"<td>{due}</td>")
prn(f"<td>{i['format']}</td>")
- if "img" in i:
- img_src = i["img"]
- if imagepath and imagerelativepath:
- ext = i["img_type"].split("/")[-1]
- img_contents = base64.b64decode(i["img"])
- # use chars -25 to -5 (so 20 characters long, 5 from the right end)
- # to avoid the == at the end
- # also img50 was annoying.
- filename = f"{i['img'][-25:-5:]}.{ext}".replace("/","_")
- filepath = os.path.join(imagepath, filename)
- relativefilepath = os.path.join(imagerelativepath, filename)
- with open(filepath,"wb") as w:
- if debuglevel >= 3:
- eprint(f"Writing to file {filepath} for {i['title']}")
- w.write(img_contents)
- prn(f"<td><img class='thumb' src='{relativefilepath}' /></td>")
- else:
- prn(f"<td><img class='thumb' src='data:{i['img_type']};base64, {img_src}' /></td>")
- else:
- prn(f"<td>none</td>")
+ prn(html_td_img(i, imagepath, imagerelativepath))
prn(f"<td>{i['title']}</td>")
prn(f"</tr>\n")
prn(f"</tbody>\n")
@@ -116,11 +127,7 @@ def html(checkouts, reservations, imagepath, imagerelativepath):
prn(f"<td>{i['location']}</td>")
prn(f"<td>{i['position']}</td>")
prn(f"<td>{i['status']}</td>")
- if "img" in i:
- img_src = i["img"]
- else:
- img_src = ""
- prn(f"<td><img class='thumb' src='data:{i['img_type']};base64, {img_src}' /></td>")
+ prn(html_td_img(i, imagepath, imagerelativepath))
prn(f"<td>{i['title']}</td>")
prn(f"</tr>\n")
prn(f"</tbody>\n")
bgstack15