From 19f3476c62818333a6f64f7ef06fc183f1230a03 Mon Sep 17 00:00:00 2001 From: Molly White Date: Thu, 23 Oct 2014 22:06:57 -0400 Subject: [PATCH] Add !bash command --- plugins/__init__.py | 1 + plugins/bash.py | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 plugins/bash.py diff --git a/plugins/__init__.py b/plugins/__init__.py index 5137b9b..163bf39 100644 --- a/plugins/__init__.py +++ b/plugins/__init__.py @@ -16,6 +16,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import plugins.actions +import plugins.bash import plugins.connection import plugins.eightball import plugins.freenode diff --git a/plugins/bash.py b/plugins/bash.py new file mode 100644 index 0000000..e369406 --- /dev/null +++ b/plugins/bash.py @@ -0,0 +1,79 @@ +# Copyright (c) 2013-2014 Molly White +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software +# and associated documentation files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all copies or +# substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING +# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +from plugins.util import command, get_url +from bs4 import BeautifulSoup + + +@command("b", "qdb") +def bash(m): + """Return a quote from bash.org.""" + if len(m.line) > 1 and m.line[1].isnumeric(): + bash_specific(m, m.line[1]) + else: + bash_rand(m) + + +def bash_rand(m): + """Get a random quote from bash.org""" + resp = get_url(m, "http://bash.org?random") + if resp: + soup = BeautifulSoup(resp) + raw = soup.find(class_="qt") + meta = soup.find(class_="quote") + while True: + if not raw: + bash_rand(m) + return + lines = raw.get_text().splitlines() + if len(lines) < 5: + break + raw = raw.find_next(class_="qt") + meta = soup.find_next(class_="quote") + format_quote(m, lines, meta) + else: + m.bot.private_message(m.location, "Could not find bash quote.") + + +def bash_specific(m, number): + """Get a specific quote from bash.org.""" + resp = get_url(m, "http://bash.org?" + number) + if resp: + soup = BeautifulSoup(resp) + raw = soup.find(class_="qt") + meta = soup.find(class_="quote") + lines = raw.get_text().splitlines() + if len(lines) and not m.is_pm: + m.bot.private_message(m.location, "This quote is too long to post publicly, " + "but you can view it at http://bash.org?" + "{}.".format(number)) + else: + format_quote(m, lines, meta, number) + else: + m.bot.private_message(m.location, "Could not find bash quote.") + + +def format_quote(m, raw, meta, number=None): + """Format the quote with some metadata.""" + score = meta.font.string + score_str = "\x0304{}\x03".format(score) if "-" in score else "\x0303{}\x03".format(score) + url = "http://bash.org?" + (number if number else meta.b.string.strip("#")) + meta_str = "--- {} ({}) {} ".format(meta.b.string, score_str, url) + m.bot.private_message(m.location, meta_str.ljust(80, '-')) + for line in raw: + m.bot.private_message(m.location, line) + m.bot.private_message(m.location, "-" * 80)