list index out or range,getting rss feed urls out of db

99 views
Skip to first unread message

sebey

unread,
May 28, 2008, 6:30:15 AM5/28/08
to Django users
from django.http import HttpResponse
import feedparser
from ubermicro.shows.models import show

def show_page(request):
"""this is where we take what we need form the rss feeds in the
data base"""
query = show.objects.filter(show_feed__contains="http://")
podcast = feedparser.parse(query)
#show_about = podcast.feed.description
show_latest_title = podcast.entries[0].title
#show_latest = podcast.entries[0].description


return HttpResponse(show_latest_title)

this code is what I think its doing is that I am grabing the rss url
then using feedparser (http://www.feedparser.org) to get rss element
such as <description> and such but every time I try to do this I get
this error

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/core/handlers/base.py" in get_response
77. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/sebey/Sites/ubermicro/../ubermicro/shows/views.py" in
show_page
10. show_latest_title = podcast.entries[0].title

IndexError at /shows/
list index out of range
I am to django programing web dev etc. but I guess that the query I am
useing is not correct so I what should I do thanks
Message has been deleted

Rajesh Dhawan

unread,
May 28, 2008, 10:20:32 AM5/28/08
to Django users


On May 28, 6:30 am, sebey <se...@mac.com> wrote:
> from django.http import HttpResponse
> import feedparser
> from ubermicro.shows.models import show
>
> def show_page(request):
>     """this is where we take what we need form the rss feeds in the
> data base"""
>     query = show.objects.filter(show_feed__contains="http://")
>     podcast = feedparser.parse(query)

It seems that you are making feedparser parse an instance of a Django
ORM query. I think what you want to do is to make it parse a URL. May
be something like this:

for q in query:
podcast = feedparser.parse(q.show_feed)
if podcast.entries:
show_latest_title = podcast.entries[0].title

This will do it for all objects in your query. So you will have to
collect that list of titles etc. in a collection of some kind (list,
dict, etc.) and pass it on to your template.

>     #show_about = podcast.feed.description
>     show_latest_title = podcast.entries[0].title

The above statement assumes that there is at least one entry in the
feed. That may not be always true. So, you should consider testing
that first. Something like:

if podcast.entries:
show_latest_title = podcast.entries[0].title

>     #show_latest = podcast.entries[0].description
>
>     return  HttpResponse(show_latest_title)
>
> this code is what I think its doing is that I am grabing the rss url
> then using feedparser (http://www.feedparser.org) to get rss element
> such as <description> and such but every time I try to do this I get
> this error
>
> Traceback (most recent call last):
> File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
> site-packages/django/core/handlers/base.py" in get_response
>   77. response = callback(request, *callback_args, **callback_kwargs)
> File "/Users/sebey/Sites/ubermicro/../ubermicro/shows/views.py" in
> show_page
>   10. show_latest_title = podcast.entries[0].title
>
>   IndexError at /shows/
>   list index out of range
> I am to django programing web dev etc. but I guess that the query I am
> useing is not correct so I what should I do thanks

-Rajesh

sebastian stephenson

unread,
May 28, 2008, 11:37:36 AM5/28/08
to django...@googlegroups.com
great finally some help thank you so much!

see ya

sebey


sebey

unread,
May 28, 2008, 11:45:05 AM5/28/08
to Django users
please much suggestions thank you

Eric Abrahamsen

unread,
May 28, 2008, 2:42:09 PM5/28/08
to django...@googlegroups.com
What I was originally suggesting you do (which of course might not
turn out to be the best solution) is use feedparser to read feed urls,
and save those feeds into your database. That way, you wouldn't be
using feedparser at all in your views, just regular database queries.

If your models include Feed and FeedItem (FeedItem having a foreignkey
to a Feed), then feed parser will do something like this:

f = feedparser.parse('http://somefeed.url')
feed = Feed.objects.get(link=f.feed.link) # f.feed provides
information corresponding to one Feed instance
for item in f['entries']: # entries are all the items under a
particular feed
it, created = FeedItem.objects.get_or_create(title=item.title,
link=item.link, desc=item.description, date=item.date_parsed,
id=item.id, feed=feed)

Repeat the above for every url in your list of feed urls. Note: this
code goes in your standalone script running on a cron job, NOT in your
views or whatever other normal part of your django code base. That
will put RSS feed items into your database, your views.py pulls them
out again as per normal django methods:

feed = Feed.objects.get(link=somelinkvalue)
items = feed.feeditem_set.all()
return render_to_response(template, {'items':items})

That's how it ought it work, in my opinion. Note that it's late here,
and I've been drinking, so there may be some inaccuracies in the
above. I concur with the general consensus, that you need to start
with some simple CGI scripts and python basics, and move up from
there. That said, good luck!

E

sebastian stephenson

unread,
May 29, 2008, 5:08:31 AM5/29/08
to django...@googlegroups.com
well umm. can I add urls though the admin interface?

see ya

sebey


Eric Abrahamsen

unread,
May 29, 2008, 5:29:10 AM5/29/08
to django...@googlegroups.com
Sure. Add instances of your Feed class in the admin. Then in your
standalone script, the one that runs under your regular django
environment, do this:

from ubermicro.shows.models import Feed

feeds = Feed.objects.all()
for feed in feeds:
f = feedparser.parse(feed.link)


for item in f['entries']:

# continue as below


So you'll add Feeds in the admin, but your standalone script will add
FeedItems automatically; you'll probably just leave those alone in the
admin. Part of the standalone script should clear old FeedItems from
the database when you don't need them anymore.

E

sebastian stephenson

unread,
May 29, 2008, 7:38:59 AM5/29/08
to django...@googlegroups.com
great thats helpful thanks

see ya

sebey


Reply all
Reply to author
Forward
0 new messages