Skip to content

Article details #19

Open
Open

Description

@littleyoda

Describe the bug

Currently, very little information is being returned regarding the article:

{
    "name": "Rasting H\u00e4hnchen-Minutenschnitzel",
    "id": "s1028337"
}

My implementation returns almost all available information as JSON.

Steps to reproduce the issue

from python_picnic_api2 import PicnicAPI
import json
import jq

p = PicnicAPI("XXXX","XXXXX",'de');

JQ_EXTRACTION_FILTER = r'''
# Extract accordion data
def accordion_data:
  [
    .body.child.child.children[]? | 
    select(.id == "accordion-section")? | 
    .children[]? | 
    select(.id == "product-page-accordions")? | 
    .pml.component.items[]? | 
    (.header.child.children[]? | select(.type == "ICON" and has("iconKey"))? | .iconKey) as $icon |
    if $icon == "apple" then
      {
        iconKey: $icon,
        markdowns: [.body.child.children[]? | select(.type == "STACK") | 
                    [.children[]? | select(.type == "RICH_TEXT" and has("markdown")) | .markdown] | 
                    select(length > 0)]
      }
    else
      {
        iconKey: $icon,
        markdowns: [.body | .. | objects | select(.type == "RICH_TEXT" and has("markdown")) | .markdown]
      }
    end |
    select(.iconKey and (.markdowns | length > 0))
  ] | 
  map({(.iconKey): .markdowns}) | 
  add // {};

# Extract name data from main container
def name_data:
  (.. | objects | select(.id == "product-details-page-root-main-container") | 
   .pml.component.children) as $children |
  
  if ($children | length) > 0 then
    # First child is always the product name (RICH_TEXT with HEADER1)
    ($children[0] | select(.type == "RICH_TEXT" and .textType == "HEADER1") | .markdown | gsub("#\\(#[0-9a-fA-F]+\\)"; "")) as $name |
    
    # Check if second child is manufacturer (RICH_TEXT with textAttributes, no textType) or skip if STACK
    (if ($children | length) > 1 and ($children[1].type == "RICH_TEXT") and (($children[1].textType // null) == null) and ($children[1] | has("textAttributes")) 
     then ($children[1].markdown) 
     else null end) as $manufacturer |
    
    # Find weight in STACK children
    ($children[] | select(.type == "STACK") | .children[]? | select(.type == "RICH_TEXT" and has("markdown")) | .markdown | gsub("#\\(#[0-9a-fA-F]+\\)"; "")) as $weight |
    
    {
      product: ($name // null),
      manufacturer: ($manufacturer // null),  
      quantity: ($weight // null),
      name: (if ($manufacturer // null) != null then ($manufacturer + " " + ($name // "")) else ($name // null) end)
    }
  else {} end;

# Extract image IDs from main image container
def image_data:
  (.. | objects | select(.id == "product-page-image-gallery-main-image-container") | 
   [.. | objects | select(.type == "IMAGE" and has("source")) | .source.id] ) as $image_ids |
  if ($image_ids | length) > 0 then {image_ids: $image_ids} else {} end;

# Extract selling_unit_id from analytics
def selling_unit_data:
  ([.. | objects | select(has("analytics")) | .analytics.contexts[]? | select(has("data")) | .data | select(has("selling_unit_id")) | .selling_unit_id] | unique) as $selling_unit_ids |
  if ($selling_unit_ids | length) > 0 then {selling_unit_id: ($selling_unit_ids[0] // null)} else {} end;

# Extract product description (optional) 
def description_data:
  ([.. | objects | select(.id == "product-page-description") | .. | objects | select(.type == "RICH_TEXT" and has("markdown")) | .markdown]) as $description_markdowns |
  {"product-description": $description_markdowns};

# Extract categories from target URLs
def category_data:
  ([.. | objects | select(has("target")) | .target | select(test("app\\.picnic://categories/")) | capture("app\\.picnic://categories/(?<l1>[0-9]+)(/l2/(?<l2>[0-9]+))?(/l3/(?<l3>[0-9]+))?") | [.l1, .l2, .l3] | map(select(. != null)) | map(tonumber)] | unique) as $categories |
  if ($categories | length) > 0 then {"categories": ($categories[0] // [])} else {} end;

# Extract allergies from product-page-allergies
def allergies_data:
  ([.. | objects | select(.id == "product-page-allergies") | .. | objects | select(.type == "RICH_TEXT" and has("markdown")) | .markdown] | unique) as $allergies |
  if ($allergies | length) > 0 then {"allergies": $allergies} else {} end;

# Rename keys after extraction
(accordion_data // {}) + (name_data // {}) + (image_data // {}) + (selling_unit_data // {}) + (description_data // {}) + (category_data // {}) + (allergies_data // {}) |
with_entries(
  if .key == "whisk" then .key = "preparation"
  elif .key == "apple" then .key = "nutritional_values"
  elif .key == "list" then .key = "ingredients"  
  elif .key == "infoCircle" then .key = "info"
  else .
  end
)
'''



def get_article_details(article_id):
    path = f"/pages/product-details-page-root?id={article_id}&show_category_action=true"
    data = p._get(path, add_picnic_headers=True)

    # Use jq to extract accordion data
    compiled_filter = jq.compile(JQ_EXTRACTION_FILTER)
    
    # Handle StopIteration for queries that return no results
    try:
        result = compiled_filter.input(data).first()
    except StopIteration:
        return None
    
    if not result or not isinstance(result, dict) or not result:
        return None
    return result
  
print("Current implementation:")
data = p.get_article("s1028337")
print(json.dumps(data, indent=4))

print("\nDetails:")
d = get_article_details("s1028337")
print(json.dumps(d, indent=4))

What did you expect to happen instead?

It would be great if you could somehow integrate the routine.

Python Version

No response

Activity

  1. added
    bug
    on Sep 13, 2025
  2. littleyoda commented on Sep 13, 2025

    @littleyoda
    Author

    Here is example of what will be returned. (Sorry, everything is in German.)

    {
        "ingredients": [
            "100% H\u00e4hnchenbrustfilet"
        ],
        "nutritional_values": [
            [
                "Pro 100g"
            ],
            [
                "Brennwert",
                "440 kJ /"
            ],
            [
                "kcal",
                "105 kcal"
            ],
            [
                "Fett",
                "1,7g"
            ],
            [
                "davon ges\u00e4ttigte Fetts\u00e4uren",
                "0,5g"
            ],
            [
                "Kohlenhydrate",
                "0,0g"
            ],
            [
                "Zucker",
                "0,0g"
            ],
            [
                "Eiwei\u00df",
                "22,2g"
            ],
            [
                "Salz",
                "0,14g"
            ]
        ],
        "info": [
            "**Aufbewahrung**\nBei -2\u00b0C bis +4\u00b0C lagern.\n\n**Haltungsform 2**\nBei der Haltungsform 2 haben die Tiere mehr Bewegungsfreiraum und k\u00f6nnen ihrem nat\u00fcrlichen Spieltrieb mit extra Besch\u00e4ftigungsmaterial nachgehen.\n\n**Ursprungsland**\nDeutschland\n\nDas Ursprungsland kann je nach Angebot abweichen und ist dem Aufdruck am Artikel zu entnehmen, welcher bei der Lieferung \u00fcberpr\u00fcft werden kann.\n\n**Verantwortlicher Lebensmittelunternehmer**\nFleischhof Rasting GmbH\nEisbachstr./Am Pannacker 53340 Meckenheim"
        ],
        "product": "H\u00e4hnchen-Minutenschnitzel",
        "manufacturer": "Rasting",
        "quantity": "400g",
        "name": "Rasting H\u00e4hnchen-Minutenschnitzel",
        "image_ids": [
            "2252cf5724d2b93bc289e47f4bc25164c0910c8c1c5021569ab26d40e76cece9",
            "2e1ac37578803b35af24d8c1a1f4ced7d13f7f6fd7b7315986ab37f119e4c52f"
        ],
        "selling_unit_id": "s1028337",
        "product-description": [
            "Saftige Minutenschnitzel aus frischem H\u00e4hnchenfleisch, perfekt f\u00fcr die schnelle Pfanne oder den Grill. Einfach w\u00fcrzen und in wenigen Minuten goldbraun braten.\n\n#(GREEN1)**Frische-Versprechen**#(GREEN1)\nDirekt von hier zu dir: Damit du zu 100 % zufrieden bist."
        ],
        "categories": [
            19359,
            22106,
            22109
        ]
    }
    
  3. littleyoda commented on Sep 14, 2025

    @littleyoda
    Author

    Current Version can be found here:
    https://github.com/littleyoda/python-picnic-api

  4. added
    enhancement
    and removed
    bug
    on Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions