From e787df18501af938577f9376620f6e29c2481f33 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Sun, 12 Feb 2012 01:01:34 -0500 Subject: [PATCH] add rule34 module --- modules/rule34.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 modules/rule34.py diff --git a/modules/rule34.py b/modules/rule34.py new file mode 100644 index 0000000..1260924 --- /dev/null +++ b/modules/rule34.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 +""" +rule34.py - urban dictionary module +author: mutantmonkey +""" + +from urllib.parse import quote as urlquote +from urllib.error import HTTPError +import web +import lxml.html + +def rule34(phenny, input): + """.rule34 - Rule 34: If it exists there is porn of it.""" + + q = input.group(2) + if not q: + phenny.say(".rule34 - Rule 34: If it exists there is porn of it.") + return + + try: + req = web.get("http://rule34.xxx/index.php?page=post&s=list&tags={0}".format(urlquote(q))) + except (HTTPError, IOError): + phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.") + return + + doc = lxml.html.fromstring(req) + doc.make_links_absolute('http://rule34.xxx/') + + try: + thumb = doc.find_class('thumb')[0] + link = thumb.find('a').attrib['href'] + except AttributeError: + phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.") + return + + response = '!!NSFW!! -> {0} <- !!NSFW!!'.format(link) + phenny.reply(response) +rule34.rule = (['rule34'], r'(.*)') + +if __name__ == '__main__': + print(__doc__.strip())