Login

Deli.cio.us rss template tag

Author:
aaloy
Posted:
April 11, 2009
Language:
Python
Version:
1.0
Tags:
delicious
Score:
0 (after 0 ratings)

This snipped get the latest rss links from delicious from a DELICIOUS_USER that you have to define in your settings.

I use this snippet in the trespams code blog to inform the visitor about the last links I have bookmarked.

This is a modified version of snipped 819 to allow using any variable in the template to obtain the results.

It also added a 6 hours caché for the rss.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: UTF-8 -*-

from django import template
from django.conf import settings
from django.views.decorators.cache import cache_page

register = template.Library()

@register.tag(name="get_delicious_links")
def do_delicious_links(parser, token):
    """
    {% get_delicious_lings as links%}

    Get the last del.icio.us entries for the configured user in
    DELICIOUS_USER settings.

    Caches the result for 6 hours.
    Delicious tags is based on http://www.djangosnippets.org/snippets/819/
    """
    bits = token.contents.split()
    if len(bits) == 3 and bits[1] == 'as':
        return DeliciousObject(bits[2])
    else:
        return template.TemplateSyntaxError, "Invalid sytax: use get_delicious_links as variable_name"
do_delicious_links = cache_page(do_delicious_links, 21600)

class DeliciousObject(template.Node):
    def __init__(self, context_variable):
        self.context_variable = context_variable

    def render(self, context):
        try:
            import feedparser
            d = feedparser.parse('http://del.icio.us/rss/%s'% settings.DELICIOUS_USER)
            delicious=[]
            for entry in d['entries'][:min(8,len(d['entries']))]:
                title=str(entry['title'].encode('ascii', 'xmlcharrefreplace'))
                delicious.append({'title':title,'link':str(entry['links'][0]['href'])})
        except:
            delicious=[]
        context[self.context_variable] = delicious
        return ""

More like this

  1. Cache Any Function by jeffwheeler 8 years, 5 months ago
  2. SnippySnip by youell 7 years, 1 month ago
  3. HTTP (basic) auth enabled (new-style) syndication framework feed class by hupf 4 years, 8 months ago
  4. Delicious Tag by alcides 7 years, 2 months ago
  5. Class Feeds DRY TemplateTag by gmandx 5 years, 3 months ago

Comments

Please login first before commenting.