Login

Akismet Webservice

Author:
sneeu
Posted:
March 14, 2007
Language:
Python
Version:
Pre .96
Score:
6 (after 6 ratings)

A short little bit of code to test for comment spam against Akimet.

Use: a = Akismet('<AkismetKey>', 'http://sneeu.com/blog/') a.verify_key() print a.comment_check( comment_author='...', comment_author_email='[email protected]', user_ip='10.0.0.1', comment_content="""Comment content!""" )

 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
import urllib, urllib2


AKISMET_URL = 'rest.akismet.com/1.1'
USER_AGENT = 'askimet.py/0.0'


class Akismet(object):
    def __init__(self, api_key, blog):
        self.api_key = api_key
        self.blog = blog
        self.service = 'http://%s.%s' % (self.api_key, AKISMET_URL)

    def verify_key(self):
        return urllib2.urlopen(
            urllib2.Request(
                'http://%s/verify-key' % AKISMET_URL,
                urllib.urlencode({'key': self.api_key, 'blog': self.blog}),
                {'User-Agent': USER_AGENT}
            )
        ).read() == 'valid'

    def _get_url_data(self, method, comment_data):
        comment = {'blog': self.blog, 'comment_type': 'comment'}
        comment.update(comment_data)

        data = urllib2.urlopen(
            urllib2.Request(
                '%s/%s' % (self.service, method),
                urllib.urlencode(comment),
                {'User-Agent': USER_AGENT}
            )
        ).read()
        if data not in ('true', 'false'):
            raise Exception(data)
        return data == 'true'

    def __getattr__(self, name):
        def f(**kwargs):
            return self._get_url_data(name.replace('_', '-'), kwargs)
        return f

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.