Cached Del.icio.us API Calls

 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
###
# API Call
###

from datetime import datetime
import pydelicious
from time import strptime

from YOURPROJECT.blog.decorators import cache_function
from YOURPROJECT.blog.models import Bookmark
from YOURPROJECT import settings

@cache_function(60*10)
def delicious():
    """Call the del.icio.us API, and cache the call for ten (10) minutes."""
    api = pydelicious.apiNew(settings.DELICIOUS_USER, settings.DELICIOUS_PASS)
    recent_posts = api.posts_recent(count='5') # Count must be a string
    
    for post in recent_posts:
        # Check if the post already exists, based on the hash
        post['dt'] = datetime(*strptime(post['dt'], '%Y-%m-%dT%H:%M:%SZ')[0:6])
        post_hash = post['hash']
        
        valid_params = ['description', 'extended', 'href', 'dt', 'tags']
        post = dict([(k,v) for k,v in post.items() if k in valid_params])
        
        post_object = Bookmark.objects.get_or_create(address_hash=post_hash,
            defaults=post)
        
        if not post_object[1]: # If it wasn't just created
            for param in valid_params:
                if post_object[0].__getattribute__(param) != post[param]:
                    post_object[0].__setattr__(param, post[param])
            post_object[0].save()

###
# Bookmarks model
###

class Bookmark(models.Model):
    """
    A del.icio.us bookmark. This will be automatically created based on
    calls to the del.icio.us API.
    """
    address_hash = models.CharField(blank=False, maxlength=32)
    description = models.CharField(blank=False, maxlength=255)
    extended = models.TextField(blank=True)
    href = models.URLField()
    dt = models.DateTimeField()
    
    tags = models.CharField(maxlength=250, validator_list=[isTagList],
        help_text='Seperate tags with spaces.')
    
    class Admin:
        pass
    
    def title(self):
        "Mimics the title field in other models."
        return self.description
    
    def __str__(self):
        return self.title()
    
    def save(self):
        super(Bookmark, self).save()
        Tag.objects.update_tags(self, self.tags)

More like this

  1. Cache Any Function by jeffwheeler 6 years, 2 months ago
  2. Delicious Tag by alcides 4 years, 11 months ago
  3. Deli.cio.us rss template tag by aaloy 4 years, 1 month ago
  4. Google Contacts API friend finder by dgouldin 5 years, 1 month ago
  5. Update only selected model fields by jcrocholl 5 years, 6 months ago

Comments

ubernostrum (on March 15, 2007):

Nice.

I've been doing the reverse on my blog: I post to my own admin (mostly because I want a couple extra things, like a "via" field to store the site I spotted the link on) and then use pydelicious to pass the link on to del.icio.us. I'll get that code cleaned up and post it so we can have examples of how to do it either way :)

#

ThaBergMan (on October 6, 2007):

I tried to use this snippet this week with the latest pydelicious, but there seem to be some changes that caused quite a few headaches.

First:

Line 19, for post in recent_posts: had to read:

for post in recent_posts['posts']:

Also, some of the fields seem to be different. Changes had to be made in the model and the view from 'dt' to 'time' and from 'tags' to 'tag'

#

benspaulding (on June 5, 2008):

@ubernostrum: When might we see your reversed implementation as mentioned above?

#

(Forgotten your password?)