This repository has been archived on 2023-04-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
CloudBot/disabled_stuff/drama.py

32 lines
871 B
Python
Raw Permalink Normal View History

import re
2011-11-21 01:07:02 +13:00
2014-02-14 16:36:57 +13:00
from util import hook, http, text
2012-04-18 16:35:57 +12:00
api_url = "http://encyclopediadramatica.se/api.php?action=opensearch"
ed_url = "http://encyclopediadramatica.se/"
2011-11-21 01:07:02 +13:00
@hook.command
def drama(inp):
2013-09-04 18:30:04 +08:00
"""drama <phrase> -- Gets the first paragraph of
the Encyclopedia Dramatica article on <phrase>."""
2011-11-21 01:07:02 +13:00
j = http.get_json(api_url, search=inp)
2011-11-21 01:07:02 +13:00
if not j[1]:
2012-04-18 16:35:57 +12:00
return "No results found."
2011-11-21 01:07:02 +13:00
article_name = j[1][0].replace(' ', '_').encode('utf8')
url = ed_url + http.quote(article_name, '')
page = http.get_html(url)
2013-09-04 20:30:26 +12:00
for p in page.xpath('//div[@id="bodyContent"]/p'):
2011-11-21 01:07:02 +13:00
if p.text_content():
2012-04-18 16:35:57 +12:00
summary = " ".join(p.text_content().splitlines())
summary = re.sub("\[\d+\]", "", summary)
2013-09-04 20:30:26 +12:00
summary = text.truncate_str(summary, 220)
2013-09-05 09:46:49 +08:00
return "{} :: {}".format(summary, url)
2011-11-21 01:07:02 +13:00
2012-04-18 16:35:57 +12:00
return "Unknown Error."