mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
Feature: Pick best image from list (#745)
* Pick largest image from list * Add a safe value incase all requests fail * Formatting
This commit is contained in:
parent
3831eef508
commit
18b099b115
@ -47,7 +47,21 @@ def scrape_image(image_url: str, slug: str) -> Path:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if isinstance(image_url, list): # Handles List Types
|
if isinstance(image_url, list): # Handles List Types
|
||||||
image_url = image_url[0]
|
# Multiple images have been defined in the schema - usually different resolutions
|
||||||
|
# Typically would be in smallest->biggest order, but can't be certain so test each.
|
||||||
|
# 'Google will pick the best image to display in Search results based on the aspect ratio and resolution.'
|
||||||
|
|
||||||
|
all_image_requests = []
|
||||||
|
for url in image_url:
|
||||||
|
try:
|
||||||
|
r = requests.get(url, stream=True, headers={"User-Agent": ""})
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Image {url} could not be requested")
|
||||||
|
continue
|
||||||
|
if r.status_code == 200:
|
||||||
|
all_image_requests.append((url, r))
|
||||||
|
|
||||||
|
image_url, _ = max(all_image_requests, key=lambda url_r: len(url_r[1].content), default=("", 0))
|
||||||
|
|
||||||
if isinstance(image_url, dict): # Handles Dictionary Types
|
if isinstance(image_url, dict): # Handles Dictionary Types
|
||||||
for key in image_url:
|
for key in image_url:
|
||||||
|
@ -145,7 +145,7 @@ def clean_scraper(scraped_data: SchemaScraperFactory.SchemaScraper, url: str) ->
|
|||||||
return Recipe(
|
return Recipe(
|
||||||
name=try_get_default(scraped_data.title, "name", "No Name Found", cleaner.clean_string),
|
name=try_get_default(scraped_data.title, "name", "No Name Found", cleaner.clean_string),
|
||||||
slug="",
|
slug="",
|
||||||
image=try_get_default(scraped_data.image, "image", None),
|
image=try_get_default(None, "image", None),
|
||||||
description=try_get_default(None, "description", "", cleaner.clean_string),
|
description=try_get_default(None, "description", "", cleaner.clean_string),
|
||||||
nutrition=try_get_default(None, "nutrition", None, cleaner.clean_nutrition),
|
nutrition=try_get_default(None, "nutrition", None, cleaner.clean_nutrition),
|
||||||
recipe_yield=try_get_default(scraped_data.yields, "recipeYield", "1", cleaner.clean_string),
|
recipe_yield=try_get_default(scraped_data.yields, "recipeYield", "1", cleaner.clean_string),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user