Skip to content

Commit db6fd1b

Browse files
authored
Merge pull request #32 from LEv145:dev
v 1.0.11
2 parents f0155fa + 3b16b40 commit db6fd1b

File tree

5 files changed

+68
-123
lines changed

5 files changed

+68
-123
lines changed

aiontai/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
EmptyAPIResultError,
44
NHentaiAPI,
55
SortOptions,
6-
WrongPageError,
7-
WrongSearchError,
8-
WrongTagError,
96
)
107
from .client import (
118
NHentaiClient,
@@ -41,7 +38,4 @@
4138
"Tag",
4239
"TagType",
4340
"Title",
44-
"WrongPageError",
45-
"WrongSearchError",
46-
"WrongTagError",
4741
]

aiontai/api.py

Lines changed: 40 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
Any,
99
AsyncIterator,
1010
Dict,
11+
Optional,
1112
Type,
1213
TypeVar,
1314
)
1415

1516
from aiohttp import (
1617
ClientResponse,
17-
ClientResponseError,
1818
ClientSession,
1919
)
2020

@@ -63,7 +63,7 @@ async def close(self) -> None:
6363

6464
async def get_doujin(self, doujin_id: int) -> Dict[str, Any]:
6565
"""
66-
Get doujin raw data by id.
66+
Get doujin raw data by ID.
6767
6868
Args:
6969
doujin_id: ID of doujin.
@@ -73,16 +73,18 @@ async def get_doujin(self, doujin_id: int) -> Dict[str, Any]:
7373
7474
Raises:
7575
DoujinDoesNotExistError: If doujin does not exit.
76-
ClientResponseError: Error from response.
76+
HTTPError: Error from response.
7777
"""
7878
url = f"https://nhentai.net/api/gallery/{doujin_id}"
7979

8080
try:
8181
async with self._request("GET", url=url) as response:
8282
json: Dict[str, Any] = await response.json()
83-
except ClientResponseError as error:
84-
if error.status == 404:
85-
raise DoujinDoesNotExistError("That doujin does not exist.") from error
83+
except HTTPError as error:
84+
if error.responce.status == 404:
85+
raise DoujinDoesNotExistError(
86+
"That doujin does not exist.",
87+
) from error
8688
else:
8789
raise error
8890

@@ -99,7 +101,7 @@ async def is_exist(self, doujin_id: int) -> bool:
99101
Doujin is exists.
100102
101103
Raises:
102-
ClientResponseError: Error from response.
104+
HTTPError: Error from response.
103105
"""
104106
try:
105107
await self.get_doujin(doujin_id)
@@ -115,7 +117,7 @@ async def get_random_doujin(self) -> Dict[str, Any]:
115117
Doujin raw data from responce.
116118
117119
Raises:
118-
ClientResponseError: Error from response.
120+
HTTPError: Error from response.
119121
"""
120122
url = "https://nhentai.net/random/"
121123

