Skip to content

Commit

Permalink
Added Catboys API
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekidev committed Apr 30, 2023
1 parent e5c1b28 commit 2a21d9c
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 163 deletions.
304 changes: 145 additions & 159 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion anime_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
), # Yandere and Konachan are forks of the same github repo. That's why they have almost-identical apis.
("Konachan", None, "https://konachan.com/help/api", False),
("Waifu.im", apis.WaifuImAPI, "https://waifu.im/", True),
("Catboys", None, "https://catboys.com/api", False),
("Catboys", apis.CatboysAPI, "https://catboys.com/api", True),
(
"Anime Character Database",
None,
Expand Down
1 change: 1 addition & 0 deletions anime_api/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
from .nekos_best import NekosBest
from .waifu_im import WaifuImAPI
from .nekos_api import NekosAPI
from .catboys import CatboysAPI
85 changes: 85 additions & 0 deletions anime_api/apis/catboys/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Base module for the Catboys API. Endpoints can be found at https://catboys.com/api
"""
import typing
import requests

from anime_api import exceptions

from anime_api.apis.catboys.objects import Image, EightBall, DiceRoll


class CatboysAPI:
"""
Endpoints: https://api.catboys.com/endpoints
"""

endpoint: str = "https://api.catboys.com"

def __init__(self, endpoint: typing.Optional[str] = None):
self.endpoint = endpoint or self.endpoint

def get_random_image(self) -> Image:
"""
Returns a random image.
"""

response = requests.get(self.endpoint + "/img")
CatboysAPI._check_response_code(response)

return Image.from_json(response.json())

def get_random_baka_gif(self) -> str:
"""
Returns a random baka gif.
"""

response = requests.get(self.endpoint + "/baka")
CatboysAPI._check_response_code(response)

return response.json()["url"]

def get_eight_ball(self) -> EightBall:
"""
Returns an 8ball prediction.
"""

response = requests.get(self.endpoint + "/8ball")
CatboysAPI._check_response_code(response)

return EightBall.from_json(response.json())

def get_dice_roll(self) -> DiceRoll:
"""
Returns a random number and image from a virtual 6-sided dice.
"""

response = requests.get(self.endpoint + "/dice")
CatboysAPI._check_response_code(response)

return DiceRoll.from_json(response.json())

def get_catboy_saying(self) -> str:
"""
Returns a random saying from a virtual catboy.
"""

response = requests.get(self.endpoint + "/catboy")
CatboysAPI._check_response_code(response)

return response.json()["response"]

@staticmethod
def _check_response_code(res) -> None:
"""
Check if the request was successful.
"""
data = res.json()
if (
res.status_code not in range(200, 300)
or data.get("error", "none") != "none"
):
raise exceptions.ServerError(
res.status_code,
f"An error occurred while fetching the data from the server{'. ' + data['error'] if data.get('error', 'none') != 'none' else ''}",
)
39 changes: 39 additions & 0 deletions anime_api/apis/catboys/objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dataclasses import dataclass


@dataclass
class _Artist:
name: str
url: str


@dataclass
class Image:
url: str
artist: _Artist
source_url: str

def from_json(data: dict):
return Image(
url=data["url"],
artist=_Artist(data["artist"], data["artist_url"]),
source_url=data["source_url"],
)


@dataclass
class EightBall:
answer: str
image: str

def from_json(data: dict):
return EightBall(answer=data["response"], image=data["url"])


@dataclass
class DiceRoll:
number: int
image: str

def from_json(data: dict):
return DiceRoll(number=data["response"], image=data["url"])
4 changes: 2 additions & 2 deletions anime_api/apis/nekos_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def decorator(*args, **kwargs):

class NekosAPI:
"""
Docs: https://nekos.nekidev.com/docs/rest-api/endpoints
Docs: https://v1.nekosapi.com/docs/rest-api/endpoints
"""

endpoint: str = "https://nekos.nekidev.com/api"
endpoint: str = "https://api.nekosapi.com/v1"
token: typing.Optional[str] = None

def __init__(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "anime-api"
version = "0.15.4"
version = "1.0.0"
description = "A collection of wrappers for anime-related APIs"
authors = ["Neki <[email protected]>"]
readme = "README.md"
Expand Down
58 changes: 58 additions & 0 deletions tests/test_catboys_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Run tests for the AnimuAPI class
Usage:
Replace the api_token variable with your API token.
cd tests
poetry run python -m pytest test_catboys_api.py
"""
from anime_api.apis import CatboysAPI
from anime_api.apis.catboys import objects


def test_get_random_image():
"""
Test the get_random_image method
"""
api = CatboysAPI()
image = api.get_random_image()
assert isinstance(image, objects.Image)


def test_get_random_baka_gif():
"""
Test the get_random_baka_gif method
"""
api = CatboysAPI()
gif = api.get_random_baka_gif()
assert isinstance(gif, str)


def test_get_eight_ball():
"""
Test the get_eight_ball method
"""
api = CatboysAPI()
eight_ball = api.get_eight_ball()
assert isinstance(eight_ball, objects.EightBall)


def test_get_dice_roll():
"""
Test the get_dice_roll method
"""

api = CatboysAPI()
dice_roll = api.get_dice_roll()
assert isinstance(dice_roll, objects.DiceRoll)


def test_get_catboy_saying():
"""
Test the get_catboy_saying method
"""

api = CatboysAPI()
catboy_saying = api.get_catboy_saying()
assert isinstance(catboy_saying, str)

0 comments on commit 2a21d9c

Please sign in to comment.