From 3714eecab9961804c7575975ce90ac564862101c Mon Sep 17 00:00:00 2001 From: xenofem Date: Mon, 22 Jan 2024 02:58:17 -0500 Subject: [PATCH] implement metadata viewing/editing and basic PDF image extraction --- dlibrary.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/dlibrary.py b/dlibrary.py index d958bcc..9f04a08 100755 --- a/dlibrary.py +++ b/dlibrary.py @@ -150,7 +150,18 @@ def collate(args): con.close() def link_pdf(src, dest): - print(f'PDF support not yet implemented, skipping {src}') + with fitz.open(src) as pdf: + images_by_page = [page.get_images() for page in pdf] + if all(len(images) == 1 for images in images_by_page): + dest.mkdir(parents=True) + for (idx, images) in enumerate(images_by_page): + xref = images[0][0] + image = pdf.extract_image(xref) + file_path = dest / f'{idx:04d}.{image["ext"]}' + with open(file_path, 'wb') as f: + f.write(image["image"]) + else: + print(f'Support for weirder PDFs not yet implemented, skipping {src}') def complete_prefix_number_ordering(entries): matches = reversed(list(NUMBER_REGEX.finditer(entries[0].name))) @@ -182,7 +193,32 @@ def link_ordered_files(ordering, dest): link_path.symlink_to(relpath(src_path, dest)) def metadata(args): - pass + con = sqlite3.connect(args.destdir / 'meta.db') + cur = con.cursor() + + if args.virtual is not None: + cur.execute("UPDATE works SET virtual = ? WHERE id = ?", (1 if args.virtual else 0, args.work_id)) + con.commit() + + res = cur.execute( + "SELECT title, circle, date, description, series, virtual FROM works WHERE id = ?", + (args.work_id,), + ).fetchone() + + if res is None: + print(f'Work id {args.work_id} not found!') + return + + (title, circle, date, description, series, virtual) = res + print(f'Work ID: {args.work_id}') + print(f'Title: {title}') + print(f'Circle: {circle}') + print(f'Pub date: {date}') + print(f'Description: {description}') + print(f'Series: {series}') + print(f'Virtual: {"Yes" if virtual == 1 else "No"}') + + con.close() def publish(args): pass