Skip to content

Commit

Permalink
fix: proper type data returns
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkaroid committed Dec 7, 2022
1 parent 6743028 commit 99af19b
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 146 deletions.
10 changes: 4 additions & 6 deletions booru/client/atfbooru.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import re
import aiohttp
from typing import Union
from ..utils.fetch import request, roll
from ..utils.constant import Api, better_object, parse_image_danbooru, get_hostname
Expand All @@ -17,7 +16,7 @@ class Atfbooru(object):
Search and gets images from atfbooru.
search_image : function
Gets images, image urls only from atfbooru.
Search and gets images from atfbooru, but only returns image.
"""

Expand Down Expand Up @@ -50,7 +49,6 @@ def __init__(self, api_key: str = "", user_id: str = ""):
----------
api_key : str
Your API Key which is accessible within your account options page
user_id : str
Your user ID, which is accessible on the account options/profile page.
"""
Expand All @@ -72,7 +70,7 @@ async def search(
page: int = 1,
random: bool = True,
gacha: bool = False,
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Search method
Expand Down Expand Up @@ -101,15 +99,15 @@ async def search(

elif block and re.findall(block, query):
raise ValueError(Booru.error_handling_sameval)

self.query = query
self.specs["tags"] = self.query
self.specs["limit"] = limit
self.specs["page"] = page

raw_data = await request(site=Booru.atfbooru, params_x=self.specs, block=block)
self.appended = Atfbooru.append_object(raw_data)

try:
if gacha:
return better_object(roll(self.appended))
Expand Down
27 changes: 6 additions & 21 deletions booru/client/behoimi.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
import re
import aiohttp
from typing import Union
from ..utils.fetch import request, roll
from ..utils.constant import Api, better_object, parse_image, get_hostname
from random import shuffle, randint
from random import shuffle

Booru = Api()

# these referer request just help you out to interacts with the API, not for displaying images


class Behoimi(object):
"""3d booru / Behoimi wrapper
"""3d booru / Behoimi Client
Methods
-------
search : function
Search and gets images from behoimi.
search_image : function
Gets images, image urls only from behoimi.
Search and gets images from behoimi, but only returns image.
"""

@staticmethod
async def mock(site: str, params: dict):
async with aiohttp.ClientSession(headers=Booru.behoimi_bypass) as session:
async with session.get(site, params=params) as resp:
return await resp.json()

@staticmethod
def append_object(raw_object: dict):
"""Extends new object to the raw dict
Expand Down Expand Up @@ -62,27 +55,22 @@ async def search(
page: int = 1,
random: bool = True,
gacha: bool = False,
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Search and gets images from behoimi.
Parameters
----------
query : str
The query to search for.
block : str
The tags to block.
limit : int
The limit of images to return.
page : int
The number of desired page
random : bool
Shuffle the whole dict, default is True.
gacha : bool
Get random single object, limit property will be ignored.
Expand All @@ -104,7 +92,7 @@ async def search(

raw_data = await request(site=Booru.behoimi, params_x=self.specs, block=block)
self.appended = Behoimi.append_object(raw_data)

try:
if gacha:
return better_object(roll(self.appended))
Expand All @@ -118,21 +106,18 @@ async def search(

async def search_image(
self, query: str, block="", limit: int = 100, page: int = 1
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Gets images, meant just image urls from behoimi.
Parameters
----------
query : str
The query to search for.
block : str
The tags to block.
limit : int
The limit of images to return.
page : int
The number of desired page
Expand Down
16 changes: 6 additions & 10 deletions booru/client/danbooru.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import re
import aiohttp
from typing import Union
from ..utils.fetch import request, roll
from ..utils.constant import Api, better_object, parse_image_danbooru, get_hostname
from random import shuffle, randint
from random import shuffle

Booru = Api()

Expand All @@ -17,7 +16,7 @@ class Danbooru(object):
Search and gets images from danbooru.
search_image : function
Gets images, image urls only from danbooru.
Search and gets images from danbooru, but only returns image.
"""

Expand Down Expand Up @@ -55,7 +54,7 @@ def __init__(self, api_key: str = "", login: str = ""):
Your user ID, which is accessible on the account options/profile page.
"""

if api_key =="" and login == "":
if api_key == "" and login == "":
self.api_key = None
self.login = None
self.specs = {}
Expand All @@ -64,8 +63,6 @@ def __init__(self, api_key: str = "", login: str = ""):
self.login = login
self.specs = {"api_key": self.api_key, "login": self.login}



async def search(
self,
query: str,
Expand All @@ -74,7 +71,7 @@ async def search(
page: int = 1,
random: bool = True,
gacha: bool = False,
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Search method
Expand Down Expand Up @@ -103,15 +100,15 @@ async def search(

elif block and re.findall(block, query):
raise ValueError(Booru.error_handling_sameval)

self.query = query
self.specs["tags"] = self.query
self.specs["limit"] = limit
self.specs["page"] = page

raw_data = await request(site=Booru.danbooru, params_x=self.specs, block=block)
self.appended = Danbooru.append_object(raw_data)

try:
if gacha:
return better_object(roll(self.appended))
Expand Down Expand Up @@ -144,7 +141,6 @@ async def search_image(
-------
dict
The json object (as string, you may need booru.resolve())
"""

if limit > 1000:
Expand Down
10 changes: 6 additions & 4 deletions booru/client/derpibooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Union
from ..utils.fetch import request, roll
from ..utils.constant import Api, better_object, parse_image, get_hostname
from random import shuffle, randint
from random import shuffle

Booru = Api()

Expand All @@ -16,7 +16,7 @@ class Derpibooru(object):
Search and gets images from derpibooru.
search_image : function
Gets images, image urls only from derpibooru.
Search and gets images from derpibooru, but only returns image.
"""

Expand Down Expand Up @@ -102,7 +102,7 @@ async def search(

raw_data = await request(site=Booru.derpibooru, params_x=self.specs, block="")
self.appended = Derpibooru.append_object(raw_data["images"])

try:
if gacha:
return better_object(roll(self.appended))
Expand All @@ -114,7 +114,9 @@ async def search(
except Exception as e:
raise Exception(f"Failed to get data: {e}")

async def search_image(self, query: str, limit: int = 100, page: int = 1) -> Union[aiohttp.ClientResponse, str]:
async def search_image(
self, query: str, limit: int = 100, page: int = 1
) -> Union[aiohttp.ClientResponse, str]:

"""Gets images, meant just image urls from derpibooru.
Expand Down
2 changes: 1 addition & 1 deletion booru/client/e621.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def search(

raw_data = await request(site=Booru.e621, params_x=self.specs, block="")
self.appended = E621.append_object(raw_data["posts"])

try:
if gacha:
return better_object(roll(self.appended))
Expand Down
3 changes: 1 addition & 2 deletions booru/client/e926.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def search(

raw_data = await request(site=Booru.e926, params_x=self.specs, block="")
self.appended = E926.append_object(raw_data["posts"])

try:
if gacha:
return better_object(roll(self.appended))
Expand Down Expand Up @@ -145,7 +145,6 @@ async def search_image(
if limit > 1000:
raise ValueError(Booru.error_handling_limit)


self.query = query
self.specs["tags"] = self.query
self.specs["limit"] = limit
Expand Down
9 changes: 6 additions & 3 deletions booru/client/furbooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,15 @@ async def search(

try:
if gacha:
return better_object(self.not_random[randint(0, len(self.not_random))])
return better_object(
self.not_random[randint(0, len(self.not_random))]
)
elif random:
return better_object(self.not_random)
else:
return better_object(Furbooru.append_object(self.final["images"]))
return better_object(
Furbooru.append_object(self.final["images"])
)

except Exception as e:
raise Exception(f"Failed to get data: {e}")
Expand Down Expand Up @@ -147,7 +151,6 @@ async def search_image(self, query: str, limit: int = 100, page: int = 1):
if limit > 1000:
raise ValueError(Booru.error_handling_limit)


self.query = query
self.specs["q"] = self.query
self.specs["per_page"] = limit
Expand Down
16 changes: 7 additions & 9 deletions booru/client/gelbooru.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import re
import aiohttp
from typing import Union
from ..utils.fetch import request, roll
from ..utils.constant import Api, better_object, parse_image, get_hostname
Expand All @@ -17,7 +16,7 @@ class Gelbooru(object):
Search and gets images from gelbooru.
search_image : function
Gets images, meant just image urls from gelbooru.
Search and gets images from gelbooru, but only returns image.
"""

Expand Down Expand Up @@ -49,10 +48,10 @@ def __init__(self, api_key: str = "", user_id: str = ""):
Parameters
----------
api_key : str
Your API Key which is accessible within your account options page
Your API Key (If possible)
user_id : str
Your user ID, which is accessible on the account options/profile page.
Your user ID (If possible)
"""

if api_key and user_id == "":
Expand All @@ -72,7 +71,7 @@ async def search(
page: int = 1,
random: bool = True,
gacha: bool = False,
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Search method
Expand Down Expand Up @@ -111,7 +110,7 @@ async def search(

raw_data = await request(site=Booru.gelbooru, params_x=self.specs, block=block)
self.appended = Gelbooru.append_object(raw_data)

try:
if gacha:
return better_object(roll(self.appended))
Expand All @@ -122,10 +121,10 @@ async def search(
return better_object(Gelbooru.append_object(self.appended))
except Exception as e:
raise Exception(f"Failed to get data: {e}")

async def search_image(
self, query: str, block: str = "", limit: int = 100, page: int = 1
) -> Union[aiohttp.ClientResponse, str]:
) -> Union[list, str, None]:

"""Parses image only
Expand All @@ -149,7 +148,6 @@ async def search_image(

if limit > 1000:
raise ValueError(Booru.error_handling_limit)

elif block and re.findall(block, query):
raise ValueError(Booru.error_handling_sameval)

Expand Down
Loading

0 comments on commit 99af19b

Please sign in to comment.