[foods] Add recipe website parsing
Some checks failed
build & deploy / test (push) Failing after 1m10s
build & deploy / deploy (push) Has been skipped

This commit is contained in:
2026-03-06 10:46:34 -05:00
parent 210ff0a4aa
commit 770a51b9c0
12 changed files with 1493 additions and 29 deletions

View File

@ -5,6 +5,7 @@ from typing import Any, Optional
import pendulum
import pytz
import requests
from beers.models import Beer
from boardgames.models import BoardGame, BoardGameDesigner, BoardGameLocation
from books.constants import READCOMICSONLINE_URL
@ -13,6 +14,8 @@ from books.utils import parse_readcomicsonline_uri
from bricksets.models import BrickSet
from dateutil.parser import parse
from django.utils import timezone
from foods.models import Food
from foods.sources.rscraper import RecipeScraperService
from locations.constants import LOCATION_PROVIDERS
from locations.models import GeoLocation
from music.constants import JELLYFIN_POST_KEYS, MOPIDY_POST_KEYS
@ -81,7 +84,10 @@ def mopidy_scrobble_media(post_data: dict, user_id: int) -> Scrobble:
log = {}
try:
log = {"mopidy_source": post_data.get("mopidy_uri", "").split(":")[0]}
log = {
"mopidy_source": post_data.get("mopidy_uri", "").split(":")[0],
"raw_data": post_data,
}
except IndexError:
pass
@ -544,6 +550,40 @@ def email_scrobble_board_game(
return scrobbles_created
def scrobble_from_recipe_website(
url: str,
user_id: int,
source: str = "Vrobbler",
action: Optional[str] = None,
) -> Scrobble:
"""Scrobble a recipe from a website URL."""
food, created = Food.find_or_create_from_recipe(url)
scrobble_dict = {
"user_id": user_id,
"timestamp": timezone.now(),
"playback_position_seconds": 0,
"source": source,
}
logger.info(
"[vrobbler-scrobble] recipe scrobble request received",
extra={
"food_id": food.id,
"user_id": user_id,
"scrobble_dict": scrobble_dict,
"media_type": Scrobble.MediaType.FOOD,
"created_bool": created,
},
)
scrobble = Scrobble.create_or_update(food, user_id, scrobble_dict)
if action == "stop":
scrobble.stop(force_finish=True)
return scrobble
def manual_scrobble_from_url(
url: str,
user_id: int,
@ -554,6 +594,12 @@ def manual_scrobble_from_url(
we want to scrobble as a media type in and of itself. This checks whether
we know about the content type, and routes it to the appropriate media
scrobbler. Otherwise, return nothing."""
if RecipeScraperService.is_recipe_url(url):
return scrobble_from_recipe_website(
url, user_id, source=source, action=action
)
content_key = ""
domain = extract_domain(url)
@ -906,6 +952,12 @@ def manual_scrobble_webpage(
source: str = "Bookmarklet",
action: Optional[str] = None,
):
if RecipeScraperService.is_recipe_url(url):
return scrobble_from_recipe_website(
url, user_id, source=source, action=action
)
webpage = WebPage.find_or_create({"url": url})
scrobble_dict = {