-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdog.py
77 lines (58 loc) · 2.09 KB
/
dog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import json
from errbot import botcmd, BotPlugin
import requests
class Dog(BotPlugin):
"""Get random dog image URLs"""
BASE_URL = 'https://dog.ceo/api'
breeds = []
@botcmd(split_args_with=' ')
def doggo(self, msg, args):
"""
Retrieve a random dog image, optionally specifying a breed
"""
url = '{}/breeds/image/random'.format(self.BASE_URL)
if len(args) > 0 and args[0]:
breed = args[0]
if not self.breeds:
self.reloadbreeds(msg, args)
if breed in self.breeds:
url = '{}/breed/{}/images/random'.format(self.BASE_URL, breed)
else:
return 'Breed not found: {}. List breeds with !listbreeds'.format(breed)
try:
resp = requests.get(url)
resp.raise_for_status()
except requests.exceptions.HTTPError as e:
logging.debug(e)
return 'Unable to return a dog image'
return resp.json()['message']
@botcmd
def listbreeds(self, msg, args):
"""
List available breeds for use in the random image retriever
"""
if not self.breeds:
self.reloadbreeds(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(msg.frm.nick))
for breed in sorted(self.breeds):
self.send(direct_to_user, breed)
@botcmd(admin_only=True)
def reloadbreeds(self, msg, args):
"""
Reloads the list of breeds currently available
"""
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