Re: Akismet PyBlosxom Plugin

Michael Olson <mwolson-mXXj517/[email protected]>
Newsgroups gmane.comp.web.pyblosxom.user
Message-ID <[email protected]>
"Benj. Mako Hill" <[email protected]> writes:

> I've built a new version of the Akismet plugin that incorporates
> both my work and Blake's. As Michael Olson's has suggested, I've
> also included a command-line program that can be run locally and
> that can parse and submit spam comments to the Akismet server.

Cool.  I've made an additional script based on that one that is able
to parse the emailed version of the comment, rather than the raw
comment.  I need this because logging into the server that hosts my
blog is rather tedious.  You're welcome to add this to the
akismet_comments package if you like.


-- 
Michael Olson -- FSF Associate Member #652 -- http://www.mwolson.org/
Interests: Emacs Lisp, text markup, protocols -- Jabber: mwolson_at_hcoop.net
  /` |\ | | | Projects: Emacs, Muse, ERC, EMMS, Planner, ErBot, DVC
 |_] | \| |_| Reclaim your digital rights by eliminating DRM.
      See http://www.defectivebydesign.org/what_is_drm for details.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
pyblosxom-users mailing list
pyblosxom-users-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-users
akismet_submit_email.py (text/x-python, 5.1 KB)
#!/usr/bin/env python 

# Copyright (C) 2006 Benjamin Mako Hill <[email protected]>
#                    Michael Olson <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

__author__      = "Benjamin Mako Hill"
__version__     = "0.2"
__url__         = "http://mako.cc/projects/pyblosxom"
__description__ = "Run comments through Akismet."

import sys
import os
import os.path
import re
From getopt import gnu_getopt

# insert necessary pyblosxom module directories and the config directory
# in thte path 
sys.path.insert(1, os.path.dirname(sys.path[0]))
From config import py as cfg
sys.path.pop(1)

# set some variables based on the configuration file
api_key = cfg['akismet_api_key']
base_url = cfg['base_url']

# import the akismet module
for path in cfg['plugin_dirs']:
    sys.path.insert(1, os.path.dirname(sys.path[0]) + "/" + path)
From akismet import Akismet

# initialize the akismet module
api = Akismet(api_key, base_url, agent='PyBlosxom/1.3')
if not api.verify_key():
    print >>sys.stderr, "Could not verify akismet API key.",
    sys.exit()

# define the main subroutine to be run once per file submitted
def submit_spam_comment(comment=""):
    fields = { 'Comment location' : 'comment',
               'Email' : 'comment_author_email',
               'Name' : 'comment_author',
               'Hostname' : 'user_ip',
               'URL' : 'comment_author_url' }

    data = {}
    for field in fields:
        match = re.search((r'(\n|^)%s: *(.*)\n' % field), comment)
        if match:
            undecoded_text = False;
            if field == 'Comment location':
                # comment is rest of email message
                undecoded_text = comment[match.end(2)+1:]
            elif field == 'Hostname':
                # hostname is in form "name (ip)"
                match = re.search(r'\(([^)]+)\)$', match.group(2))
                if match:
                    undecoded_text = match.group(1)
            else:
                undecoded_text = match.group(2)
            if not undecoded_text:
                continue
            # ensure that the text is decoded properly
            text = ""
            for char in list(undecoded_text):
                try:
                    char.encode('ascii')
                except:
                    text = text + "&#" + str(ord(char)) + ";"
                else:
                    text = text + char
            data[fields[field]] = text

    if not data.has_key('user_ip') or not data['user_ip']:
        return False
    if not data.has_key('comment') or not data['comment']:
        return False
    else:
        body = data['comment']

    # blank user_agent because it's required
    data['user_agent'] = ""

    global api
    try:
        api.submit_spam(body, data, False)
        return True
    except:
        print >>sys.stderr, "Comment *not* subumitted successfully!"
        raise


## run once per file submitted
##################################
# set default options
delete_files = False
from_stdin = False

# extract options
options, args = gnu_getopt(sys.argv[1:], 'd')
for option, argument in options:
    print option
    if option == "-d":
        delete_files = True

if len(args) == 0 and len(sys.argv) == 2 and sys.argv[1] == "-":
    # read from stdin exactly once
    args = ("0")
    from_stdin = True
if len(args) > 0 or from_stdin:
    for arg in args: 
        if from_stdin:
            comment = sys.stdin.read()
        elif not os.path.exists(arg):
            print "File does not exist: %s" % arg
            continue
        else:
            comment = open(arg, 'r').read()

        # submit the comment as spam
        rv = submit_spam_comment(comment)
        if not rv:
            if from_stdin:
                print "Could not parse input as a comment"
            else:
                print "Could not parse comment file: %s" % arg
            continue
        
        # delete the file if the option has been passed
        if from_stdin:
            print "Comment from stdin submitted as spam"
        elif delete_files:
            os.unlink(arg)
            print "Comment in file submitted as spam and deleted: %s" % arg
        else:
            print "Comment in file submitted as spam: %s" % arg
else:
    print >>sys.stderr, """USAGE:
  akismet_submit_email.py [-d] filename1 [filename2 ...]

You must enter at least one filename. Use \"-d\" to delete files after
submission. If filename is -, the comment will be read from stdin."""
signature.asc (application/pgp-signature, 188 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFFP//w+1Ho2POo0xkRAnohAJ4+7mnBlw/DtF3rZkDVufVYjdQYDQCgteEB
Mznhblws6bj606qzqUuMvdk=
=di5r
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.