From 67af730a73c5c2d8f596e08ca1c499580d07c040 Mon Sep 17 00:00:00 2001
From: Jared McKnight <jmcknight@archertech.ca>
Date: Thu, 15 Feb 2018 16:22:31 -0500
Subject: [PATCH] adds breed list and single breed random image

---
 dog.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/dog.py b/dog.py
index 79e247b..d1baeb7 100644
--- a/dog.py
+++ b/dog.py
@@ -11,9 +11,21 @@ class Dog(BotPlugin):
 
     BASE_URL = 'https://dog.ceo/api'
 
-    @botcmd
+    breeds = None
+
+    @botcmd(split_args_with=' ')
     def doggo(self, msg, args):
-        url = '{}/breeds/image/random'.format(self.BASE_URL)
+        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:
             resp = requests.get(url)
@@ -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