Login

Akismet Webservice

Author:
sneeu
Posted:
March 14, 2007
Language:
Python
Version:
Pre .96
Tags:
rest python akismet spam webservice comment
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='sneeu@example.com', 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. Clean spam from comments by running management command and akismet by iElectric 2 years, 6 months ago
  2. Using Akismet/TypePad AntiSpam with Django's new comments framework by sciyoshi 6 years, 8 months ago
  3. Prevent Django newcomments spam with Akismet (reloaded) by sciyoshi 5 years, 9 months ago
  4. Django Akismet by yeago 6 years, 4 months ago
  5. Unobtrusive comment moderation by ubernostrum 8 years, 1 month ago

Comments

Please login first before commenting.