There are other articles written about this, but I felt the need to write a better one. Ahem.

Meat and potatoes time.

In your settings.py file, set 2 new variables:


# 'PING' blog indexing sites.
PING_BLOG_INDEX = True

# List of blog index ping URL's
BLOG_INDEX = ['http://rpc.technorati.com/rpc/ping',
'http://blogsearch.google.com/ping/RPC2',
'http://rpc.weblogs.com/RPC2']

PING_BLOG_INDEX is self explanatory. BLOG_INDEX is a list of blog XMLRPC url's that this application is going to use to notify the remote website (blog indexer) that your blog has been updated.

Let's create a new file in your blog application directory and name it ping.py. This file will hold the code that actually pings the blog indexers. Here it is:


from django.conf import settings

def pingSites(entry, blog_name):
for site in settings.BLOG_INDEX:
try:
rpc = xmlrpclib.Server(site)
try:
p = rpc.weblogUpdates.extendedPing(blog_name,
settings.SITE_URL,
entry.get_absolute_url(),
settings.SITE_URL + '/feeds/rss2'
)
except:
# May not support extendedPing()
# Try normal ping
p = rpc.weblogUpdates.ping(blog_name,
settings.SITE_URL)

if p.has_key('flerror') and p['flerror'] == True:
errlog(p['message'])
except:
errlog('pingSites: %s, exception!' % (site))

A few notes on the above code:


  1. errlog() is a function I have that just logs errors via syslog for my review. If you don't want to track the errors then a simple "pass" will do. I am just anal about errors and like to follow them. Heh, I said anal.

  2. This code assumes 2 things. One is that you have a "SITE_URL" option in your settings.py file. It should just be something like "SITE_URL = 'http://www.petersanchez.com'". And two is that your "Entry" model (the model that stores your blog posts) has a "get_absolute_url()" method. If it currently doesn't, I have to ask: What's wrong with you? Add one!

  3. You probably need to change the '/feeds/rss2' line to match the URL for your own RSS feed. Don't have an RSS feed on your blog yet? Write one, its super simple. Doc's are here.

Now lets edit your blogs models.py file be sure to import the pingSites() function that we just created in ping.py.


from your_project.blog.ping import pingSites

In your "Entry" model (mine is named "Entry", your mileage may vary) create a custom save() function.


def save(self):
# Save first, ping second (if configured)
super(Entry, self).save()
if settings.PING_BLOG_INDEX:
blog = Blog.objects.all()[0]
pingSites(self, blog.name)

Notes on above code:


  1. The 'blog' variable used in my example is because the software I wrote supports multiple blogs from a single installation. If you don't have a similar setup, just remove the "blog = Blog...." line and replace "blog.name" with the name of your blog. For example: pingSites(self, 'Joe Blow Blog')

That's it. You should be good to go. Next time you update your blog the blogosphere will immediately know about it via the blog indexers.

Technorati Tags: , , , ,

If you like this post then please consider subscribing to my full RSS feed.

Trackbacks/Pings (Trackback URL)

Comments  

I took the following approach to doing this: http://www.imalm.com/blog/2007/feb/10/using-django-signals-ping-sites-update/

Jon on July 17, 2007 at 6:43 p.m.

@ Jon - Nice article. Very clever way to get it done!

Peter on July 20, 2007 at 8:52 a.m.

Do you really have to query blog objects like

blog = Blog.objects.all()[0]

in safe method. Isn't enough to saj just

pingSites(self, self.name)

skrat on August 25, 2007 at 1:42 p.m.

@ Skrat

Well name isn't the name, or title, of the current blog entry. It is the name of the Blog overall. As the software supports multiple Blogs from a single installation, I had to get the first one. So unfortunately it is needed.

Peter on September 4, 2007 at 3:44 p.m.

Post a Comment

Verification Image

Subscribe


Subscribe to Full RSS Feed

Name:
Email:
Hey, I hate spam as much as you! I will never share your info with anyone!

WTF?

Peter Chillin'

Hey there. My name is Peter. I am a father, a husband, a geek, a horrible poker player and a bit of a degenerate. And I'm OK with that. More...

pjs[at]petersanchez[dot]com

flickr - groovr - del.icio.us - PGP

Search

 

The Latest

Topics

BlogRush