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

30 lines
869 B
Python
Raw Normal View History

from util import hook, http, text
import re
2011-11-20 13:07:02 +01:00
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):
2013-09-04 12:30:04 +02:00
"""drama <phrase> -- Gets the first paragraph of
the Encyclopedia Dramatica article on <phrase>."""
2011-11-20 13:07:02 +01:00
j = http.get_json(api_url, search=inp)
2011-11-20 13:07:02 +01:00
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)
2013-09-04 10:30:26 +02:00
for p in page.xpath('//div[@id="bodyContent"]/p'):
2011-11-20 13:07:02 +01:00
if p.text_content():
2012-04-18 06:35:57 +02:00
summary = " ".join(p.text_content().splitlines())
summary = re.sub("\[\d+\]", "", summary)
2013-09-04 10:30:26 +02:00
summary = text.truncate_str(summary, 220)
2013-09-05 03:46:49 +02:00
return "{} :: {}".format(summary, url)
2011-11-20 13:07:02 +01:00
2012-04-18 06:35:57 +02:00
return "Unknown Error."