-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addes support for the Nekos.best API
- Loading branch information
Showing
9 changed files
with
326 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
""" | ||
Base module for the nekos.best API. Documentation can be found at https://docs.nekos.best | ||
""" | ||
import typing | ||
import requests | ||
|
||
from anime_api import exceptions | ||
from anime_api.apis.nekos_best.objects import Image, Artist, Anime | ||
from anime_api.apis.nekos_best.types import ImageCategory, ImageType | ||
|
||
|
||
class NekosBest: | ||
""" | ||
Docs: https://docs.nekos.best/ | ||
""" | ||
|
||
endpoint = "https://nekos.best/api/v2" | ||
|
||
def get_random_images( | ||
self, image_type: ImageCategory, amount: int = 1 | ||
) -> typing.List[Image]: | ||
""" | ||
Returns a list of random images | ||
""" | ||
|
||
if amount < 1 or amount > 20: | ||
raise ValueError("Amount must be between 1 and 20") | ||
|
||
response = requests.get( | ||
self.endpoint + "/" + image_type.value, params={"amount": amount} | ||
) | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError(status_code=response.status_code) | ||
|
||
return [ | ||
Image( | ||
url=image["url"], | ||
artist=Artist( | ||
url=image["artist_href"], | ||
name=image["artist_name"], | ||
) | ||
if "artist_href" in image | ||
else None, | ||
anime=Anime( | ||
title=image["anime_name"], | ||
) | ||
if "anime_name" in image | ||
else None, | ||
source_url=image["source_url"] if "source_url" in image else None, | ||
) | ||
for image in response.json()["results"] | ||
] | ||
|
||
def search_images( | ||
self, | ||
query: str, | ||
image_category: typing.Optional[ImageCategory] = None, | ||
image_type: typing.Optional[ImageType] = None, | ||
amount: int = 1, | ||
) -> typing.List[Image]: | ||
""" | ||
Returns a list of images that match the query | ||
""" | ||
|
||
if amount < 1 or amount > 20: | ||
raise ValueError("Amount must be between 1 and 20") | ||
|
||
params = { | ||
"query": query, | ||
"amount": amount, | ||
} | ||
|
||
if image_type: | ||
params["type"] = image_type.value | ||
|
||
if image_category: | ||
params["category"] = image_category.value | ||
|
||
response = requests.get(self.endpoint + "/search", params=params) | ||
|
||
if response.status_code != 200: | ||
raise exceptions.ServerError(status_code=response.status_code) | ||
|
||
return [ | ||
Image( | ||
url=image["url"], | ||
artist=Artist( | ||
url=image["artist_href"], | ||
name=image["artist_name"], | ||
) | ||
if "artist_href" in image | ||
else None, | ||
anime=Anime( | ||
title=image["anime_name"], | ||
) | ||
if "anime_name" in image | ||
else None, | ||
source_url=image["source_url"] if "source_url" in image else None, | ||
) | ||
for image in response.json()["results"] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from dataclasses import dataclass | ||
|
||
import typing | ||
|
||
|
||
@dataclass | ||
class Artist: | ||
""" | ||
Object representation of an image's artist | ||
""" | ||
|
||
url: typing.Optional[str] = None | ||
name: typing.Optional[str] = None | ||
|
||
|
||
@dataclass | ||
class Anime: | ||
""" | ||
Object representation of an anime | ||
""" | ||
|
||
title: str | ||
|
||
|
||
@dataclass | ||
class Image: | ||
""" | ||
Object representation of an image | ||
""" | ||
|
||
url: str | ||
artist: typing.Optional[Artist] = None | ||
anime: typing.Optional[Anime] = None | ||
source_url: typing.Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from enum import Enum | ||
|
||
|
||
class ImageCategory(Enum): | ||
""" | ||
Image's category | ||
""" | ||
|
||
HIGHFIVE = "highfive" | ||
HAPPY = "happy" | ||
SLEEP = "sleep" | ||
HANDHOLD = "handhold" | ||
LAUGH = "laugh" | ||
BITE = "bite" | ||
POKE = "poke" | ||
TICKLE = "tickle" | ||
KISS = "kiss" | ||
WAVE = "wave" | ||
THUMBSUP = "thumbsup" | ||
STARE = "stare" | ||
CUDDLE = "cuddle" | ||
SMILE = "smile" | ||
BAKA = "baka" | ||
BLUSH = "blush" | ||
THINK = "think" | ||
POUT = "pout" | ||
FACEPALM = "facepalm" | ||
WINK = "wink" | ||
SHOOT = "shoot" | ||
SMUG = "smug" | ||
CRY = "cry" | ||
PAT = "pat" | ||
PUNCH = "punch" | ||
DANCE = "dance" | ||
FEED = "feed" | ||
SHRUG = "shrug" | ||
BORED = "bored" | ||
KICK = "kick" | ||
HUG = "hug" | ||
YEET = "yeet" | ||
SLAP = "slap" | ||
NEKO = "neko" | ||
HUSBANDO = "husbando" | ||
KITSUNE = "kitsune" | ||
WAIFU = "waifu" | ||
|
||
|
||
class ImageType(Enum): | ||
""" | ||
Image format | ||
""" | ||
|
||
IMAGE = 1 | ||
GIF = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
Run tests for the NekosBest class | ||
""" | ||
from anime_api.apis import NekosBest | ||
from anime_api.apis.nekos_best.objects import Image | ||
from anime_api.apis.nekos_best.types import ImageCategory, ImageType | ||
|
||
|
||
def test_get_random_images(): | ||
""" | ||
Tests the get_random_images method | ||
""" | ||
api = NekosBest() | ||
images = api.get_random_images(ImageCategory.NEKO) | ||
assert isinstance(images, list) | ||
assert len(images) > 0 | ||
assert isinstance(images[0], Image) | ||
|
||
|
||
def test_search_images(): | ||
""" | ||
Tests the search_images method | ||
""" | ||
api = NekosBest() | ||
images = api.search_images("neko", ImageCategory.NEKO, ImageType.IMAGE, 3) | ||
for image in images: | ||
assert isinstance(image, Image) | ||
assert not image.url.endswith(".gif") |