- Author:
- jeffwheeler
- Posted:
- March 15, 2007
- Language:
- Python
- Version:
- Pre .96
- Score:
- 6 (after 6 ratings)
This will save your del.icio.us bookmarks in your own database with a Bookmarks model. It depends on several things:
DELICIOUS_USER
andDELICIOUS_PASS
defined in settings.py- pydelicious installed
- Any view that uses del.icio.us should call the delicious() function, to update the database.
- The cache_function decorator must be available. (I have it in a decorators.py file in my app; if you move it, you need to update the import)
- The django-tagging app, although it could be modified to not need this rather easily.
Other than all those dependencies, it's actually rather simple. It will call del.icio.us for the 5 most recent posts, and create them if they're not already in the database. Otherwise, it'll check for updates, and change as appropriate. It won't return anything, as it updates the models themselves.
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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
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 :)
#
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'
#
@ubernostrum: When might we see your reversed implementation as mentioned above?
#
Please login first before commenting.