Akismet.py

Akismet is the hot new spam-detector by the same people who brought you WordPress. In fact, it’s so hot and new that the only real way I’ve seen so far to interact with it has been the official plugin (in PHP) for WordPress.

Since the first draft of the API documentation was released earlier today, I thought I’d write up a quick Python module that will talk to it.

It’s a very light wrapper around the API’s functionality. I didn’t want to do anything fancy with this, so I didn’t bother with classes or the like.

To use it, put akismet.py somewhere where Python will see it. The do something like this: (akismet_example.py)

—–

import akismet

akismet.USERAGENT = "MyApplication/MyVersion"

my_api_key = "SOMETHING"

try:
  real_key = akismet.verify_key(my_api_key,"http://www.example.com")
  if real_key:
    is_spam = akismet.comment_check(my_api_key,"http://www.example.com",
      "127.0.0.1", "Mozilla/5.0 (...) Gecko/20051111 Firefox/1.5",
        comment_content="VIAGRA! LOTS OF VIAGRA!")
      if is_spam:
        print "Yup, that's spam alright."
      else:
        print "Hooray, your users aren't scum!"
except akismet.AkismetError, e:
    print e.response, e.statuscode

# If you're a good person, you can report false positives via
# akismet.submit_ham(), and false negatives via akismet.submit_spam(),
# using exactly the same parameters as akismet.comment_check().

—–

I’m not 100% sure of the pure Pythonic nature of the way I’m handling arguments… but it does seem to work.

Download: Akismet.pyAkismet_example.py

Note: To use it you’ll need a valid WordPress.com API key. Which is easy to get — you just just have to join WordPress.com, and you’ll find it in your admin interface, in the “Users” section.

Update: Fuzzyman has written another Python module to access Akismet, taking a more class-oriented approach than mine. So if you hate the minimalist implementation here… there is an alternative! (Okay, it’s not really minimalist — anything which defines Exceptions can’t be called minimalist, to my mind.)