added functions to reloadsubbreeds and listsubbreeds, for sub-breeds of breeds on dog.ceo API
This commit is contained in:
parent
85a037aefc
commit
a6086b75b3
1 changed files with 64 additions and 1 deletions
65
doggo.py
65
doggo.py
|
@ -36,7 +36,6 @@ class Doggo(BotPlugin):
|
||||||
|
|
||||||
return resp.json()['message']
|
return resp.json()['message']
|
||||||
|
|
||||||
|
|
||||||
@botcmd(split_args_with=' ')
|
@botcmd(split_args_with=' ')
|
||||||
def listbreeds(self, msg, args):
|
def listbreeds(self, msg, args):
|
||||||
"""
|
"""
|
||||||
|
@ -79,3 +78,67 @@ class Doggo(BotPlugin):
|
||||||
# Tell user command was successful
|
# Tell user command was successful
|
||||||
direct_to_user = self.build_identifier(str(msg.frm))
|
direct_to_user = self.build_identifier(str(msg.frm))
|
||||||
self.send(direct_to_user, "Breed list successfully loaded")
|
self.send(direct_to_user, "Breed list successfully loaded")
|
||||||
|
|
||||||
|
@botcmd(split_args_with=' ')
|
||||||
|
def listsubbreeds(self, msg, args):
|
||||||
|
"""
|
||||||
|
List available sub-breeds of a breed for use in the random image retriever
|
||||||
|
"""
|
||||||
|
if len(args) > 0 and args[0]:
|
||||||
|
breed = args[0]
|
||||||
|
if not self.breeds:
|
||||||
|
args.append('calledbyfunction')
|
||||||
|
self.reloadbreeds(msg, args)
|
||||||
|
if not self.breeds:
|
||||||
|
return 'Unable to load sub-breeds list'
|
||||||
|
if breed not in self.breeds.keys():
|
||||||
|
return 'Breed not found: {}. List breeds with !listbreeds'.format(breed)
|
||||||
|
else:
|
||||||
|
return 'Please tell me the breed for which you want sub-breeds, like: *!listsubbreeds terrier*'
|
||||||
|
|
||||||
|
# Send the output to the user to prevent spamming the channel
|
||||||
|
direct_to_user = self.build_identifier(str(msg.frm))
|
||||||
|
|
||||||
|
if len(self.breeds[breed]) == 0:
|
||||||
|
args.append('calledbyfunction')
|
||||||
|
self.reloadsubbreeds(msg, args)
|
||||||
|
if len(self.breeds[breed]) == 0:
|
||||||
|
return 'No sub-breeds available for {}.'.format(breed)
|
||||||
|
|
||||||
|
for subbreed in sorted(self.breeds[breed]):
|
||||||
|
self.send(direct_to_user, subbreed)
|
||||||
|
|
||||||
|
@botcmd(admin_only=True, split_args_with=' ')
|
||||||
|
def reloadsubbreeds(self, msg, args):
|
||||||
|
"""
|
||||||
|
Reloads the list of sub-breeds currently available for a given breed
|
||||||
|
"""
|
||||||
|
if len(args) > 0 and args[0]:
|
||||||
|
breed = args[0]
|
||||||
|
if not self.breeds:
|
||||||
|
self.reloadbreeds(msg, args)
|
||||||
|
if not self.breeds:
|
||||||
|
return 'Unable to load sub-breeds list'
|
||||||
|
if breed in self.breeds.keys():
|
||||||
|
url = '{}/breed/{}/list'.format(self.BASE_URL, breed)
|
||||||
|
else:
|
||||||
|
return 'Breed not found: {}. List breeds with !listbreeds'.format(breed)
|
||||||
|
else:
|
||||||
|
return 'Please tell me the breed for which you want sub-breeds, like: *!reloadsubbreeds terrier*'
|
||||||
|
|
||||||
|
try:
|
||||||
|
resp = requests.get(url)
|
||||||
|
resp.raise_for_status()
|
||||||
|
except requests.exceptions.HTTPError as e:
|
||||||
|
logging.debug(e)
|
||||||
|
return 'Unable to load sub-breeds list'
|
||||||
|
|
||||||
|
data = resp.json()['message']
|
||||||
|
if not isinstance(data, list):
|
||||||
|
return 'Unable to load sub-breeds list'
|
||||||
|
|
||||||
|
self.breeds[breed] = data
|
||||||
|
if 'calledbyfunction' not in args:
|
||||||
|
# Tell user command was successful
|
||||||
|
direct_to_user = self.build_identifier(str(msg.frm))
|
||||||
|
self.send(direct_to_user, "Sub-breed list successfully loaded")
|
||||||
|
|
Loading…
Reference in a new issue