Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unnecessary pillow import by using image size from CDN endpoint #218

Merged
merged 9 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions geotribu_cli/content/header_check.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import argparse
import logging
import os
from functools import lru_cache
from pathlib import Path

import frontmatter
import requests
from requests import Response

from geotribu_cli.constants import GeotribuDefaults
from geotribu_cli.json.json_client import JsonFeedClient
from geotribu_cli.utils.check_image_size import get_image_dimensions_by_url
from geotribu_cli.utils.check_path import check_path
from geotribu_cli.utils.slugger import sluggy

logger = logging.getLogger(__name__)
defaults_settings = GeotribuDefaults()

URL_CDN = "https://cdn.geotribu.fr"

gounux marked this conversation as resolved.
Show resolved Hide resolved
MANDATORY_KEYS = [
"title",
"authors",
Expand Down Expand Up @@ -110,10 +114,21 @@ def check_author_md(author: str, folder: Path) -> bool:
return os.path.exists(p)


@lru_cache(maxsize=512)
def download_image_sizes() -> dict:
# download images sizes and indexes
image_req: Response = requests.get(f"{URL_CDN}/img/search-index.json")
image_req.raise_for_status()
return image_req.json()["images"]
gounux marked this conversation as resolved.
Show resolved Hide resolved


def check_image_size(
image_url: str, minw: int, maxw: int, minh: int, maxh: int
image_url: str, images: dict, minw: int, maxw: int, minh: int, maxh: int
) -> bool:
width, height = get_image_dimensions_by_url(image_url)
key = image_url.replace(f"{URL_CDN}/img/", "")
if key not in images:
return False
width, height = images[key]
return minw <= width <= maxw and minh <= height <= maxh


Expand Down Expand Up @@ -179,6 +194,7 @@ def run(args: argparse.Namespace) -> None:
logger.error("Pas d'URL pour l'image")
elif not check_image_size(
yaml_meta["image"],
download_image_sizes(),
args.min_image_width,
args.max_image_width,
args.min_image_height,
Expand Down
12 changes: 10 additions & 2 deletions tests/test_utils_images_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ def test_check_image_dimensions(self):

def test_image_url_dimensions(self):
for url, width, height in [
("https://cdn.geotribu.fr/img/coup_de_gueule.jpg", 74, 64),
("https://cdn.geotribu.fr/img/pytroll.png", 100, 100),
(
"https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/lidarhd_pdal/lidar_zoom.png",
1380,
763,
),
(
"https://cdn.geotribu.fr/img/articles-blog-rdp/articles/2024/lidarhd_pdal/sol.png",
1000,
557,
),
]:
w, h = get_image_dimensions_by_url(url)
self.assertEqual(w, width)
Expand Down
Loading