adds breed list and single breed random image

This commit is contained in:
Jared McKnight 2018-02-15 16:22:31 -05:00
parent 7dc839c1dd
commit 67af730a73
No known key found for this signature in database
GPG key ID: DB3C3196E89FCC30

46
dog.py
View file

@ -11,8 +11,20 @@ class Dog(BotPlugin):
BASE_URL = 'https://dog.ceo/api'
@botcmd
breeds = None
@botcmd(split_args_with=' ')
def doggo(self, msg, args):
url = None
if len(args) > 0:
breed = args[0]
if not self.breeds:
self.reload_breeds(self, msg, args)
if breed in self.breeds:
url = '{}/breed/{}/images/random'.format(self.BASE_URL, breed)
if not url:
url = '{}/breeds/image/random'.format(self.BASE_URL)
try:
@ -23,3 +35,35 @@ class Dog(BotPlugin):
return 'Unable to return a dog image'
return resp.json()['message']
@botcmd
def breeds(self, msg, args):
if not self.breeds:
self.reload_breeds(self, msg, args)
if not self.breeds:
return 'Unable to load breeds list'
# Send the output to the user to prevent spamming the channel
direct_to_user = self.build_identifier(str(mess.frm.nick))
for breed in sorted(self.breeds):
self.send(direct_to_user, breed)
@botcmd(admin_only=True)
def reload_breeds(self, msg, args):
url = '{}/breeds/list'.format(self.BASE_URL)
try:
resp = requests.get(url)
resp.raise_for_status()
except requests.exceptions.HTTPError as e:
logging.debug(e)
return 'Unable to load breeds list'
data = resp.json()['message']
if not isinstance(data, list):
return 'Unable to load breeds list'
self.breeds = data