diff --git a/README.md b/README.md index b57886c..febe575 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@
-Get random anime gifs by category. Use Python (intended (for now) for Discord). +API wrapper for animegifs. Get random anime gifs by category. Use Python (intended (for now) for Discord). -WIP - updated in time to time. Versions below v0.5.3 aren't expected to work flawlessly or at all. Version below v0.7 will not have the gifs library updated anymore. +WIP - updated in time to time. Versions below v0.5.3 aren't expected to work flawlessly or at all. +Version below v1.0 will not have the gifs library updated anymore and the gifs may return 404 as they were hosted on Discord. For troubleshoots, known errors and categories list, check below. `pip install animegifs` @@ -90,7 +91,7 @@ except animegifs.errors.CategoryError: * Marry **N** -* Nod, Nosebleed, Nuzzle +* Nod, Nosebleed, Note, Nuzzle **P** * Pat, Peck, Poke, Popcorn, Pout, Punch, Punish @@ -124,7 +125,6 @@ If you also want to contribute to the gifs collection, you can submit a gif at: # Troubleshooting and other -The first call (only!) in the session is expected to have a slower reaction time (5-15s) because of the authentication process. If you encounter an error, please raise an issue on the issue page: https://github.com/MarcoSa-2000/animegifs/issues. Alternatively, you can join my Discord server (https://discord.com/invite/TKZJ4GJj2z) to request new categories, functions, provide feedback, or report any errors. I do also have a multi-function Discord bot. Feel free to check out the web dashboard here: https://enkidu-app.github.io. diff --git a/animegifs/animegifs.py b/animegifs/animegifs.py index 9ed1fbd..7506f30 100644 --- a/animegifs/animegifs.py +++ b/animegifs/animegifs.py @@ -1,6 +1,33 @@ -import random -from mal import Anime -from animegifs.distutils import gifs, errors +from animegifs.distutils import errors +import requests +import urllib.parse + +version = "v3" + +def request_api(type, arg): + arg = urllib.parse.quote(arg) + if type == "get_gif": + gif_url = requests.get(f"https://animegifs-enkidu.koyeb.app/{version}/api/?category={arg}") + if gif_url.status_code != 200: + raise errors.CategoryError + data = gif_url.json() + gif = data['gif'] + return gif + elif type == "get_mal": + gif_mal = requests.get(f"https://animegifs-enkidu.koyeb.app/{version}/get_mal/?gif={arg}") + data = gif_mal.json() + mal = data['mal'] + return mal + elif type == "get_mal_id": + gif_mal_id = requests.get(f"https://animegifs-enkidu.koyeb.app/{version}/get_malid/?gif={arg}") + data = gif_mal_id.json() + mal_id = data['mal_id'] + return mal_id + elif type == "get_animetitle": + gif_animetitle = requests.get(f"https://animegifs-enkidu.koyeb.app/{version}/get_animetitle/?gif={arg}") + data = gif_animetitle.json() + animetitle = data['animetitle'] + return animetitle class Animegifs: @@ -15,30 +42,14 @@ def get_gif(self, category: str) -> str: category (str): Valid categories: attack, bite, bloodsuck, blush, bonk, brofist, cry, cuddle, dance, disgust, exploding, facedesk, facepalm, flick, flirt, handhold, happy, harass, highfive, hug, icecream, insult, kill, kiss, - lick, love, marry, nod, nosebleed, nuzzle, pat, peck, poke, popcorn, pout, + lick, love, marry, nod, nosebleed, note, nuzzle, pat, peck, poke, popcorn, pout, punch, punish, random, run, sad, scared, shoot, shrug, sip, slap, smirk, sorry, spank, stare, steal-magic, tease, threat, tickle, tired, wave, yawn. Returns: gif: gif (url) -> str """ - if type(category) is int: - raise errors.CategoryIntegral(category) - if category.lower() in list(gifs.access().keys()) or category.lower() == 'random': - if category.lower() == 'random': - gif_list = [] - for key, gif_url in gifs.access().items(): - for urls in gif_url: - gif_list.append(urls[0]) - gif = gif_list - else: - gifs_list = gifs.access()[category.lower()] - gif = [gif_url[0] for gif_url in gifs_list] - gif = random.choice(gif) - elif category.lower() not in list(gifs.access().keys()): - raise errors.CategoryError(category) - else: - raise errors.CategoryUnknown(category) + gif = request_api("get_gif", category) return gif def get_mal(self, gif) -> str: @@ -51,17 +62,10 @@ def get_mal(self, gif) -> str: Returns: mal: mal (url) -> str """ - for key, gif_url in gifs.access().items(): - for gif_name in gif_url: - if gif_name[0] == gif: - result = gif_name[1] - try: - mal = f"https://myanimelist.net/anime/{int(result)}/" - except ValueError as exc: - raise errors.MethodNotUpdated(gif) from exc - return mal - else: - continue + mal = request_api("get_mal", gif) + if mal == "null": + raise errors.CategoryError + return mal def get_malId(self, gif) -> int: """ @@ -73,17 +77,10 @@ def get_malId(self, gif) -> int: Returns: malid: malId -> int """ - for key, gif_url in gifs.access().items(): - for gif_name in gif_url: - if gif_name[0] == gif: - result = gif_name[1] - try: - malid = int(result) - except ValueError as exc: - raise errors.MethodNotUpdated(gif) from exc - return malid - else: - continue + mal_id = request_api("get_mal_id", gif) + if mal_id == "null": + raise errors.CategoryError + return mal_id def get_animetitle(self, gif) -> str: """ @@ -95,14 +92,7 @@ def get_animetitle(self, gif) -> str: Returns: title: title -> str """ - for key, gif_url in gifs.access().items(): - for gif_name in gif_url: - if gif_name[0] == gif: - result = gif_name[1] - try: - title = Anime(int(result)).title - except ValueError as exc: - raise errors.AnimeNotFound(gif) from exc - return title - else: - continue + animetitle = request_api("get_animetitle", gif) + if animetitle == "null": + raise errors.CategoryError + return animetitle diff --git a/animegifs/distutils/errors.py b/animegifs/distutils/errors.py index 99a8c09..214fb18 100644 --- a/animegifs/distutils/errors.py +++ b/animegifs/distutils/errors.py @@ -1,29 +1,6 @@ -class CategoryIntegral(Exception): - """ - Category can't be an integral, only a string. - You can check valid categories at: https://github.com/MarcoSa-2000/animegifs#category-list - """ - - def __init__(self, category, error="Category can't be an integral."): - self.category = category - self.error = error - super().__init__(self.error) - class CategoryError(Exception): """ - Not a valid category. Category must be a string. - You can check valid categories at: https://github.com/MarcoSa-2000/animegifs#category-list - """ - - def __init__(self, category, error="Not a valid category."): - self.category = category - self.error = error - super().__init__(self.error) - -class CategoryUnknown(Exception): - """ - This is a not handled error, probably the category type is neither an int nor a str. - Make sure to check category is a string and is a valid category. + Not a valid category. Category must be an existent category and a string. You can check valid categories at: https://github.com/MarcoSa-2000/animegifs#category-list """ @@ -32,48 +9,16 @@ def __init__(self, category, error="Not a valid category."): self.error = error super().__init__(self.error) -class MethodNotUpdated(Exception): - """ - The method for get the gif's