forked from WKS410/Client-Services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimeonegai.py
97 lines (84 loc) · 2.98 KB
/
animeonegai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import requests
class AnimeOnegaiClient:
"""
Cliente para la API de AnimeOnegai
"""
BASE_URL = 'https://animeonegai.com/api/v1/'
def __init__(self, api_key=None, secret_key=None):
"""
Inicializa el cliente con claves API y secretas (opcional)
"""
self.api_key = api_key
self.secret_key = secret_key
self.token = None
def _get_token(self):
"""
Genera un token de autenticación utilizando las claves API y secretas
"""
url = self.BASE_URL + 'authenticate'
data = {
'apiKey': self.api_key,
'secretKey': self.secret_key
}
response = requests.post(url, data=data)
if response.status_code == 200:
self.token = response.json()['token']
else:
print('Error al generar token:', response.json()['message'])
def _request(self, method, endpoint, data=None, params=None):
"""
Realiza una solicitud HTTP a la API con el token de autenticación (si es necesario)
"""
url = self.BASE_URL + endpoint
headers = {}
if self.token:
headers['Authorization'] = 'Bearer ' + self.token
response = requests.request(method, url, json=data, params=params, headers=headers)
if response.status_code == 401 and self.api_key and self.secret_key:
self._get_token()
headers['Authorization'] = 'Bearer ' + self.token
response = requests.request(method, url, json=data, params=params, headers=headers)
return response.json()
# Recursos de la API
def search_anime(self, query):
"""
Busca anime por nombre
"""
endpoint = 'anime'
params = {'search': query}
return self._request('GET', endpoint, params=params)
def get_anime(self, anime_id):
"""
Obtiene información detallada de un anime por su ID
"""
endpoint = f'anime/{anime_id}'
return self._request('GET', endpoint)
def get_anime_episodes(self, anime_id):
"""
Obtiene los episodios de un anime por su ID
"""
endpoint = f'anime/{anime_id}/episodes'
return self._request('GET', endpoint)
def get_anime_comments(self, anime_id):
"""
Obtiene los comentarios de un anime por su ID
"""
endpoint = f'anime/{anime_id}/comments'
return self._request('GET', endpoint)
def get_manga(self, manga_id):
"""
Obtiene información detallada de un manga por su ID
"""
endpoint = f'manga/{manga_id}'
return self._request('GET', endpoint)
def get_manga_chapters(self, manga_id):
"""
Obtiene los capítulos de un manga por su ID
"""
endpoint = f'manga/{manga_id}/chapters'
return self._request('GET', endpoint)
def get_manga_comments(self, manga_id):
"""
Obtiene los comentarios de un manga por su ID
"""
endpoint = f