akismet 24.5.0a1¶
A Python interface to the Akismet spam-filtering service.
Two Akismet API clients are available from this library:
akismet.SyncClient
is an Akismet API client which performs synchronous (blocking) HTTP requests to the Akismet web service.akismet.AsyncClient
is an Akismet API client which performs asynchronous (async
/await
/non-blocking) HTTP requests to the Akismet web service.
Aside from one being sync and the other async, the two clients expose identical APIs, and implement all methods of the Akismet web API, including the v1.2 key and API usage metrics.
To use this library, you will need to obtain an Akismet API key and register a site for
use with the Akismet web service; you can do this at <https://akismet.com>. Once you
have a key and corresponding registered site URL to use with it, place them in the
environment variables PYTHON_AKISMET_API_KEY
and PYTHON_AKISMET_BLOG_URL
, and
they will be automatically detected and used.
You can then construct a client instance and call its methods. For example, to check a submitted forum post for spam:
import akismet
akismet_client = akismet.SyncClient.validated_client()
if akismet_client.comment_check(
user_ip=submitter_ip,
comment_content=submitted_content,
comment_type="forum-post",
comment_author=submitter_name
):
# This piece of content was classified as spam; handle it appropriately.
Or using the asynchronous client:
import akismet
akismet_client = await akismet.AsyncClient.validated_client()
if await akismet_client.comment_check(
user_ip=submitter_ip,
comment_content=submitted_content,
comment_type="forum-post",
comment_author=submitter_name
):
# This piece of content was classified as spam; handle it appropriately.
Note that in both cases the client instance is created via the alternate constructor
validated_client()
. This is recommended instead of using the default constructor
(i.e., directly calling akismet.SyncClient()
or akismet.AsyncClient()
); the
validated_client()
constructor will validate the configuration with the Akismet web
service before returning the client.
You can also use either client class as a context manager. This also will validate the configuration for you prior to returning the client:
import akismet
with akismet.SyncClient() as akismet_client:
if akismet_client.comment_check(
user_ip=submitter_ip,
comment_content=submitted_content,
comment_type="forum-post",
comment_author=submitter_name
):
# This piece of content was classified as spam; handle it appropriately.
Or using the asynchronous client:
import akismet
async with akismet.AsyncClient() as akismet_client:
if await akismet_client.comment_check(
user_ip=submitter_ip,
comment_content=submitted_content,
comment_type="forum-post",
comment_author=submitter_name
):
# This piece of content was classified as spam; handle it appropriately.
Documentation contents¶
See also
The developer documentation for the Akismet web service is useful for familiarizing yourself with the available API operations and features.