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

28 lines
856 B
Python
Raw Normal View History

2011-11-20 13:07:02 +01:00
from util import hook, http
2012-04-18 06:35:57 +02:00
api_url = "http://encyclopediadramatica.se/api.php?action=opensearch"
ed_url = "http://encyclopediadramatica.se/"
2011-11-20 13:07:02 +01:00
@hook.command
def drama(inp):
2012-05-16 05:07:27 +02:00
"drama <phrase> -- Gets the first paragraph of" \
2012-04-18 06:35:57 +02:00
" the Encyclopedia Dramatica article on <phrase>."
2011-11-20 13:07:02 +01:00
j = http.get_json(api_url, search=inp)
if not j[1]:
2012-04-18 06:35:57 +02:00
return "No results found."
2011-11-20 13:07:02 +01:00
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():
2012-04-18 06:35:57 +02:00
summary = " ".join(p.text_content().splitlines())
2011-11-20 13:07:02 +01:00
if len(summary) > 300:
summary = summary[:summary.rfind(' ', 0, 300)] + "..."
2012-04-18 06:35:57 +02:00
return "%s :: \x02%s\x02" % (summary, url)
2011-11-20 13:07:02 +01:00
2012-04-18 06:35:57 +02:00
return "Unknown Error."