示例#1
0
 async def image(self, breed: str = None):
     url = self.base + "images/search?has_breeds=1"
     if breed:
         breeds = {}
         async with self.session.get(
                 url=self.base + "breeds",
                 headers={"x-api-key": os.getenv("DOG")}) as resp:
             if resp.status != 200:
                 return raise_for_status(resp)
             breed_list = await resp.json()
         for x in breed_list:
             breeds[x["name"].lower()] = x["id"]
         if breed.lower() not in breeds:
             return None, None, None
         url += f"&breed_id={breeds[breed.lower()]}"
     async with self.session.get(url=url,
                                 headers={"x-api-key":
                                          os.getenv("DOG")}) as resp:
         if resp.status != 200:
             return raise_for_status(resp)
         resp = (await resp.json())[0]
         details = ""
         for x, y in resp["breeds"][0].items():
             if x not in ["id", "name"]:
                 x = x.capitalize().replace('_', ' ')
                 if x == "Height":
                     height = f"**{x}:** {y['imperial']}in ({y['metric']}cm)\n"
                     continue
                 if x == "Weight":
                     weight = f"**{x}:** {y['imperial']}lb ({y['metric']}kg)\n"
                     continue
                 details += f"**{x}:** {y}\n"
         details += height + weight
         return resp["url"], resp["breeds"][0]["name"], details
示例#2
0
 async def image(self, breed: str = None):
     url = self.base + "images/search?has_breeds=1"
     if breed:
         breeds = {}
         async with self.session.get(
                 url=self.base + "breeds",
                 headers={"x-api-key": os.getenv("CAT")}) as resp:
             if resp.status != 200:
                 return raise_for_status(resp)
             breed_list = await resp.json()
         for x in breed_list:
             breeds[x["name"].lower()] = x["id"]
         if breed.lower() not in breeds:
             return None, None, None
         url += f"&breed_id={breeds[breed.lower()]}"
     async with self.session.get(url=url,
                                 headers={"x-api-key":
                                          os.getenv("CAT")}) as resp:
         if resp.status != 200:
             return raise_for_status(resp)
         resp = (await resp.json())[0]
         details = ""
         traits = []
         urls = {}
         for x, y in resp["breeds"][0].items():
             if x not in ["id", "name", "country_codes", "country_code"]:
                 x = x.capitalize().replace("_", " ")
                 if x in [
                         "Cfa url", "Vetstreet url", "Vcahospitals url",
                         "Wikipedia url"
                 ]:
                     x = x.split(" ")[0].replace("Cfa", "CFA").replace(
                         "Vcahospitals", "VCA Hospitals")
                     urls[x] = y.replace("(", "").replace(")", "")
                 elif x == "Origin":
                     origin = f"**{x}:** {y} ({resp['breeds'][0]['country_code']})\n"
                 elif x == "Weight":
                     weight = f"**{x}:** {y['imperial']}lb ({y['metric']}kg)\n"
                 elif x == "Life span":
                     details += f"**{x}:** {y} years\n"
                 elif x in self.traits:
                     if y == 1:
                         traits.append(x)
                 elif x in self.scale:
                     y = str(y).replace("1", "Bad").replace("2", "Poor").replace("3", "Average") \
                         .replace("4", "Above average").replace("5", "Good")
                     details += f"**{x}:** {y}\n"
                 else:
                     details += f"**{x}:** {y}\n"
         details = origin + details
         details += f"**Traits:** {', '.join(traits)}\n" if traits else ""
         details += weight
         if urls:
             details += f"**URLs:** {' • '.join([f'[{x}]({y})' for x, y in urls.items()])}"
         return resp["url"], resp["breeds"][0]["name"], details
示例#3
0
 async def breeds(self):
     async with self.session.get(url=self.base + "breeds",
                                 headers={"x-api-key":
                                          os.getenv("DOG")}) as resp:
         if resp.status != 200:
             return raise_for_status(resp)
         breed_list = await resp.json()
     breeds = [x["name"] for x in breed_list]
     breed_count = len(breeds)
     breeds = ", ".join(breeds)
     pages = pagify(breeds, delims=[" "], page_length=2048)
     pages = [x for x in pages]
     return pages, breed_count
示例#4
0
 async def fact(self, animal: str):
     endpoint = choice(self.endpoints["facts"][animal])
     async with self.session.get(url=endpoint["url"]) as resp:
         if resp.status != 200:
             return raise_for_status(resp)
         return (await resp.json())[endpoint["key"]]