adds breed list and single breed random image
This commit is contained in:
parent
7dc839c1dd
commit
67af730a73
1 changed files with 46 additions and 2 deletions
46
dog.py
46
dog.py
|
@ -11,8 +11,20 @@ class Dog(BotPlugin):
|
||||||
|
|
||||||
BASE_URL = 'https://dog.ceo/api'
|
BASE_URL = 'https://dog.ceo/api'
|
||||||
|
|
||||||
@botcmd
|
breeds = None
|
||||||
|
|
||||||
|
@botcmd(split_args_with=' ')
|
||||||
def doggo(self, msg, args):
|
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)
|
url = '{}/breeds/image/random'.format(self.BASE_URL)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -23,3 +35,35 @@ class Dog(BotPlugin):
|
||||||
return 'Unable to return a dog image'
|
return 'Unable to return a dog image'
|
||||||
|
|
||||||
return resp.json()['message']
|
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
|
||||||
|
|
Loading…
Reference in a new issue