diff --git a/README.md b/README.md index b57886c..febe575 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,10 @@ GitHub watchers

-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 is not yet supported for that gif url. - Usually means is just in work in progress and will be available soon. - """ - - def __init__(self, gif, error="Method not yet available for this gif."): - self.gif = gif - self.error = error - super().__init__(self.error) - -class AnimeNotFound(Exception): +class CommonError(Exception): """ - Raised if the anime's id is either invalid or has been removed or relocated. - Check firstly if myanimelist.net is working correctly, if confirmed, + This is yet a not handled error. + Make sure you input the gif url for get methods. + Make sure gif url is a string. + Check if myanimelist.net is working correctly. please raise an issue in https://github.com/MarcoSa-2000/animegifs/issues. """ - def __init__(self, gif, error="Anime's ID is either invalid, removed or MyAnimeList is currently down."): + def __init__(self, gif, error="Gif url is not valid or anime's ID is either removed or MyAnimeList is currently down."): self.gif = gif self.error = error super().__init__(self.error) - -class AuthTimeout(Exception): - """ - Authentication request timed out. Probably status error 504 on server side. - Check your connection too. - """ - - def __init__(self, exc, error="Authentication request timed out."): - self.exc = exc - self.error = error - super().__init__(self.error) - -class AuthError(Exception): - """ - Authentication request returned an error. Probably status error 4xx. - It will be resolved soon, if it persists, - issue an issue on https://github.com/MarcoSa-2000/animegifs/issues. - """ - - def __init__(self, exc, error="Authentication request returned an error."): - self.exc = exc - self.error = error - super().__init__(self.error) diff --git a/animegifs/distutils/gifs.py b/animegifs/distutils/gifs.py deleted file mode 100644 index fe283c0..0000000 --- a/animegifs/distutils/gifs.py +++ /dev/null @@ -1,38 +0,0 @@ -import json -import requests -import jwt -from animegifs.distutils import errors - -global TOKEN -TOKEN = None - -def authentication(): - try: - response = requests.post("https://enkidu-app-5a3qq2fqya-uc.a.run.app/key1", timeout=100) - except requests.exceptions.Timeout: - try: - response = requests.post("https://enkidu-app-5a3qq2fqya-uc.a.run.app/key2", timeout=25) - except requests.exceptions.Timeout as exc: - raise errors.AuthTimeout(exc) - if response.status_code == 200: - auth_key = response.json()['key'] - else: - exc = response.status_code - raise errors.AuthError(exc) - return auth_key - -def access(): - global TOKEN - if TOKEN: - data = jwt.decode(TOKEN, "enkidu-key", algorithms="HS512") - data = list(data.keys()) - data = requests.get(data[0]).content - data = json.loads(data) - else: - TOKEN = authentication() - data = jwt.decode(TOKEN, "enkidu-key", algorithms="HS512") - data = list(data.keys()) - data = requests.get(data[0]).content - data = json.loads(data) - - return data diff --git a/setup.py b/setup.py index d1b2e21..a09eb38 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ long_description = "\n" + fh.read() LONG_DESCRIPTION = long_description -VERSION = '0.7.2' +VERSION = '1.0.0' setup( name='animegifs', @@ -23,9 +23,8 @@ license='MIT', url="https://github.com/MarcoSa-2000/animegifs", install_requires=[ - 'requests==2.31.0', - 'mal-api==0.5.3', - 'PyJWT>=2.4.0,<=2.8.0' + 'requests~=2.31.0', + 'mal-api==0.5.3' ], python_requires='>=3.8', classifiers=[