~widelands-dev/widelands-website/anti_spam

« back to all changes in this revision

Viewing changes to akismet-0.2.0/test/test_akismet.py

  • Committer: franku
  • Date: 2016-10-08 14:24:47 UTC
  • Revision ID: somal@arcor.de-20161008142447-30l69t80p35aamuk
removedĀ akismetĀ api

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# Version 0.1.2
3
 
# 2005/12/05
4
 
 
5
 
# Copyright Michael Foord 2005
6
 
# test_akismet.py
7
 
# Test CGI for akismet.py - the Python interface to the akismet API
8
 
 
9
 
# http://www.voidspace.org.uk/python/modules.shtml
10
 
# http://akismet.com
11
 
 
12
 
# Released subject to the BSD License
13
 
# Please see http://www.voidspace.org.uk/python/license.shtml
14
 
 
15
 
# For information about bugfixes, updates and support, please join the Pythonutils mailing list.
16
 
# http://groups.google.com/group/pythonutils/
17
 
# Comments, suggestions and bug reports welcome.
18
 
# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
19
 
# E-mail fuzzyman@voidspace.org.uk
20
 
 
21
 
"""
22
 
A simple test CGI for akismet.py
23
 
 
24
 
Requires cgiutils and a file called apikey.txt
25
 
"""
26
 
 
27
 
import cgi
28
 
import os
29
 
import sys
30
 
import cgitb
31
 
cgitb.enable()
32
 
 
33
 
sys.path.append('../modules')
34
 
sys.path.append('modules')
35
 
 
36
 
from cgiutils import *
37
 
import akismet
38
 
from akismet import Akismet
39
 
 
40
 
__version__ = '0.1.2'
41
 
 
42
 
DEBUG = False
43
 
 
44
 
valuelist = ['comment', 'comment_author_email', 'comment_author',
45
 
    'comment_author_url', ]
46
 
 
47
 
header = '''<html><head><title>Akismet Test CGI</title></head><body>
48
 
<center><h1>Testing the Python Interface to Akismet</h1>
49
 
By 
50
 
<em><a href="http://www.voidspace.org.uk/python/index.shtml">Fuzzyman</a></em>
51
 
<br><br>
52
 
<h2>Combatting Comment Spam</h2>
53
 
<h2>Akismet Test Version %s</h2>
54
 
<a href="http://www.voidspace.org.uk/python/modules.shtml#akismet">
55
 
<h2>Python API Version %s</h2></a>
56
 
''' % (__version__, akismet.__version__)
57
 
 
58
 
form = '''
59
 
**result**
60
 
<em>Posts With the Name set as </em><strong>viagra-test-123</strong>
61
 
<em>Should Always be Marked as Spam</em><br>
62
 
<strong>Enter a comment to test :</strong>
63
 
<form method="post" action="**scriptname**">
64
 
    <table>
65
 
        <tr><td align="right"><small><strong><label for="comment_author">Your Name:</label></strong></small></td>
66
 
            <td><input type="text" name="comment_author" value ="**comment_author**" ></td></tr>
67
 
        <tr><td align="right"><small><strong><label for="comment_author_email">Your E-Mail Address:</label></strong></small></td>
68
 
            <td><input type="text" name="comment_author_email" size="40" value="**comment_author_email**" ></td></tr>
69
 
 
70
 
        <tr><td align="right"><small><strong><label for="comment_author_url">Homepage:</label></strong></small></td>
71
 
            <td><input type="text" name="comment_author_url" value="**comment_author_url**" size="40"></td></tr>
72
 
    </table><br>
73
 
    <small><strong><label for="comment">Please make your comments below</label></strong></small><br>
74
 
    <textarea name="comment" cols="60" rows="6" wrap="hard">**comment**</textarea>
75
 
    <br><br>
76
 
    <input type="submit" value="Go For It"><input type="reset">
77
 
</form>
78
 
'''
79
 
 
80
 
footer = '</center></body></html>'
81
 
 
82
 
no_key = '''<h1>This script needs a %sWordpress API Key</h1>
83
 
<h2><a href="http://wordpress.com">Vist Wordpress</a></h2>
84
 
'''
85
 
 
86
 
res_line = '<h1>Akismet Says the Comment is %s</h1>'
87
 
 
88
 
def results(req):
89
 
    #
90
 
    # FIXME: could break here if apikey.txt exists, but has no key/blog_url
91
 
    api = Akismet()
92
 
    if api.key is None:
93
 
        # apikey.txt file
94
 
        return no_key % ''
95
 
    if not api.verify_key():
96
 
        # invalid key
97
 
        return no_key % 'Valid '
98
 
    # check the form - it contains some relevant data
99
 
    # the rest will be filled in with defaults
100
 
    for entry, val in os.environ.items():
101
 
        if entry.startswith('HTTP'):
102
 
            req[entry] = val
103
 
    result = api.comment_check(req['comment'], req, DEBUG=DEBUG)
104
 
    if DEBUG:
105
 
        return res_line % result
106
 
    elif result:
107
 
        return res_line % 'Spam'
108
 
    else:
109
 
        return res_line % 'Ham'
110
 
 
111
 
def main():
112
 
    # getrequest, serverline, cgiprint, replace - all come from cgituils
113
 
    req = getrequest(valuelist)
114
 
    cgiprint(serverline)
115
 
    cgiprint()
116
 
    print header
117
 
    #
118
 
    if req['comment'].strip():
119
 
        result = '<br><br>%s<br><br>' % results(req)
120
 
    else:
121
 
        req['comment_author'] = 'viagra-test-123'
122
 
        result = ''
123
 
    rep = {'**result**': result }
124
 
    for key, val in req.items():
125
 
        rep['**%s**' % key] = val.strip()
126
 
    rep['**scriptname**'] = os.environ['SCRIPT_NAME']
127
 
    print replace(form, rep)
128
 
    print footer
129
 
 
130
 
 
131
 
if __name__ == '__main__':
132
 
    if not 'SCRIPT_NAME' in os.environ:
133
 
        print 'This script must be run as a CGI'
134
 
    else:
135
 
        main()
136
 
 
137
 
"""
138
 
 
139
 
CHANGELOG
140
 
=========
141
 
 
142
 
2005/12/05      Version 0.1.2
143
 
-----------------------------
144
 
 
145
 
Added DEBUG mode
146
 
 
147
 
 
148
 
2005/12/04      Version 0.1.1
149
 
-----------------------------
150
 
 
151
 
Added the version numbers
152
 
 
153
 
Added default name 'viagra-test-123'
154
 
 
155
 
 
156
 
2005/12/02      Version 0.1.0
157
 
-----------------------------
158
 
 
159
 
A simple test script for akismet.py
160
 
 
161
 
It tests ``verify_key`` and ``comment_check`` in 80 lines of code
162
 
 
163
 
"""
 
 
b'\\ No newline at end of file'