phenny-1/modules/rule34.py

45 lines
1.2 KiB
Python
Raw Normal View History

2012-02-12 01:01:34 -05:00
#!/usr/bin/python3
"""
rule34.py - urban dictionary module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
from urllib.parse import quote as urlquote
from urllib.error import HTTPError
import web
import lxml.html
def rule34(phenny, input):
""".rule34 <query> - Rule 34: If it exists there is porn of it."""
q = input.group(2)
if not q:
2012-05-14 16:49:41 -04:00
phenny.say(rule34.__doc__.strip())
2012-02-12 01:01:34 -05:00
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/')
2012-02-12 01:06:32 -05:00
thumbs = doc.find_class('thumb')
if len(thumbs) <= 0:
2012-02-12 01:03:39 -05:00
phenny.reply("You just broke Rule 34! Better start uploading...")
return
2012-02-12 01:01:34 -05:00
try:
2012-02-12 01:06:32 -05:00
link = thumbs[0].find('a').attrib['href']
2012-02-12 01:01:34 -05:00
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())