forked from MikeBrink/python-picnic-api
-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Describe the bug
I was able to figure out the recipe topic a bit.
The source code shows how to search for recipes and how to display recipe information.
Since I don't know if and how you want to integrate it into the library, it's just a simple Python script for now.
To parse the JSON files properly, I'm using jq.
Steps to reproduce the issue
run this script:
from python_picnic_api2 import PicnicAPI
# pip install jq
import jq
import urllib.parse
p = PicnicAPI("XXX","XXXXXX",'de');
def getRecipes(searchTerm):
searchTerm = urllib.parse.quote_plus(searchTerm)
recipes = p._get(f"/pages/search-page-results?search_term={searchTerm}&page_context=MEALS&is_recipe=true", add_picnic_headers=True)
query = (
# Search in the recipes search results
'.. | objects | select(.id=="search-flat-recipes-result") | .children[] | {'
# Recipe Name
' name: .pml.component.accessibilityLabel, '
# Return first recipe_id
'recipe_id: ([ .. | objects | .recipe_id? ] | map(select(. != null)) | .[0]), '
# Return first image_id
'image_id: ([ .. | objects | select(.type=="IMAGE") | .source.id? ] | map(select(. != null)) | .[0])'
'}'
)
compiled = jq.compile(query)
return compiled.input(recipes).all()
def getRecipeDetails(recipe_id):
data = p._get(f"/pages/recipe-details-page?recipe_id={recipe_id}", add_picnic_headers=True)
extraction = {
"ingredients":(
".. | objects | select(.id==\"recipe-core-ingredients-details-section\")"
" | .. | objects | .markdown? | select(type == \"string\")"
),
"utensils":(
".. | objects | select(.id==\"recipe-ingredients-utensils\")"
" | .. | objects | .markdown? | select(type == \"string\")"
),
"instructions": (
".. | objects | select(.id==\"recipe-details-instructions-section\")"
" | .. | objects | .markdown? | select(type == \"string\")"
),
"description": (
".. | objects | select(.id==\"recipe-description-section\")"
" | .. | objects | .markdown? | select(type == \"string\")"
),
"articles": (
".. | objects | select(.id==\"recipe-portioning-content-wrapper\")"
" | .. | objects | .ingredientsState? | select(. != null) | .[]"
),
"name": (
".. | objects | select(.type==\"RICH_TEXT\" and .textType==\"HEADLINE1\")"
" | .markdown? | select(type == \"string\")"
),
"url": (
".. | strings | select(test(\"https://picnic[.]app/de/go/[A-Za-z0-9]+\"))"
" | match(\"https://picnic[.]app/de/go/[A-Za-z0-9]+\").string"
)
}
out = {}
for desc, expression in extraction.items():
compiled = jq.compile(expression)
out[desc] = compiled.input(data).all()
return out
recipies = getRecipes("pizza")
print("First recipe in the search")
print(json.dumps(recipies[0], ensure_ascii=False, indent=2))
print("Details for " + recipies[0]["name"])
details = getRecipeDetails(recipies[0]["recipe_id"])
print(json.dumps(details, ensure_ascii=False, indent=2))
print("Ingredients:")
for article in details["articles"]:
a = p.get_article(article["sellingUnitId"])
print(f'{article["requiredAmount"]} {a["name"]}')
What did you expect to happen instead?
none
Python Version
No response
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request