Skip to content

Instantly share code, notes, and snippets.

@temnoregg
Created January 11, 2012 12:44
Show Gist options
  • Save temnoregg/1594490 to your computer and use it in GitHub Desktop.
Save temnoregg/1594490 to your computer and use it in GitHub Desktop.
Django spam filter moderator with StopForumSpam service check
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