mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-13 06:59:53 -05:00
update error handling and add cache
This commit is contained in:
parent
989ff4abc4
commit
422165c01b
@ -15,38 +15,70 @@ class Bothttpcodes():
|
|||||||
self.api_url = 'https://httpstatuses.com/'
|
self.api_url = 'https://httpstatuses.com/'
|
||||||
self.basic_xpath = "/html/body/article/h1[1]/text()"
|
self.basic_xpath = "/html/body/article/h1[1]/text()"
|
||||||
self.explain_xpath = "/html/body/article/p[1]/text()"
|
self.explain_xpath = "/html/body/article/p[1]/text()"
|
||||||
|
self.cache = {}
|
||||||
|
|
||||||
def fetch_result(self, query):
|
def fetch_result(self, query):
|
||||||
|
|
||||||
|
query = str(query)
|
||||||
|
|
||||||
|
if not query:
|
||||||
|
return {
|
||||||
|
"query": "",
|
||||||
|
"basic": "An Error Occured",
|
||||||
|
"explanation": "You must provide a HTTP status code to look up.",
|
||||||
|
"error": True
|
||||||
|
}
|
||||||
|
|
||||||
|
# check cache
|
||||||
|
cached = self.check_cache(query)
|
||||||
|
if cached:
|
||||||
|
return cached
|
||||||
|
|
||||||
returndict = {
|
returndict = {
|
||||||
|
"query": query,
|
||||||
"basic": "",
|
"basic": "",
|
||||||
"explanation": "",
|
"explanation": "",
|
||||||
|
"error": False
|
||||||
}
|
}
|
||||||
|
|
||||||
if not re.match('^[1-5]\d{2}$', query):
|
if not re.match('^[1-5]\d{2}$', query):
|
||||||
returndict["basic"] = "Invalid HTTP status code:"
|
returndict["error"] = True
|
||||||
returndict["explanation"] = str(query)
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["explanation"] = "Invalid HTTP status code: %s" % query
|
||||||
|
self.add_to_cache(returndict)
|
||||||
return returndict
|
return returndict
|
||||||
|
|
||||||
url = self.api_url + query
|
url = self.api_url + query
|
||||||
try:
|
try:
|
||||||
r = requests.get(url=url, timeout=(10.0, 4.0))
|
r = requests.get(url=url, timeout=(10.0, 4.0))
|
||||||
except requests.exceptions.ConnectTimeout:
|
except requests.exceptions.ConnectTimeout:
|
||||||
returndict["basic"] = "Connection timed out."
|
returndict["error"] = True
|
||||||
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["explanation"] = "Connection timed out."
|
||||||
return returndict
|
return returndict
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError:
|
||||||
returndict["basic"] = "Couldn't connect to server."
|
returndict["error"] = True
|
||||||
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["explanation"] = "Couldn't connect to server."
|
||||||
return returndict
|
return returndict
|
||||||
except requests.exceptions.ReadTimeout:
|
except requests.exceptions.ReadTimeout:
|
||||||
returndict["basic"] = "Server took too long to send data."
|
returndict["error"] = True
|
||||||
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["explanation"] = "Server took too long to send data."
|
||||||
return returndict
|
return returndict
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
returndict["basic"] = "Unknown HTTP status code:"
|
returndict["error"] = True
|
||||||
returndict["explanation"] = str(query)
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["explanation"] = "Unknown HTTP status code: %s" % query
|
||||||
|
self.add_to_cache(returndict)
|
||||||
return returndict
|
return returndict
|
||||||
try:
|
try:
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.HTTPError as e:
|
except requests.exceptions.HTTPError as e:
|
||||||
returndict["basic"] = "HTTP error:"
|
returndict["error"] = True
|
||||||
returndict["explanation"] = str(e.message)
|
returndict["basic"] = "An Error Occured"
|
||||||
|
returndict["basic"] = "HTTP error: %s" % str(e.message)
|
||||||
|
self.add_to_cache(returndict)
|
||||||
return returndict
|
return returndict
|
||||||
|
|
||||||
tree = html.fromstring(r.content)
|
tree = html.fromstring(r.content)
|
||||||
@ -59,7 +91,8 @@ class Bothttpcodes():
|
|||||||
if title:
|
if title:
|
||||||
returndict["basic"] = str(title)
|
returndict["basic"] = str(title)
|
||||||
else:
|
else:
|
||||||
returndict["basic"] = "An Error Occured:"
|
returndict["error"] = True
|
||||||
|
returndict["basic"] = "An Error Occured"
|
||||||
returndict["explanation"] = "Xpath for basic yeilded no information"
|
returndict["explanation"] = "Xpath for basic yeilded no information"
|
||||||
return returndict
|
return returndict
|
||||||
|
|
||||||
@ -74,7 +107,21 @@ class Bothttpcodes():
|
|||||||
else:
|
else:
|
||||||
returndict["explanation"] = "Xpath for explanation yeilded no information"
|
returndict["explanation"] = "Xpath for explanation yeilded no information"
|
||||||
|
|
||||||
|
self.add_to_cache(returndict)
|
||||||
return returndict
|
return returndict
|
||||||
|
|
||||||
|
def add_to_cache(self, cachedict):
|
||||||
|
if cachedict["query"] not in list(self.cache.keys()):
|
||||||
|
self.cache[cachedict["query"]] = {
|
||||||
|
"query": cachedict["query"],
|
||||||
|
"basic": cachedict["basic"],
|
||||||
|
"explanation": cachedict["explanation"],
|
||||||
|
}
|
||||||
|
|
||||||
|
def check_cache(self, query):
|
||||||
|
if query not in list(self.cache.keys()):
|
||||||
|
return None
|
||||||
|
return self.cache[query]
|
||||||
|
|
||||||
|
|
||||||
httpcodes = Bothttpcodes()
|
httpcodes = Bothttpcodes()
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
Added Tool to check HTTP code meanings, accessible via `.httpcodes` command
|
Added Tool to check HTTP code meanings, accessible via `.httpcodes` command
|
||||||
Migrated Functionality into a SpiceBot class for other tools to use
|
Migrated Functionality into a SpiceBot class for other tools to use
|
||||||
|
update error handling and add cache
|
||||||
|
|||||||
@ -12,10 +12,24 @@ import sopel_modules.SpiceBot as SpiceBot
|
|||||||
@SpiceBot.prerun('module')
|
@SpiceBot.prerun('module')
|
||||||
@sopel.module.commands('httpcode')
|
@sopel.module.commands('httpcode')
|
||||||
def bot_command_http_codes(bot, trigger, botcom):
|
def bot_command_http_codes(bot, trigger, botcom):
|
||||||
|
|
||||||
query = spicemanip(botcom.dict["args"], 1) or None
|
query = spicemanip(botcom.dict["args"], 1) or None
|
||||||
if not query:
|
query = str(query)
|
||||||
return bot.osd("You must provide a HTTP status code to look up.")
|
|
||||||
message = ["[HTTP code search for " + str(query) + "]"]
|
result = SpiceBot.httpcodes.fetch_result(query)
|
||||||
result = SpiceBot.httpcodes.fetch_result(str(query))
|
|
||||||
|
message = ["[HTTP code search for " + str(query or "") + "]"]
|
||||||
|
|
||||||
|
if not result["error"] or not query:
|
||||||
message.extend([result["basic"], result["explanation"]])
|
message.extend([result["basic"], result["explanation"]])
|
||||||
|
|
||||||
|
elif query in ["urmom", "yourmom"]:
|
||||||
|
message.extend(["Too Many Results", "There are too many pages on the internet regarding your mother."])
|
||||||
|
|
||||||
|
elif query == "42":
|
||||||
|
message.extend(["The Hitchhiker's Guide To The Galaxy", "The answer to life, the world, and everything."])
|
||||||
|
|
||||||
|
elif query == "69":
|
||||||
|
message.extend(["Oral Fixation", "Round and Round they go, where they stop: nobody knows"])
|
||||||
|
|
||||||
bot.osd(message)
|
bot.osd(message)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user