ContributedMiddleware: obfuscateEmail.py

File obfuscateEmail.py, 657 bytes (added by scum, 9 years ago)

ObfuscateEmail converts all emails on the page to a format in code that spam bots have a hard time reading, yet looks normal to site visitors. Ex: name@… becomes name@domain.com in html.

Line 
1import re
2regx_extractEmail = re.compile('([0-9a-zA-Z][-.\w]*[0-9a-zA-Z]*\@[0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.+[a-zA-Z]{2,9})', re.DOTALL)
3
4class ObfuscateEmailMiddleware:
5        def obfuscateEmail(self, email):
6                newEmail = ""
7                x = 0
8                for letter in email.group(0):
9                        if x%2 == 0:
10                                newEmail = newEmail + "&#%s;" % ord(letter)
11                        else:
12                                newEmail += letter
13                        x += 1 
14                newEmail.replace("@", "@")
15                return newEmail
16       
17        def process_response(self, request, response):
18                if response.has_header and response.headers.get("Content-Type", "")[0:9] == "text/html":
19                        response.content = regx_extractEmail.sub(self.obfuscateEmail, response.content)
20                return response
Back to Top