From 584f56e6968b1f67fde213e27c5328f3a25ab0f6 Mon Sep 17 00:00:00 2001 From: deathbybandaid Date: Fri, 4 Oct 2019 10:04:40 -0400 Subject: [PATCH] test --- sopel_modules/SpiceBot/HTTP_Codes.py | 80 +++++++++++++++++++ sopel_modules/SpiceBot/Version.py | 2 +- sopel_modules/SpiceBot/__init__.py | 1 + .../SpiceBot_Release_Notes/HTTP_Codes.txt | 2 + .../SpiceBot_Core_Tools/HTTP_Codes.py | 4 +- 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 sopel_modules/SpiceBot/HTTP_Codes.py create mode 100644 sopel_modules/SpiceBot_Configs/SpiceBot_Release_Notes/HTTP_Codes.txt diff --git a/sopel_modules/SpiceBot/HTTP_Codes.py b/sopel_modules/SpiceBot/HTTP_Codes.py new file mode 100644 index 0000000..69cfd0f --- /dev/null +++ b/sopel_modules/SpiceBot/HTTP_Codes.py @@ -0,0 +1,80 @@ +# coding=utf8 +from __future__ import unicode_literals, absolute_import, division, print_function +""" +This is the SpiceBot HTTP Codes system. +""" + +import re +import requests +from lxml import html + + +class Bothttpcodes(): + + def __init__(self): + self.api_url = 'https://httpstatuses.com/' + self.basic_xpath = "/html/body/article/h1[1]/text()" + self.explain_xpath = "/html/body/article/p[1]/text()" + + def fetch_result(self, query): + returndict = { + "basic": "", + "explanation": "", + } + if not re.match('^[1-5]\d{2}$', query): + returndict["basic"] = "Invalid HTTP status code:" + returndict["explanation"] = str(query) + return returndict + + url = self.api_url + query + try: + r = requests.get(url=url, timeout=(10.0, 4.0)) + except requests.exceptions.ConnectTimeout: + returndict["basic"] = "Connection timed out." + return returndict + except requests.exceptions.ConnectionError: + returndict["basic"] = "Couldn't connect to server." + return returndict + except requests.exceptions.ReadTimeout: + returndict["basic"] = "Server took too long to send data." + return returndict + if r.status_code == 404: + returndict["basic"] = "Unknown HTTP status code:" + returndict["explanation"] = str(query) + return returndict + try: + r.raise_for_status() + except requests.exceptions.HTTPError as e: + returndict["basic"] = "HTTP error:" + returndict["explanation"] = str(e.message) + return returndict + + tree = html.fromstring(r.content) + try: + title = tree.xpath((self.basic_xpath)) + if isinstance(title, list): + title = title[0] + except Exception as e: + title = None + if title: + returndict["basic"] = str(title) + else: + returndict["basic"] = "An Error Occured:" + returndict["explanation"] = "Xpath for basic yeilded no information" + return returndict + + try: + explanation = tree.xpath((self.explain_xpath)) + if isinstance(explanation, list): + explanation = explanation[0] + except Exception as e: + explanation = None + if explanation: + returndict["explanation"] = str(explanation) + else: + returndict["explanation"] = "Xpath for explanation yeilded no information" + + return returndict + + +httpcodes = Bothttpcodes() diff --git a/sopel_modules/SpiceBot/Version.py b/sopel_modules/SpiceBot/Version.py index ad308de..a6c4a0b 100644 --- a/sopel_modules/SpiceBot/Version.py +++ b/sopel_modules/SpiceBot/Version.py @@ -1,7 +1,7 @@ # coding=utf8 from __future__ import unicode_literals, absolute_import, division, print_function """ -This is the SpiceBot Update system. +This is the SpiceBot Version system. """ import sopel diff --git a/sopel_modules/SpiceBot/__init__.py b/sopel_modules/SpiceBot/__init__.py index 90f1629..556fa86 100644 --- a/sopel_modules/SpiceBot/__init__.py +++ b/sopel_modules/SpiceBot/__init__.py @@ -40,6 +40,7 @@ from .Translate import * from .Version import * from .RegNicks import * from .ReleaseNotes import * +from .HTTP_Codes import * __author__ = 'Sam Zick' diff --git a/sopel_modules/SpiceBot_Configs/SpiceBot_Release_Notes/HTTP_Codes.txt b/sopel_modules/SpiceBot_Configs/SpiceBot_Release_Notes/HTTP_Codes.txt new file mode 100644 index 0000000..c6ba1d6 --- /dev/null +++ b/sopel_modules/SpiceBot_Configs/SpiceBot_Release_Notes/HTTP_Codes.txt @@ -0,0 +1,2 @@ +Added Tool to check HTTP code meanings, accessible via `.httpcodes` command +Migrated Functionality into a SpiceBot class for other tools to use diff --git a/sopel_modules/SpiceBot_Core_Tools/HTTP_Codes.py b/sopel_modules/SpiceBot_Core_Tools/HTTP_Codes.py index 7911264..0db2c58 100644 --- a/sopel_modules/SpiceBot_Core_Tools/HTTP_Codes.py +++ b/sopel_modules/SpiceBot_Core_Tools/HTTP_Codes.py @@ -24,8 +24,8 @@ def bot_command_http_codes(bot, trigger, botcom): if not query: return bot.osd("You must provide a HTTP status code to look up.") message = ["[HTTP code search for " + str(query) + "]"] - result = fetch_result(str(query)) - message.extend(result) + result = SpiceBot.httpcodes.fetch_result(str(query)) + message.append([result["basic"], result["explanation"]]) bot.osd(message)