Created
January 11, 2012 12:44
-
-
Save temnoregg/1594490 to your computer and use it in GitHub Desktop.
Django spam filter moderator with StopForumSpam service check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2 | |
import xml.dom import minidom | |
from django.contrib.comments.moderation import CommentModerator | |
SFS_API_URL = 'http://www.stopforumspam.com/api?' | |
def is_spam(ip, email): | |
""" | |
Check if IP and email is listed at StopForumSpam blacklist | |
""" | |
ip_blacklist = email_blacklist = False | |
data = urllib.urlencode({ 'ip': ip, 'email': email }) | |
try: | |
response = urllib2.urlopen(SFS_API_URL + data) | |
except urllib2.HTTPError as e: | |
response = False | |
if response: | |
dom = minidom.parse(response) | |
ip_blacklist = dom.getElementsByTagName('appears')[0].childNodes[0].data == 'yes' | |
email_blacklist = dom.getElementsByTagName('appears')[1].childNodes[0].data == 'yes' | |
return ip_blacklist or email_blacklist | |
class SFSModerator(CommentModerator): | |
def moderate(self, comment, content_object, request): | |
"""Moderate (change is_public) if ip or e-mail listed at StopForumSpam""" | |
return is_spam(comment.ip_address, comment.user_email) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment