Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enclosures property does not exist if entry has an id but no link #340

Open
philgyford opened this issue Mar 10, 2023 · 2 comments
Open

Comments

@philgyford
Copy link

philgyford commented Mar 10, 2023

Normally I find that an entry's enclosures property always exists, but is an empty list if the entry has no enclosures.

However, if an entry has an id, but no link, then the enclosures property does not exist.

Three examples of this with Atom feeds (I think it was the same with RSS), using python 3.10 and feedparser 6.0.10:

1. Has link but no id - enclosures property exists

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Test feed</title>
  <link href="https://example.org/"/>
  <id>https://example.org/</id>
  <entry>
    <link>https://example.org/items/1</link>
    <title>Hello</title>
    <published>2022-12-07T10:40:00+00:00</published>
    <summary>Hello.</summary>
  </entry>
</feed>
>>> d = feedparser.parse("https://example.org/test.xml")
>>> d.entries[0].enclosures
[]

2. Has both link and id - enclosures property exists

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Test feed</title>
  <link href="https://example.org/"/>
  <id>https://example.org/</id>
  <entry>
    <link>https://example.org/items/1</link>
    <id>https://example.org/items/1</id>
    <title>Hello</title>
    <published>2022-12-07T10:40:00+00:00</published>
    <summary>Hello.</summary>
  </entry>
</feed>
>>> d = feedparser.parse("https://example.org/test.xml")
>>> d.entries[0].enclosures
[]

3. Has id but no link - enclosures property does not exist

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Test feed</title>
  <link href="https://example.org/"/>
  <id>https://example.org/</id>
  <entry>
    <id>https://example.org/items/1</id>
    <title>Hello</title>
    <published>2022-12-07T10:40:00+00:00</published>
    <summary>Hello.</summary>
  </entry>
</feed>
>>> d = feedparser.parse("https://example.org/test.xml")
>>> d.entries[0].enclosures
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /usr/local/lib/python3.10/site-packages/feedparser/util.py:156, in FeedParserDict.__getattr__(self, key)
    155 try:
--> 156     return self.__getitem__(key)
    157 except KeyError:

File /usr/local/lib/python3.10/site-packages/feedparser/util.py:65, in FeedParserDict.__getitem__(self, key)
     62     norel = lambda link: FeedParserDict([(name, value) for (name, value) in link.items() if name != 'rel'])
     63     return [
     64         norel(link)
---> 65         for link in dict.__getitem__(self, 'links')
     66         if link['rel'] == 'enclosure'
     67     ]
     68 elif key == 'license':

KeyError: 'links'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[20], line 1
----> 1 d.entries[0].enclosures

File /usr/local/lib/python3.10/site-packages/feedparser/util.py:158, in FeedParserDict.__getattr__(self, key)
    156     return self.__getitem__(key)
    157 except KeyError:
--> 158     raise AttributeError("object has no attribute '%s'" % key)

AttributeError: object has no attribute 'enclosures'
@kurtmckee
Copy link
Owner

I agree that this should not crash. I'll work to get this resolved.

Thanks for reporting this!

@dmoklaf
Copy link

dmoklaf commented Apr 7, 2024

Hi, any news on this one? This prevents some feeds from being parsed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants