Skip to content

Commit 5dc1085

Browse files
authored
Merge pull request #119 from openzim/custom_icon
Add support for custom ZIM illustration
2 parents e1b706e + 91f696a commit 5dc1085

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Make the UI responsive (#22)
1313
- Enhance the UI design to be closer to original FCC UI (#23)
14+
- Add support for custom ZIM illustration (#79)
1415

1516
## [1.3.0] - 2025-01-24
1617

scraper/src/fcc2zim/context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class Context:
7070
# Date when the crawl started
7171
start_date: datetime.date
7272

73+
# Path or URL to use as ZIM illustration
74+
illustration: str | None = None
75+
7376
# logger to use everywhere (do not mind about mutability, we want to reuse same
7477
# logger everywhere)
7578
logger: logging.Logger = getLogger( # noqa: RUF009

scraper/src/fcc2zim/entrypoint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ def prepare_context(raw_args: list[str]) -> None:
146146
dest="overwrite_existing_zim",
147147
)
148148

149+
parser.add_argument(
150+
"--illustration",
151+
help="Path or URL to ZIM illustration. Bitmap and SVG formats are supported.",
152+
)
153+
149154
args = parser.parse_args(raw_args)
150155

151156
# Ignore unset values so they do not override the default specified in Context

scraper/src/fcc2zim/scraper.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import io
12
import logging
23
from pathlib import Path
34

4-
from zimscraperlib.inputs import compute_descriptions
5+
from zimscraperlib.image.conversion import convert_image, convert_svg2png
6+
from zimscraperlib.image.probing import format_for
7+
from zimscraperlib.image.transformation import resize_image
8+
from zimscraperlib.inputs import compute_descriptions, handle_user_provided_file
59
from zimscraperlib.zim import Creator, metadata
610

711
from fcc2zim.build import build_command
@@ -81,10 +85,36 @@ def __init__(self):
8185
else:
8286
logger.info(f"ZIM path: {self.zim_path}")
8387

84-
logo_path = Path(__file__).parent.joinpath("assets", "fcc_48.png")
88+
if context.illustration:
89+
logo_path = handle_user_provided_file(context.illustration)
90+
if not logo_path:
91+
raise ValueError(f"Logo not found at {context.illustration}")
92+
else:
93+
logo_path = Path(__file__).parent.joinpath("assets", "fcc_48.png")
8594
if not logo_path.exists():
8695
raise ValueError(f"Logo not found at {logo_path}")
8796

97+
illustration = io.BytesIO()
98+
illustration_format = format_for(logo_path, from_suffix=False)
99+
if illustration_format == "SVG":
100+
convert_svg2png(
101+
logo_path,
102+
illustration,
103+
metadata.DefaultIllustrationMetadata.illustration_size,
104+
metadata.DefaultIllustrationMetadata.illustration_size,
105+
)
106+
else:
107+
if illustration_format != "PNG":
108+
convert_image(logo_path, illustration, fmt="PNG")
109+
else:
110+
illustration = io.BytesIO(logo_path.read_bytes())
111+
resize_image(
112+
illustration,
113+
width=metadata.DefaultIllustrationMetadata.illustration_size,
114+
height=metadata.DefaultIllustrationMetadata.illustration_size,
115+
method="cover",
116+
)
117+
88118
self.creator = Creator(self.zim_path, "index.html").config_metadata(
89119
std_metadata=metadata.StandardMetadataList(
90120
Name=metadata.NameMetadata(context.name),
@@ -94,7 +124,7 @@ def __init__(self):
94124
Publisher=metadata.PublisherMetadata(context.publisher),
95125
Date=metadata.DateMetadata(context.start_date),
96126
Illustration_48x48_at_1=metadata.DefaultIllustrationMetadata(
97-
logo_path.read_bytes()
127+
illustration
98128
),
99129
Description=metadata.DescriptionMetadata(context.description),
100130
LongDescription=(

0 commit comments

Comments
 (0)