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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/python
__version__ = "0.3"
__date__ = "2005-12-01"
__author__ = "David Lynch (kemayo AT Google's mail service DOT com)"
__copyright__ = "Copyright 2005, David Lynch"
__license__ = "New BSD"
__history__ = """
0.3 - 20051205 - Cleaned up __post.
0.2 - 20051201 - Added documentation, and tweaked the circumstances where an error
will be thrown.
0.1 - 20051201 - Initial release. Everything pretty much works. Probably.
"""
import http.client
from urllib.parse import urlencode
USERAGENT = ""
AKISMET_URL = "rest.akismet.com"
AKISMET_PORT = 80
class AkismetError(Exception):
def __init__(self, response, statuscode):
self.response = response
self.statuscode = statuscode
def __str__(self):
return repr(self.value)
def __post(request, host, path, port=80):
connection = http.client.HTTPConnection(host, port)
connection.request("POST", path, request, {
"User-Agent": "%s | %s/%s" % (USERAGENT, "Akistmet.py", __version__),
"Content-type": "application/x-www-form-urlencoded"
})
response = connection.getresponse()
return response.read(), response.status
def verify_key(key, blog):
"""Find out whether a given WordPress.com API key is valid.
Required parameters:
key: A WordPress.com API key.
blog: URL of the front page of the site comments will be submitted to.
Returns True if a valid key, False if invalid.
"""
response, status = __post("key=%s&blog=%s" % (key, blog), AKISMET_URL, "/1.1/verify-key", AKISMET_PORT)
if response == "valid":
return True
elif response == "invalid":
return False
else:
raise AkismetError(response, status)
def comment_check(key, blog, user_ip, user_agent, **other):
"""Submit a comment to find out whether Akismet thinks that it's spam.
Required parameters:
key: A valid WordPress.com API key, as tested with verify_key().
blog: URL of the front page of the site the comment will appear on.
user_ip: IP address of the being which submitted the comment.
user_agent: User agent reported by said being.
Suggested "other" keys: "permalink", "referrer", "comment_type", "comment_author",
"comment_author_email", "comment_author_url", "comment_content", and any other HTTP
headers sent from the client.
More detail on what should be submitted is available at:
http://akismet.com/development/api/
Returns True if spam, False if ham. Throws an AkismetError if the server says
anything unexpected.
"""
request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
request.update(other)
response, status = __post(urlencode(request), "%s.%s" % (key, AKISMET_URL), "/1.1/comment-check", AKISMET_PORT)
if response == "true":
return True
elif response == "false":
return False
else:
raise AkismetError(response, status)
def submit_spam(key, blog, user_ip, user_agent, **other):
"""Report a false negative to Akismet.
Same arguments as comment_check.
Doesn't return anything. Throws an AkismetError if the server says anything.
"""
request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
request.update(other)
response, status = __post(urlencode(request), "%s.%s" % (key, AKISMET_URL), "/1.1/submit-spam", AKISMET_PORT)
if status != 200 or response != "":
raise AkismetError(response, status)
def submit_ham(key, blog, user_ip, user_agent, **other):
"""Report a false positive to Akismet.
Same arguments as comment_check.
Doesn't return anything. Throws an AkismetError if the server says anything.
"""
request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
request.update(other)
response, status = __post(urlencode(request), "%s.%s" % (key, AKISMET_URL), "/1.1/submit-ham", AKISMET_PORT)
if status != 200 or response != "":
raise AkismetError(response, status)
|