Created
August 17, 2011 15:28
-
-
Save myano/1151780 to your computer and use it in GitHub Desktop.
This script checks to see if a given phrase is profane according to Google's Profane API.
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
#!/usr/bin/env python | |
# vim: set fileencoding=UTF-8 : | |
""" | |
This script checks to see if a given phrase is profane according to Google's | |
Profane API. This script returns a 'True' if it's profane, a 'False' if it's | |
not profane and a 0 if there is an error. | |
Copyright 2011 Michael Yanovich | |
Licensed under the GNU General Public License version 3. | |
""" | |
import urllib | |
site = "http://www.wdyl.com/profanity?q=" | |
## http://www.w3schools.com/tags/ref_urlencode.asp | |
HTML_ENCODINGS = { | |
" ": "%20", | |
"!": "%21", | |
'"': "%22", | |
"#": "%23", | |
"$": "%24", | |
"%": "%25", | |
"&": "%26", | |
"'": "%27", | |
"(": "%28", | |
")": "%29", | |
"*": "%2A", | |
"+": "%2B", | |
",": "%2C", | |
"-": "%2D", | |
".": "%2E", | |
"/": "%2F", | |
":": "%3A", | |
";": "%3B", | |
"<": "%3C", | |
"=": "%3D", | |
">": "%3E", | |
"?": "%3F", | |
"@": "%40", | |
"[": "%5B", | |
"\\": "%5C", | |
"]": "%5D", | |
"^": "%5E", | |
"_": "%5F", | |
"`": "%60", | |
"{": "%7B", | |
"|": "%7C", | |
"}": "%7D", | |
"~": "%7E", | |
"€": "%80", | |
"‚": "%82", | |
"ƒ": "%83", | |
"„": "%84", | |
"…": "%85", | |
"†": "%86", | |
"‡": "%87", | |
"ˆ": "%88", | |
"‰": "%89", | |
"Š": "%8A", | |
"‹": "%8B", | |
"Œ": "%8C", | |
"Ž": "%8E", | |
"‘": "%91", | |
"’": "%92", | |
"“": "%93", | |
"”": "%94", | |
"•": "%95", | |
"–": "%96", | |
"—": "%97", | |
"˜": "%98", | |
"™": "%99", | |
"š": "%9A", | |
"›": "%9B", | |
"œ": "%9C", | |
"ž": "%9E", | |
"Ÿ": "%9F", | |
"¡": "%A1", | |
"¢": "%A2", | |
"£": "%A3", | |
"¥": "%A5", | |
"|": "%A6", | |
"§": "%A7", | |
"¨": "%A8", | |
"©": "%A9", | |
"ª": "%AA", | |
"«": "%AB", | |
"¬": "%AC", | |
"¯": "%AD", | |
"®": "%AE", | |
"¯": "%AF", | |
"°": "%B0", | |
"±": "%B1", | |
"²": "%B2", | |
"³": "%B3", | |
"´": "%B4", | |
"µ": "%B5", | |
"¶": "%B6", | |
"·": "%B7", | |
"¸": "%B8", | |
"¹": "%B9", | |
"º": "%BA", | |
"»": "%BB", | |
"¼": "%BC", | |
"½": "%BD", | |
"¾": "%BE", | |
"¿": "%BF", | |
"À": "%C0", | |
"Á": "%C1", | |
"Â": "%C2", | |
"Ã": "%C3", | |
"Ä": "%C4", | |
"Å": "%C5", | |
"Æ": "%C6", | |
"Ç": "%C7", | |
"È": "%C8", | |
"É": "%C9", | |
"Ê": "%CA", | |
"Ë": "%CB", | |
"Ì": "%CC", | |
"Í": "%CD", | |
"Î": "%CE", | |
"Ï": "%CF", | |
"Ð": "%D0", | |
"Ñ": "%D1", | |
"Ò": "%D2", | |
"Ó": "%D3", | |
"Ô": "%D4", | |
"Õ": "%D5", | |
"Ö": "%D6", | |
"Ø": "%D8", | |
"Ù": "%D9", | |
"Ú": "%DA", | |
"Û": "%DB", | |
"Ü": "%DC", | |
"Ý": "%DD", | |
"Þ": "%DE", | |
"ß": "%DF", | |
"à": "%E0", | |
"á": "%E1", | |
"â": "%E2", | |
"ã": "%E3", | |
"ä": "%E4", | |
"å": "%E5", | |
"æ": "%E6", | |
"ç": "%E7", | |
"è": "%E8", | |
"é": "%E9", | |
"ê": "%EA", | |
"ë": "%EB", | |
"ì": "%EC", | |
"í": "%ED", | |
"î": "%EE", | |
"ï": "%EF", | |
"ð": "%F0", | |
"ñ": "%F1", | |
"ò": "%F2", | |
"ó": "%F3", | |
"ô": "%F4", | |
"õ": "%F5", | |
"ö": "%F6", | |
"÷": "%F7", | |
"ø": "%F8", | |
"ù": "%F9", | |
"ú": "%FA", | |
"û": "%FB", | |
"ü": "%FC", | |
"ý": "%FD", | |
"þ": "%FE", | |
"ÿ": "%FF", | |
} | |
class Web: | |
class Grab(urllib.URLopener): | |
def __init__(self, *args): | |
self.version = 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) \ | |
Gecko/20100101 Firefox/5.0' | |
urllib.URLopener.__init__(self, *args) | |
urllib._urlopener = Grab() | |
def get(self, url): | |
u = urllib.urlopen(url) | |
page = u.read() | |
u.close() | |
return page | |
web = Web() | |
def profane(phrase): | |
""" | |
Checks to see if a given phrase is profane or not. | |
Returns True if it's profane, False if not profane, | |
and a 0 if there is an error | |
""" | |
for char in phrase: | |
if char in HTML_ENCODINGS: | |
phrase = phrase.replace(char, HTML_ENCODINGS[char]) | |
url = site + phrase | |
a = web.get(url) | |
if "true" in a: | |
return True | |
elif "false" in a: | |
return False | |
else: | |
return 0 | |
def main(): | |
incoming = raw_input("Please enter your input: ") | |
print profane(incoming) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment