From 25374a5ba34b767be5c8ba424d3ee58b0b88690d Mon Sep 17 00:00:00 2001 From: xenofem Date: Tue, 6 Feb 2024 08:29:52 -0500 Subject: [PATCH] default to using first page as thumbnail --- dlibrary/dlibrary.py | 47 ++++++++++++-------------- dlibrary/static/index.js | 2 +- dlibrary/templates/categorization.html | 2 +- dlibrary/templates/list.html | 2 +- dlibrary/templates/work.html | 2 +- 5 files changed, 25 insertions(+), 30 deletions(-) diff --git a/dlibrary/dlibrary.py b/dlibrary/dlibrary.py index 3675ae0..9952429 100755 --- a/dlibrary/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -111,7 +111,6 @@ async def fetch_async(args): } authors = dlsite_metadata.author or [] tags = dlsite_metadata.genre or [] - thumbnail_local = False thumbnail_url = dlsite_metadata.work_image if thumbnail_url.startswith('//'): thumbnail_url = 'https:' + thumbnail_url @@ -120,17 +119,11 @@ async def fetch_async(args): authors = db_row.pop('authors') tags = db_row.pop('tags') if FANZA_ID_REGEX.fullmatch(work_id): - thumbnail_local = False thumbnail_url = f'https://doujin-assets.dmm.co.jp/digital/comic/{work_id}/{work_id}pl.jpg' elif FAKKU_ID_REGEX.fullmatch(work_id): - thumbnail_local = True - thumbnail_path = min(work_path.iterdir()) - if not thumbnail_path.is_file(): - print(f'Fakku thumbnail path {thumbnail_path} is not a file! Skipping {work_id}') - continue + thumbnail_url = None else: - thumbnail_local = False - thumbnail_url = input('Thumbnail image URL: ') + thumbnail_url = input('Thumbnail image URL [default: first page]: ') cur.execute( "INSERT INTO works(id, title, circle, date, description, series) VALUES(:id, :title, :circle, :date, :description, :series)", @@ -145,11 +138,7 @@ async def fetch_async(args): [{ "tag": tag, "work": work_id } for tag in tags], ) - if thumbnail_local: - ext = thumbnail_path.suffix - dest_path = thumbnails_dir / (work_id + ext) - dest_path.symlink_to(relpath(thumbnail_path, thumbnails_dir)) - else: + if thumbnail_url: ext = url_file_ext(thumbnail_url) dest_file = thumbnails_dir / (work_id + ext) print(f'Downloading thumbnail for {work_id} from {thumbnail_url}') @@ -375,7 +364,9 @@ def generate(args): con = sqlite3.connect(args.destdir / 'meta.db') cur = con.cursor() - collated_work_ids = {p.name for p in (args.destdir / 'site' / 'images').iterdir()} + site_dir = args.destdir / 'site' + + collated_work_ids = {p.name for p in (site_dir / 'images').iterdir()} actual_series = {series for (series,) in cur.execute('SELECT series FROM works GROUP BY series HAVING count(series) > 1')} @@ -385,9 +376,16 @@ def generate(args): continue authors = [author for (author,) in cur.execute('SELECT author FROM authors WHERE work = ?', (work_id,))] tags = [tag for (tag,) in cur.execute('SELECT tag FROM tags WHERE work = ?', (work_id,))] - thumbnail_filename = next( - f for f in (args.destdir / 'site' / 'thumbnails').iterdir() if f.stem == work_id - ).name + + images = [path.name for path in (site_dir / 'images' / work_id).iterdir()] + images.sort() + + try: + thumbnail_path = relpath(next( + f for f in (site_dir / 'thumbnails').iterdir() if f.stem == work_id + ), site_dir) + except StopIteration: + thumbnail_path = f'images/{work_id}/{images[0]}' work = { 'id': work_id, 'title': title, @@ -397,14 +395,11 @@ def generate(args): 'series': series, 'authors': authors, 'tags': tags, - 'thumbnail_filename': thumbnail_filename, + 'thumbnail_path': thumbnail_path, } works.append(work) - images = [path.name for path in (args.destdir / 'site' / 'images' / work_id).iterdir()] - images.sort() - - work_dir = args.destdir / 'site' / 'works' / work_id + work_dir = site_dir / 'works' / work_id viewer_dir = work_dir / 'view' viewer_dir.mkdir(parents=True, exist_ok=True) with open(work_dir / 'index.html', 'w') as f: @@ -413,7 +408,7 @@ def generate(args): f.write(viewer_template.render(depth=3, work=work, title=title, images=images)) def make_categorization(categorization, query, work_filter, work_style_cards=False): - categorization_dir = args.destdir / 'site' / categorization + categorization_dir = site_dir / categorization cats = [cat for (cat,) in cur.execute(query)] cat_samples = {} @@ -465,9 +460,9 @@ def generate(args): ) with resources.as_file(resources.files("dlibrary")) as r: - copy_recursive(r / 'static', args.destdir / 'site' / 'static') + copy_recursive(r / 'static', site_dir / 'static') - with open(args.destdir / 'site' / 'index.html', 'w') as f: + with open(site_dir / 'index.html', 'w') as f: f.write(index_template.render(depth=0, works=works)) con.close() diff --git a/dlibrary/static/index.js b/dlibrary/static/index.js index 328476d..50d7be7 100644 --- a/dlibrary/static/index.js +++ b/dlibrary/static/index.js @@ -48,7 +48,7 @@ document.addEventListener('DOMContentLoaded', () => { card.appendChild(link); const thumb = document.createElement('img'); - thumb.src = `${ROOT}/thumbnails/${work.thumbnail_filename}`; + thumb.src = `${ROOT}/${work.thumbnail_path}`; link.appendChild(thumb); const creators = document.createElement('div'); diff --git a/dlibrary/templates/categorization.html b/dlibrary/templates/categorization.html index 7849819..40e6817 100644 --- a/dlibrary/templates/categorization.html +++ b/dlibrary/templates/categorization.html @@ -12,7 +12,7 @@ {{ cat }} {% if samples[cat] %} - + {% endif %} diff --git a/dlibrary/templates/list.html b/dlibrary/templates/list.html index 0e28cc5..940ca91 100644 --- a/dlibrary/templates/list.html +++ b/dlibrary/templates/list.html @@ -7,7 +7,7 @@ {% for work in works %}
- +
[{% if work['circle'] %}{{ work['circle'] }}{% endif %}{% if work['circle'] and work['authors'] %} ({% endif %}{{ ', '.join(work['authors']) }}{% if work['circle'] and work['authors'] %}){% endif %}]
diff --git a/dlibrary/templates/work.html b/dlibrary/templates/work.html index cf0f6bc..2bedfb2 100644 --- a/dlibrary/templates/work.html +++ b/dlibrary/templates/work.html @@ -5,7 +5,7 @@