Login

Deli.cio.us rss template tag

Author:
aaloy
Posted:
April 11, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.