kurtmckee/feedparser

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

philgyford opened this issue · 2 comments

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'

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

Thanks for reporting this!