Skip to content

Handle enclosure tags and extract URL -(working solution) #117

Description

@hardrockhodl

To modify the code to handle the <enclosure> tag as the image part, you can update the logic within the update method. Specifically, you can check for the presence of the tag and extract the url attribute as the image URL.

# Check for the enclosure tag
if "enclosures" in entry:
    enclosure = entry["enclosures"][0]  # Assuming only one enclosure
    entry_value["image"] = enclosure.get("url", "")

if not entry_value.get("image"):
    # If there's no enclosure, check for image in summary
    images = []
    if "summary" in entry:
        images = re.findall(
            r"<img.+?src=\"(.+?)\".+?>", entry["summary"]
        )
    if images:
        entry_value["image"] = images[0]
    else:
        entry_value[
            "image"
        ] = "https://www.home-assistant.io/images/favicon-192x192-full.png"

This modification checks for the presence of the enclosures key in the entry. If it exists, it assumes there is at least one enclosure, and it extracts the url attribute from the first enclosure as the image URL. If there is no enclosure, it falls back to the previous logic of checking for an image in the summary.

Activity

  1. ogajduse commented on Jan 10, 2024

    @ogajduse
    Collaborator

    @hardrockhodl Could you please check the latest implementation from the master branch? I have slightly modified the implementation around getting the images from the feed entries. You might want to try installing the latest beta release using HACS, if you have the integration installed through HACS.

    If the latest implementation still does not work, I am happy to work on a solution with you.

  2. somansch commented on Feb 17, 2024

    @somansch

    @ogajduse ,
    I'm also having problems to get images from these two feeds. I tried latest v2.0b6.

    https://rss.sueddeutsche.de/alles/

    • img link is part of summary, but not set as "image"

    https://www.stern.de/feed/standard/auto/

    • img link is second under "links", but not set as "image"

    Do you need more information? Thanks for your support

  3. perlchamp commented on Feb 24, 2025

    @perlchamp

    Still a problem ?
    try this for sueddeutsche:

    {% set description = your_"how_you_fetch_the_content_of_description-xml-tag" %}
    {% set imgurl = description | regex_findall_index('(?<=<img src=")[\w:/._ \-]+(?=\?)') %}
    {{imgurl}}
    

    for stern (the enclosure doesn't work ?):
    could you please post the entry-content ?

    you will get this by:

    1. HA -> Entwicklerwerkzeuge -> template
    2. in the editor put in something like this:
    {%- for entry in state_attr('YOUR_SENSOR_ENTITY-ID', 'entries') -%}  {# ENTITY-ID: something like 'sensor.it_news' #}
      {{entry}}
    {% endfor %}
    
    1. copy the result from the preview-window and paste it here, so i can create the regex-filter...

    i opened the site and this is actually :
    <enclosure url="https://image.stern.de/30553162/t/7j/v3/w1440/r1.7778/-/e-bike-zubehoer.jpg" length="0" type="image/jpeg"/>

    if you get such a simulary content from the enclosure-tag (i will name it 'enclosure_tag'), then try this:

    {% set imgurl = enclosure_tag | regex_findall_index('(?<=url=")[\w:/._ \-]+(?=")') %}
    {{imgurl}}
    

    good luck
    P.s: du kannst auch in deutsch antworten... ;-)

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions