This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/drama.py
2012-02-29 00:29:53 -08:00

28 lines
854 B
Python
Executable file

from util import hook, http
api_url = "http://encyclopediadramatica.ch/api.php?action=opensearch"
ed_url = "http://encyclopediadramatica.ch/"
@hook.command('ed')
@hook.command
def drama(inp):
".drama <phrase> -- Gets the first paragraph of Encyclopedia Dramatica article on <phrase>."
j = http.get_json(api_url, search=inp)
if not j[1]:
return 'no results found'
article_name = j[1][0].replace(' ', '_').encode('utf8')
url = ed_url + http.quote(article_name, '')
page = http.get_html(url)
for p in page.xpath('//div[@id="bodyContent"]/p'):
if p.text_content():
summary = ' '.join(p.text_content().splitlines())
if len(summary) > 300:
summary = summary[:summary.rfind(' ', 0, 300)] + "..."
return '%s :: \x02%s\x02' % (summary, url)
return "error"