How to make Universal Feed Parser only parse feeds?

Posted by piquadrat on Stack Overflow See other posts from Stack Overflow or by piquadrat
Published on 2010-12-25T15:49:58Z Indexed on 2010/12/25 15:54 UTC
Read the original article Hit count: 290

Filed under:
|

I'm trying to get content from external feeds on my Django web site with Universal Feed Parser. I want to have some user error handling, e.g. if the user supplies a URL that is not a feed. When I tried how feedparser responds to faulty input, I was surprised to see that feedparser does not throw any Exceptions at all. E.g. on HTML content, it tries to parse some information from the HTML code, and on non-existing domains, it returns a mostly empty dictionary:

{'bozo': 1,
'bozo_exception': URLError(gaierror(-2, 'Name or service not known'),),
'encoding': 'utf-8',
'entries': [],
'feed': {},
'version': None}

Other faulty input manifest themselves in the status_code or the namespaces values in the returned dictionary.

So, what's the best approach to have sane error checking without resorting to an endless cascade of if .. elif .. elif ...?

© Stack Overflow or respective owner

Related posts about python

Related posts about feedparser