Skip to content

Commit

Permalink
1.0.0 - Now an API wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoSa-2000 committed Mar 26, 2024
1 parent f83ea7b commit 99ca0c5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 162 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
<img alt="GitHub watchers" src="https://img.shields.io/github/watchers/MarcoSa-2000/animegifs?style=social">
</p>

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`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
98 changes: 44 additions & 54 deletions animegifs/animegifs.py
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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:
Expand All @@ -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:
"""
Expand All @@ -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:
"""
Expand All @@ -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
69 changes: 7 additions & 62 deletions animegifs/distutils/errors.py
Original file line number Diff line number Diff line change
@@ -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
"""

Expand All @@ -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 <title, mal link, mal id> 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)
38 changes: 0 additions & 38 deletions animegifs/distutils/gifs.py

This file was deleted.

7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
long_description = "\n" + fh.read()

LONG_DESCRIPTION = long_description
VERSION = '0.7.2'
VERSION = '1.0.0'

setup(
name='animegifs',
Expand All @@ -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=[
Expand Down

0 comments on commit 99ca0c5

Please sign in to comment.