@@ -150,12 +152,12 @@ async def search(
150152
Doujins raw data result from responce.
151153
152154
Raises:
153-
WrongPageError: If number of page is invalid.
155+
ValueError: If number of page is invalid.
154156
EmptyAPIResultError: If api result is empty.
155-
ClientResponseError: Error from response.
157+
HTTPError: Error from response.
156158
"""
157159
if page < 1:
158-
raise WrongPageError("Page can not be less than 1")
160+
raise ValueError("Page can not be less than 1")
159161

160162
url = "https://nhentai.net/api/galleries/search"
161163
params = {
@@ -193,15 +195,14 @@ async def search_by_tag(
193195
Doujins raw data result from responce.
194196
195197
Raises:
196-
WrongPageError: If number of page is invalid.
197-
WrongTagError: If tag ID is invalid.
198+
ValueError: If number of page is invalid or tag ID is invalid.
198199
EmptyAPIResultError: If api result is empty.
199-
ClientResponseError: Error from response.
200+
HTTPError: Error from response.
200201
"""
201202
if page < 1:
202-
raise WrongPageError("Page can not be less than 1")
203+
raise ValueError("Page can not be less than 1")
203204
elif tag_id < 1:
204-
raise WrongTagError("Tag id can not be less than 1")
205+
raise ValueError("Tag id can not be less than 1")
205206

206207
url = "https://nhentai.net/api/galleries/tagged"
207208

@@ -236,7 +237,7 @@ async def get_homepage_doujins(
236237
237238
Raises:
238239
EmptyAPIResultError: If api result is empty.
239-
ClientResponseError: Error from response.
240+
HTTPError: Error from response.
240241
"""
241242
url = "https://nhentai.net/api/galleries/all"
242243

@@ -265,76 +266,43 @@ async def _request(
265266
url,
266267
**kwargs,
267268
)
268-
response.raise_for_status()
269+
270+
if response.status >= 400:
271+
response.release()
272+
raise HTTPError(response=response)
269273

270274
try:
271275
yield response
272276
finally:
273277
await response.__aexit__(None, None, None)
274278

275279

276-
# TODO
277-
# async def search_all_by_tags(self, tag_ids: list) -> List[dict]:
278-
# """Method for search doujins by tags.
279-
# Args:
280-
# :tag_ids list: List of tags
281-
282-
# Returns:
283-
# List of doujins JSON
284-
285-
# Raises:
286-
# IsNotValidSort if sort is not a member of SortOptions.
287-
# WrongPage if page less than 1.
288-
# """
289-
290-
# async def get_limit(tag_id: int) -> List[dict]:
291-
# utils.is_valid_search_by_tag_parameters(tag_id, 1, "date")
292-
293-
# url = f"{config.api_gallery_url}/tagged"
294-
# params = {
295-
# "tag_id": tag_id,
296-
# "page": 1,
297-
# "sort_by": "date"
298-
# }
299-
300-
# result = await self._get_requests(url, params=params)
301-
# if result:
302-
# return result["num_pages"]
303-
# else:
304-
# raise errors.WrongTag("There is no tag with given tag_id")
305-
306-
307-
# limits = await asyncio.gather(*[get_limit(tag_id) for tag_id in tag_ids])
308-
# limits = zip(tag_ids, limits)
280+
class NHentaiError(Exception):
281+
"""Base NHentai api error."""
309282

310-
# data = []
311283

312-
# for args in limits:
313-
# limits = args[1]
314-
# tag_ids = args[0]
315-
# for i in range(1, limits+1):
316-
# data.append((tag_ids, i))
284+
class HTTPError(NHentaiError):
285+
"""
286+
Error from responce.
317287
288+
Attributes:
289+
responce: Responce object.
290+
message: Message about error.
291+
"""
318292

319-
# pages = await asyncio.gather(*[self.search_by_tag(*args) for args in data])
320-
# return [doujin for page in pages for doujin in page]
321-
322-
323-
class WrongPageError(Exception):
324-
"""Wrong page."""
325-
326-
327-
class WrongSearchError(Exception):
328-
"""Wrong search."""
329-
293+
def __init__(
294+
self,
295+
response: ClientResponse,
296+
message: Optional[str] = None,
297+
) -> None:
298+
super().__init__(message)
330299

331-
class WrongTagError(Exception):
332-
"""Wrong tag."""
300+
self.responce = response
333301

334302

335-
class DoujinDoesNotExistError(Exception):
303+
class DoujinDoesNotExistError(NHentaiError):
336304
"""Doujin does noe exist."""
337305

338306

339-
class EmptyAPIResultError(Exception):
307+
class EmptyAPIResultError(NHentaiError):
340308
"""API result is empty."""

aiontai/client.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def close(self) -> None:
6060

6161
async def get_doujin(self, doujin_id: int) -> Doujin:
6262
"""
63-
Get doujin model by id.
63+
Get doujin model by ID.
6464
6565
Args:
6666
doujin_id: ID of doujin.
@@ -70,7 +70,7 @@ async def get_doujin(self, doujin_id: int) -> Doujin:
7070
7171
Raises:
7272
DoujinDoesNotExistError: If doujin does not exit.
73-
ClientResponseError: Error from response.
73+
HTTPError: Error from response.
7474
"""
7575
raw_data = await self.api.get_doujin(doujin_id)
7676

@@ -87,7 +87,7 @@ async def is_exist(self, doujin_id: int) -> bool:
8787
Doujin is exists.
8888
8989
Raises:
90-
ClientResponseError: Error from response.
90+
HTTPError: Error from response.
9191
"""
9292
raw_data = await self.api.is_exist(doujin_id)
9393

@@ -101,7 +101,7 @@ async def get_random_doujin(self) -> Doujin:
101101
Doujin model.
102102
103103
Raises:
104-
ClientResponseError: Error from response.
104+
HTTPError: Error from response.
105105
"""
106106
raw_data = await self.api.get_random_doujin()
107107

@@ -128,9 +128,9 @@ async def search(
128128
Doujins result model.
129129
130130
Raises:
131-
WrongPageError: If number of page is invalid.
131+
ValueError: If number of page is invalid.
132132
EmptyAPIResultError: If api result is empty.
133-
ClientResponseError: Error from response.
133+
HTTPError: Error from response.
134134
"""
135135
result = await self.api.search(
136136
query=query,
@@ -161,10 +161,9 @@ async def search_by_tag(
161161
Doujins result model.
162162
163163
Raises:
164-
WrongPageError: If number of page is invalid.
165-
WrongTagError: If tag ID is invalid.
164+
ValueError: If number of page is invalid or tag ID is invalid.
166165
EmptyAPIResultError: If api result is empty.
167-
ClientResponseError: Error from response.
166+
HTTPError: Error from response.
168167
"""
169168
result = await self.api.search_by_tag(
170169
tag_id=tag_id,
@@ -191,7 +190,7 @@ async def get_homepage_doujins(
191190
192191
Raises:
193192
EmptyAPIResultError: If api result is empty.
194-
ClientResponseError: Error from response.
193+
HTTPError: Error from response.
195194
"""
196195
result = await self.api.get_homepage_doujins(
197196
page=page,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "aiontai"
3-
version = "1.0.10"
3+
version = "1.0.11"
44
description = "Async wrapper for nhentai API"
55
authors = ["LEv145"]
66
license = "MIT"

0 commit comments

Comments
 (0)