Skip to content

Commit

Permalink
Enhance ZIM description handling and add support long description
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Jul 24, 2023
1 parent 977aaff commit 7186e5f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
5 changes: 5 additions & 0 deletions kolibri2zim/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def main():
help="Custom description for your ZIM. Kolibri channel description otherwise",
)

parser.add_argument(
"--long-description",
help="Custom long description for your ZIM, optional",
)

parser.add_argument(
"--favicon",
help="URL/path for Favicon. Kolibri channel thumbnail otherwise "
Expand Down
51 changes: 37 additions & 14 deletions kolibri2zim/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
from kiwixstorage import KiwixStorage
from pif import get_public_ip
from zimscraperlib.constants import (
MAXIMUM_DESCRIPTION_METADATA_LENGTH,
MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH,
MAXIMUM_DESCRIPTION_METADATA_LENGTH as MAX_DESC_LENGTH,
)
from zimscraperlib.constants import (
MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH as MAX_LONG_DESC_LENGTH,
)
from zimscraperlib.filesystem import get_file_mimetype
from zimscraperlib.i18n import find_language_names
Expand Down Expand Up @@ -50,6 +52,7 @@
"fname",
"title",
"description",
"long_description",
"creator",
"publisher",
"tags",
Expand Down Expand Up @@ -113,6 +116,7 @@ def go(option):
self.tags = [t.strip() for t in tags.split(",")]
self.title = go("title")
self.description = go("description")
self.long_description = go("long_description")
self.author = go("creator")
self.publisher = go("publisher")
self.name = go("name")
Expand Down Expand Up @@ -797,16 +801,8 @@ def run(self):
Name=self.clean_fname,
Language="eng",
Title=self.title,
Description=(
f"{self.description[0:MAXIMUM_DESCRIPTION_METADATA_LENGTH-4]} ..."
if len(self.description) > MAXIMUM_DESCRIPTION_METADATA_LENGTH
else self.description
),
LongDescription=(
f"{self.description[0:MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH-4]} ..."
if len(self.description) > MAXIMUM_LONG_DESCRIPTION_METADATA_LENGTH
else self.description
),
Description=self.description,
LongDescription=self.long_description,
Creator=self.author,
Publisher=self.publisher,
Date=datetime.datetime.now(datetime.UTC),
Expand Down Expand Up @@ -930,8 +926,35 @@ def sanitize_inputs(self):
self.title = self.title.strip()

if not self.description:
self.description = channel_meta["description"]
self.description = self.description.strip()
# User did not provided a description, we will infer it from channel
# metadata, limited to maximum length
if self.long_description:
raise ValueError(
"long_description cannot be set if description is not set"
)
self.description = channel_meta["description"].strip()
if len(self.description) > MAX_DESC_LENGTH:
self.long_description = self.description
self.description = f"{self.description[0:MAX_DESC_LENGTH-1]}…"
if len(self.long_description > MAX_LONG_DESC_LENGTH):
self.long_description = (
f"{self.long_description[0:MAX_LONG_DESC_LENGTH-1]}…"
)
else:
self.description = self.description.strip()
if len(self.description) > MAX_DESC_LENGTH:
raise ValueError(
f"description is too long ({len(self.description)}"
f">{MAX_DESC_LENGTH})"
)
if (
self.long_description
and len(self.long_description) > MAX_LONG_DESC_LENGTH
):
raise ValueError(
f"long_description is too long ({len(self.long_description)}"
f">{MAX_LONG_DESC_LENGTH})"
)

if not self.author:
self.author = channel_meta["author"] or "Kolibri"
Expand Down

0 comments on commit 7186e5f

Please sign in to comment.