8
8
Any ,
9
9
AsyncIterator ,
10
10
Dict ,
11
+ Optional ,
11
12
Type ,
12
13
TypeVar ,
13
14
)
14
15
15
16
from aiohttp import (
16
17
ClientResponse ,
17
- ClientResponseError ,
18
18
ClientSession ,
19
19
)
20
20
@@ -63,7 +63,7 @@ async def close(self) -> None:
63
63
64
64
async def get_doujin (self , doujin_id : int ) -> Dict [str , Any ]:
65
65
"""
66
- Get doujin raw data by id .
66
+ Get doujin raw data by ID .
67
67
68
68
Args:
69
69
doujin_id: ID of doujin.
@@ -73,16 +73,18 @@ async def get_doujin(self, doujin_id: int) -> Dict[str, Any]:
73
73
74
74
Raises:
75
75
DoujinDoesNotExistError: If doujin does not exit.
76
- ClientResponseError : Error from response.
76
+ HTTPError : Error from response.
77
77
"""
78
78
url = f"https://nhentai.net/api/gallery/{ doujin_id } "
79
79
80
80
try :
81
81
async with self ._request ("GET" , url = url ) as response :
82
82
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
86
88
else :
87
89
raise error
88
90
@@ -99,7 +101,7 @@ async def is_exist(self, doujin_id: int) -> bool:
99
101
Doujin is exists.
100
102
101
103
Raises:
102
- ClientResponseError : Error from response.
104
+ HTTPError : Error from response.
103
105
"""
104
106
try :
105
107
await self .get_doujin (doujin_id )
@@ -115,7 +117,7 @@ async def get_random_doujin(self) -> Dict[str, Any]:
115
117
Doujin raw data from responce.
116
118
117
119
Raises:
118
- ClientResponseError : Error from response.
120
+ HTTPError : Error from response.
119
121
"""
120
122
url = "https://nhentai.net/random/"
121
123
@@ -150,12 +152,12 @@ async def search(
150
152
Doujins raw data result from responce.
151
153
152
154
Raises:
153
- WrongPageError : If number of page is invalid.
155
+ ValueError : If number of page is invalid.
154
156
EmptyAPIResultError: If api result is empty.
155
- ClientResponseError : Error from response.
157
+ HTTPError : Error from response.
156
158
"""
157
159
if page < 1 :
158
- raise WrongPageError ("Page can not be less than 1" )
160
+ raise ValueError ("Page can not be less than 1" )
159
161
160
162
url = "https://nhentai.net/api/galleries/search"
161
163
params = {
@@ -193,15 +195,14 @@ async def search_by_tag(
193
195
Doujins raw data result from responce.
194
196
195
197
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.
198
199
EmptyAPIResultError: If api result is empty.
199
- ClientResponseError : Error from response.
200
+ HTTPError : Error from response.
200
201
"""
201
202
if page < 1 :
202
- raise WrongPageError ("Page can not be less than 1" )
203
+ raise ValueError ("Page can not be less than 1" )
203
204
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" )
205
206
206
207
url = "https://nhentai.net/api/galleries/tagged"
207
208
@@ -236,7 +237,7 @@ async def get_homepage_doujins(
236
237
237
238
Raises:
238
239
EmptyAPIResultError: If api result is empty.
239
- ClientResponseError : Error from response.
240
+ HTTPError : Error from response.
240
241
"""
241
242
url = "https://nhentai.net/api/galleries/all"
242
243
@@ -265,76 +266,43 @@ async def _request(
265
266
url ,
266
267
** kwargs ,
267
268
)
268
- response .raise_for_status ()
269
+
270
+ if response .status >= 400 :
271
+ response .release ()
272
+ raise HTTPError (response = response )
269
273
270
274
try :
271
275
yield response
272
276
finally :
273
277
await response .__aexit__ (None , None , None )
274
278
275
279
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."""
309
282
310
- # data = []
311
283
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.
317
287
288
+ Attributes:
289
+ responce: Responce object.
290
+ message: Message about error.
291
+ """
318
292
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 )
330
299
331
- class WrongTagError (Exception ):
332
- """Wrong tag."""
300
+ self .responce = response
333
301
334
302
335
- class DoujinDoesNotExistError (Exception ):
303
+ class DoujinDoesNotExistError (NHentaiError ):
336
304
"""Doujin does noe exist."""
337
305
338
306
339
- class EmptyAPIResultError (Exception ):
307
+ class EmptyAPIResultError (NHentaiError ):
340
308
"""API result is empty."""
0 commit comments