Exemple #1
0
def index(request):
    entries = []
    for r in In.objects.all():
        rss = r.url
        feed = feedparser.parse(rss)
        for i in feed['items']:
            i['summary'] = strip_tags(i['summary'])
            i['published_parsed'] = \
                datetime.fromtimestamp(mktime(i['published_parsed']))
        entries.extend(feed['items'])
        entries = sorted(entries, key=lambda entry: entry["published"])
        entries.reverse()
    for i in entries:
        for j in Tags.objects.all():
            if(i['summary'].lower().find(j.tag_ltrack) > -1 or i['title'].lower().find(j.tag_ltrack) > -1):
                if(Feed.objects.filter(title=i['title']).count() == 0):
                    #f = Feed(title=i['title'], link=i['link'], summary=i['summary'], published=datetime.fromtimestamp(mktime(i['published_parsed'])), tag=j.tag_name)
                    f = Feed(title=i['title'], link=i['link'], summary=i['summary'],
                             published=i['published_parsed'])
                    f.tags = j.tag_ltrack
                    f.save()
                    f.tags_track.add(j)
                    f.save()
                else:
                    f = Feed.objects.filter(title=i['title'])
                    for fe in f:
                        if(j not in fe.tags_track.all()):
                            fe.tags_track.add(j)
                            fe.tags = fe.tags + "; " + j.tag_ltrack
                            fe.save()

    return render_to_response("view/index.html", {'feed': entries},
                              context_instance=RequestContext(request))
Exemple #2
0
def add_feed(request):
    """
    Add feed view.
    Using add_feed.html temlate.

    Trying to get URL from post.
    If we couldn't - redirecting back to add_feed.html page.

    Trying to get Feed object with such URL.
    If we coult - redirecting to message page.

    Then calculate Feed's
        Title, URL, Time, Feed, User fields
    and create (& save) Feed object.

    Add entries.

    Redirects to /feeds.
    """

    __time_update(request.user)

    try:
        url = request.POST['url']
        if not url.startswith('http://'):
            url = 'http://' + url
    except KeyError:
        return render_to_response('add_feed.html',
            {'username': request.user.username})

    try:
        Feed.objects.get(url=url, user=request.user)
        return render_to_response('message.html', {'message':
            'There is already such feed',
            'back': '/feeds'})
    except Feed.DoesNotExist:
        pass

    feed = feedparser.parse(url)

    # If were errors loading XML
    try:
        # Title field in Feed
        title = feed.feed.title
    except AttributeError:
        # Display warning message
        return render_to_response('message.html', {'message':
            'Wrong feed URL or connection Error.',
            'back': '/add_feed'})

    # Time field in Feed
    time = datetime.now()

    # Create Feed and save it
    feed_obj = Feed(title=title, url=url, time=time,
        user=request.user)
    feed_obj.save()

    __add_entries(feed.entries, feed_obj)

    return redirect('/feeds')