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