get catfacts/catpics with api requests

This commit is contained in:
Gordon Shumway 2019-04-06 18:23:30 -04:00 committed by GitHub
parent 15a04d473f
commit 13465e70a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ import supybot.callbacks as callbacks
import random import random
import datetime import datetime
import os import os
import requests
try: try:
from supybot.i18n import PluginInternationalization from supybot.i18n import PluginInternationalization
@ -51,7 +52,8 @@ class Cayenne(callbacks.Plugin):
""" """
Get a random cat fact Get a random cat fact
""" """
return random.choice(self.cat_facts) data = requests.get("https://catfact.ninja/fact").json()
return data['fact']
def message_contains_trigger_word(self, message): def message_contains_trigger_word(self, message):
""" """
@ -77,17 +79,14 @@ class Cayenne(callbacks.Plugin):
Query cat URL to get a random link Query cat URL to get a random link
""" """
try: try:
link_url = self.registryValue("linkURL") response = utils.web.getUrl("http://edgecats.net/random").decode("utf8")
response = utils.web.getUrl(link_url).decode("utf8")
# Expecting a link # Expecting a link
if "http" in response: if "http" in response:
return response return response
else: else:
self.log.error("Cayenne: received unexpected response from cat URL: %s" % (response)) self.log.error("Received unexpected response from http://edgecats.net/random")
except: except:
self.log.exception("Cayenne: error fetching cat URL") self.log.exception("Error fetching URL")
def doPrivmsg(self, irc, msg): def doPrivmsg(self, irc, msg):
""" """