lolamontes69

akismet.py for Ch6 Programming Collective Intelligence

Jul 11th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. __version__ = "0.3"
  4. __date__ = "2005-12-01"
  5. __author__ = "David Lynch (kemayo AT Google's mail service DOT com)"
  6. __copyright__ = "Copyright 2005, David Lynch"
  7. __license__ = "New BSD"
  8. __history__ = """
  9. 0.3 - 20051205 - Cleaned up __post.
  10. 0.2 - 20051201 - Added documentation, and tweaked the circumstances where an error
  11.    will be thrown.
  12. 0.1 - 20051201 - Initial release.  Everything pretty much works.  Probably.
  13. """
  14.  
  15. import httplib
  16. from urllib import urlencode
  17.  
  18. USERAGENT = ""
  19. AKISMET_URL = "rest.akismet.com"
  20. AKISMET_PORT = 80
  21.  
  22. class AkismetError(Exception):
  23.     def __init__(self, response, statuscode):
  24.         self.response = response
  25.         self.statuscode = statuscode
  26.     def __str__(self):
  27.          return repr(self.value)
  28.  
  29. def __post(request, host, path, port = 80):
  30.     connection = httplib.HTTPConnection(host, port)
  31.     connection.request("POST", path, request,
  32.         {"User-Agent":"%s | %s/%s" % (USERAGENT,"Akistmet.py", __version__),
  33.         "Content-type":"application/x-www-form-urlencoded"})
  34.     response = connection.getresponse()
  35.    
  36.     return response.read(), response.status
  37.  
  38. def verify_key(key, blog):
  39.     """Find out whether a given WordPress.com API key is valid.
  40.    Required parameters:
  41.        key: A WordPress.com API key.
  42.        blog: URL of the front page of the site comments will be submitted to.
  43.    Returns True if a valid key, False if invalid.
  44.    """
  45.     response, status = __post("key=%s&blog=%s" % (key,blog), AKISMET_URL, "/1.1/verify-key", AKISMET_PORT)
  46.    
  47.     if response == "valid":
  48.         return True
  49.     elif response == "invalid":
  50.         return False
  51.     else:
  52.         raise AkismetError(response, status)
  53.  
  54. def comment_check(key, blog, user_ip, user_agent, **other):
  55.     """Submit a comment to find out whether Akismet thinks that it's spam.
  56.    Required parameters:
  57.        key: A valid WordPress.com API key, as tested with verify_key().
  58.        blog: URL of the front page of the site the comment will appear on.
  59.        user_ip: IP address of the being which submitted the comment.
  60.        user_agent: User agent reported by said being.
  61.    Suggested "other" keys: "permalink", "referrer", "comment_type", "comment_author",
  62.    "comment_author_email", "comment_author_url", "comment_content", and any other HTTP
  63.    headers sent from the client.
  64.    More detail on what should be submitted is available at:
  65.    http://akismet.com/development/api/
  66.    
  67.    Returns True if spam, False if ham.  Throws an AkismetError if the server says
  68.    anything unexpected.
  69.    """
  70.    
  71.     request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
  72.     request.update(other)
  73.     response, status = __post(urlencode(request), "%s.%s" % (key,AKISMET_URL), "/1.1/comment-check", AKISMET_PORT)
  74.    
  75.     if response == "true":
  76.         return True
  77.     elif response == "false":
  78.         return False
  79.     else:
  80.         raise AkismetError(response, status)
  81.  
  82. def submit_spam(key, blog, user_ip, user_agent, **other):
  83.     """Report a false negative to Akismet.
  84.    Same arguments as comment_check.
  85.    Doesn't return anything.  Throws an AkismetError if the server says anything.
  86.    """
  87.     request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
  88.     request.update(other)
  89.     response, status = __post(urlencode(request), "%s.%s" % (key,AKISMET_URL), "/1.1/submit-spam", AKISMET_PORT)
  90.     if status != 200 or response != "":
  91.         raise AkismetError(response, status)
  92.  
  93. def submit_ham(key, blog, user_ip, user_agent, **other):
  94.     """Report a false positive to Akismet.
  95.    Same arguments as comment_check.
  96.    Doesn't return anything.  Throws an AkismetError if the server says anything.
  97.    """
  98.     request = {'blog': blog, 'user_ip': user_ip, 'user_agent': user_agent}
  99.     request.update(other)
  100.     response, status = __post(urlencode(request), "%s.%s" % (key,AKISMET_URL), "/1.1/submit-ham", AKISMET_PORT)
  101.     if status != 200 or response != "":
  102.         raise AkismetError(response, status)
Advertisement
Add Comment
Please, Sign In to add comment