Skip to content

Commit

Permalink
Convert Webp to JPEG using JNC URL conversion scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
gvellut committed Oct 19, 2024
1 parent 8892acb commit 043a045
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions jncep/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ async def fetch_content_and_images_for_part(session, part_id):
tasks = [partial(fetch_image, session, img_url) for img_url in img_urls]
images = await bag(tasks)

# in case the url was converted in fetch_image : change content so correct
# reference
# TODO replace with local filename (done later: URL changed to local filename)
for i, img_url in enumerate(img_urls):
content = content.replace(img_url, images[i].url)

# filter images with download error
images = list(filter(None, images))
for i, image in enumerate(images):
Expand All @@ -684,8 +690,17 @@ async def fetch_content_and_images_for_part(session, part_id):
return content, images


def webp_to_jpeg(img_url: str):
# convert from webp to jpeg (webp not readable in some physical epub reader
# like kobo); scheme documented here : https://forums.j-novel.club/post/374895
return img_url.replace("/webp/", "/jpg/", 1)


async def fetch_image(session, img_url):
try:
# not always needed: only for relases after october 2024
# TODO add option to prevent that default behavior; need to pass options down
img_url = webp_to_jpeg(img_url)
img_bytes = await session.api.fetch_url(img_url)
image = Image(img_url, img_bytes)
image.local_filename = _local_image_filename(image)
Expand Down

0 comments on commit 043a045

Please sign in to comment.