Personal tools

Akismet を Perl から使う

written by ultraviolet on

Python Akismet Library is already done. So I wrote a quick and dirty perl function to call Akismet comment-check API.

use LWP::UserAgent;
use URI::Escape;

sub akismet ($$) {
    my ($apikey,$query) = @_;
    my $ua = new LWP::UserAgent;
    $ua->agent('Perl 5.8.7 | Net::Akismet/0.1');
    my $request = HTTP::Request->new('POST',
	'http://' . $apikey . '.rest.akismet.com/1.1/comment-check');
    my $query_string = '';
    my $key;
    foreach $key (keys(%$query)){
	$query_string .= $key . '=' . uri_escape($query->{$key}) . '&';
    }
    $request->content_type('application/x-www-form-urlencoded; charset=UTF-8');
    $request->content($query_string);
    my $response = $ua->request($request);
    return $response->content();
}

You can call it as follow.

my $q = {blog=>'あなたのblogのトップページのURI',
	 user_ip=>'投稿者のIPアドレス',
	 user_agent=>'投稿者の user agent',
	 referrer=>'投稿者の referrer',
	 permalink=>'投稿先の permalink',
	 comment_type=>'comment, trackback, pingback のいずれか',
	 comment_author=>'投稿者の名前',
	 comment_author_email=>'投稿者の mail address',
         comment_author_url=>'投稿者のURL',
	 comment_content=>'コメント本文',
     };
my $apikey = 'ここにあなたの API Key を書いてね';
if(akismet($apikey,$q) eq 'true'){
     # ham ですな
}else{
     # spam ですな
}

Other than comment-check, Akismet API includes verify key, submit-spam, and submit-ham interface.

Tags: , ,


One Response to “Akismet を Perl から使う”

  1. pingback from Rauru Blog » Blog Archive » Akismet.pm

    [...] Akismet を perl から使う話をもっと拡張して、API の四つの REST Interface を全部使えるように Net::Akismet クラスを作ってみたですよ。 [...]

Leave Your Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

« Back to text comment