diff --git a/booru/__init__.py b/booru/__init__.py new file mode 100644 index 0000000..2b2ee25 --- /dev/null +++ b/booru/__init__.py @@ -0,0 +1,21 @@ +__version__ = "1.0.9" +from .client.gelbooru import Gelbooru +from .client.rule34 import Rule34 +from .client.tbib import Tbib +from .client.safebooru import Safebooru +from .client.xbooru import Xbooru +from .client.realbooru import Realbooru +from .client.hypnohub import Hypnohub +from .client.danbooru import Danbooru +from .client.atfbooru import Atfbooru +from .client.yandere import Yandere +from .client.konachan import Konachan +from .client.konachan_net import Konachan_Net +from .client.lolibooru import Lolibooru +from .client.e621 import E621 +from .client.e926 import E926 +from .client.derpibooru import Derpibooru +from .client.furbooru import Furbooru +from .client.behoimi import Behoimi +from .client.paheal import Paheal +from .utils import * diff --git a/booru/client/__init__.py b/booru/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/booru/client/atfbooru.py b/booru/client/atfbooru.py new file mode 100644 index 0000000..b3961f0 --- /dev/null +++ b/booru/client/atfbooru.py @@ -0,0 +1,199 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Atfbooru(object): + """Atfbooru wrapper + + Methods + ------- + search : function + Search and gets images from atfbooru. + + get_image : function + Gets images, image urls only from atfbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by atfbooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.atfbooru)}/posts/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes atfbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from atfbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by atfbooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.atfbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Atfbooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + if random: + return better_object(self.not_random) + + else: + return better_object(Atfbooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from atfbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by atfbooru. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.atfbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/behoimi.py b/booru/client/behoimi.py new file mode 100644 index 0000000..f3935d2 --- /dev/null +++ b/booru/client/behoimi.py @@ -0,0 +1,167 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +# note: if your application rely on displaying images, you should implement a 'sync' stuff to behoimi.org itself +# these referer request just help you out to interacts with the API, not for displaying images + + +class Behoimi(object): + """3d booru / Behoimi wrapper + + Methods + ------- + search : function + Search and gets images from behoimi. + + get_image : function + Gets images, image urls only from behoimi. + + """ + + @staticmethod + def mock(site: str, params: dict): + bypass = requests.get(site, params, headers=Booru.behoimi_bypass) + return bypass + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by behoimi. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.behoimi)}/post/show/{raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from behoimi. + + Parameters + ---------- + query : str + The query to search for. + + 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. + + Returns + ------- + dict + The json object returned by behoimi. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = Behoimi.mock(Booru.behoimi, params=self.specs) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Behoimi.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Behoimi.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from behoimi. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by behoimi. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = Behoimi.mock(Booru.behoimi, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/danbooru.py b/booru/client/danbooru.py new file mode 100644 index 0000000..406701e --- /dev/null +++ b/booru/client/danbooru.py @@ -0,0 +1,200 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Danbooru(object): + """Danbooru wrapper + + Methods + ------- + search : function + Search and gets images from danbooru. + + get_image : function + Gets images, image urls only from danbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by danbooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.danbooru)}/posts/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes danbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from danbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by danbooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.danbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Danbooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Danbooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from danbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by danbooru. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.danbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/derpibooru.py b/booru/client/derpibooru.py new file mode 100644 index 0000000..db73fc2 --- /dev/null +++ b/booru/client/derpibooru.py @@ -0,0 +1,173 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Derpibooru(object): + """derpibooru wrapper + + Methods + ------- + search : function + Search and gets images from derpibooru. + + get_image : function + Gets images, image urls only from derpibooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by derpibooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.derpibooru)}/images/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, key: str = ""): + """Initializes derpibooru. + + Parameters + ---------- + key : str + An optional authentication token. If omitted, no user will be authenticated. + """ + + if key: + self.key = None + else: + self.key = key + + self.specs = {"key": self.key} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from derpibooru. + + Parameters + ---------- + query : str + The query to search for. + + + 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. + + Returns + ------- + dict + The json object returned by derpibooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["q"] = str(self.query) + self.specs["per_page"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.derpibooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final["images"]: + raise ValueError(Booru.error_handling_null) + + self.not_random = Derpibooru.append_obj(self.final["images"]) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Derpibooru.append_obj(self.final["images"])) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from derpibooru. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by derpibooru. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["q"] = str(self.query) + self.specs["per_page"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.derpibooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = [ + i["representations"]["full"] for i in self.final["images"] + ] + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/e621.py b/booru/client/e621.py new file mode 100644 index 0000000..3f19feb --- /dev/null +++ b/booru/client/e621.py @@ -0,0 +1,199 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class E621(object): + """e621 wrapper + + Methods + ------- + search : function + Search and gets images from e621. + + get_image : function + Gets images, meant just image urls from e621. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by e621. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.e621)}/posts/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes e621. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from e621. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want 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. + + Returns + ------- + dict + The json object returned by e621. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.e621, params=self.specs, headers=Booru.headers) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final["posts"]: + raise ValueError(Booru.error_handling_null) + + self.not_random = E621.append_obj(self.final["posts"]) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(E621.append_obj(self.final["posts"])) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from e621. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get( + Booru.e621, params=self.specs, headers=Booru.headers + ) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final["posts"]) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/e926.py b/booru/client/e926.py new file mode 100644 index 0000000..647e238 --- /dev/null +++ b/booru/client/e926.py @@ -0,0 +1,199 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class E926(object): + """E926 wrapper + + Methods + ------- + search : function + Search and gets images from e926. + + get_image : function + Gets images, meant just image urls from e926. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by e926. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.e926)}/posts/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes e926. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from e926. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want 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. + + Returns + ------- + dict + The json object returned by e926. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.e926, params=self.specs, headers=Booru.headers) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final["posts"]: + raise ValueError(Booru.error_handling_null) + + self.not_random = E926.append_obj(self.final["posts"]) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(E926.append_obj(self.final["posts"])) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from e926. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get( + Booru.e926, params=self.specs, headers=Booru.headers + ) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final["posts"]) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/furbooru.py b/booru/client/furbooru.py new file mode 100644 index 0000000..f695795 --- /dev/null +++ b/booru/client/furbooru.py @@ -0,0 +1,173 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Furbooru(object): + """furbooru wrapper + + Methods + ------- + search : function + Search and gets images from furbooru. + + get_image : function + Gets images, image urls only from furbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by furbooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.furbooru)}/images/{raw_object[i]['id']}" + + return raw_object + + def __init__(self, key: str = ""): + """Initializes furbooru. + + Parameters + ---------- + key : str + An optional authentication token. If omitted, no user will be authenticated. + """ + + if key: + self.key = None + else: + self.key = key + + self.specs = {"key": self.key} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from furbooru. + + Parameters + ---------- + query : str + The query to search for. + + + 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. + + Returns + ------- + dict + The json object returned by furbooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["q"] = str(self.query) + self.specs["per_page"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.furbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final["images"]: + raise ValueError(Booru.error_handling_null) + + self.not_random = Furbooru.append_obj(self.final["images"]) + shuffle(self.not_random) + + try: + if gacha: + 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_obj(self.final["images"])) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from furbooru. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by furbooru. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["q"] = str(self.query) + self.specs["per_page"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.furbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = [ + i["representations"]["full"] for i in self.final["images"] + ] + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/gelbooru.py b/booru/client/gelbooru.py new file mode 100644 index 0000000..3224a5b --- /dev/null +++ b/booru/client/gelbooru.py @@ -0,0 +1,203 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Gelbooru(object): + """Gelbooru wrapper + + Methods + ------- + search : function + Search and gets images from gelbooru. + + get_image : function + Gets images, meant just image urls from gelbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by gelbooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.gelbooru)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes gelbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from gelbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by gelbooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.gelbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if "post" not in self.final or not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Gelbooru.append_obj(self.final["post"]) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Gelbooru.append_obj(self.final["post"])) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from gelbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.gelbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if "post" not in self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/hypnohub.py b/booru/client/hypnohub.py new file mode 100644 index 0000000..f2299bb --- /dev/null +++ b/booru/client/hypnohub.py @@ -0,0 +1,201 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Hypnohub(object): + """Hypnohub wrapper + + Methods + ------- + search : function + Search and gets images from hypnohub. + + get_image : function + Gets images, image urls only from hypnohub. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by hypnohub. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.hypnohub)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes danbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from hypnohub. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by hypnohub. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.hypnohub, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Hypnohub.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Hypnohub.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from hypnohub. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by hypnohub. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.hypnohub, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/konachan.py b/booru/client/konachan.py new file mode 100644 index 0000000..701d79a --- /dev/null +++ b/booru/client/konachan.py @@ -0,0 +1,157 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Konachan(object): + """Konachan wrapper + + Methods + ------- + search : function + Search and gets images from konachan. + + get_image : function + Gets images, image urls only from konachan. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by konachan. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.konachan)}/post/show/{raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from konachan. + + Parameters + ---------- + query : str + The query to search for. + + 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. + + Returns + ------- + dict + The json object returned by konachan. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.konachan, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Konachan.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Konachan.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from konachan. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by konachan. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.konachan, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/konachan_net.py b/booru/client/konachan_net.py new file mode 100644 index 0000000..a12d8e2 --- /dev/null +++ b/booru/client/konachan_net.py @@ -0,0 +1,157 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Konachan_Net(object): + """konachan net wrapper (SFW) + + Methods + ------- + search : function + Search and gets images from konachan net. + + get_image : function + Gets images, image urls only from konachan net. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by konachan net. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.konachan_net)}/post/show/{raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from konachan net. + + Parameters + ---------- + query : str + The query to search for. + + 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. + + Returns + ------- + dict + The json object returned by konachan net. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.konachan_net, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Konachan_Net.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Konachan_Net.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from konachan net. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by konachan net. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.konachan_net, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/lolibooru.py b/booru/client/lolibooru.py new file mode 100644 index 0000000..a035795 --- /dev/null +++ b/booru/client/lolibooru.py @@ -0,0 +1,158 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + + +Booru = Api() + + +class Lolibooru(object): + """lolibooru wrapper + + Methods + ------- + search : function + Search and gets images from lolibooru. + + get_image : function + Gets images, image urls only from lolibooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by lolibooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.lolibooru)}/post/show/{raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from lolibooru. + + Parameters + ---------- + query : str + The query to search for. + + 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. + + Returns + ------- + dict + The json object returned by lolibooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.lolibooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Lolibooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Lolibooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from lolibooru. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by lolibooru. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.lolibooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/paheal.py b/booru/client/paheal.py new file mode 100644 index 0000000..e49fadd --- /dev/null +++ b/booru/client/paheal.py @@ -0,0 +1,167 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, get_hostname, read_json +from random import shuffle, randint +from xmltodict import parse + +Booru = Api() + + +class Paheal(object): + """paheal wrapper + + Methods + ------- + search : function + Search and gets images from paheal. + + get_image : function + Gets images, meant just image urls from paheal. + + """ + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes paheal. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from paheal. + + Parameters + ---------- + query : str + The tags to search for. + + 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. + + Returns + ------- + dict + The json object returned by paheal. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.tags = query + + self.specs["tags"] = str(self.tags) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.paheal, params=self.specs) + data_dict = parse(self.data.text) + unsolved = json.dumps(data_dict) + self.final = json.loads(unsolved, encoding="utf-8") + + if "tag" not in self.final["posts"]: + raise ValueError(Booru.error_handling_null) + + self.not_random = self.final["posts"]["tag"] + shuffle(self.not_random) + + try: + if gacha: + return better_object( + self.final["posts"]["tag"][ + randint(0, len(self.final["posts"]["tag"])) + ] + ) + + elif random: + return better_object(self.final["posts"]["tag"]) + + else: + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from paheal. + + Parameters + ---------- + query : str + The tags to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.tags = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.paheal, params=self.specs) + data_dict = parse(self.data.text) + unsolved = json.dumps(data_dict) + self.final = json.loads(unsolved, encoding="utf-8") + + abc_kontol = self.final["posts"]["tag"] + ## extract all image urls + self.image_urls = [] + for i in abc_kontol: # paheal emang ngentot + self.image_urls.append(i["@file_url"]) + + shuffle(self.image_urls) + return better_object(self.image_urls) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") diff --git a/booru/client/realbooru.py b/booru/client/realbooru.py new file mode 100644 index 0000000..55065e3 --- /dev/null +++ b/booru/client/realbooru.py @@ -0,0 +1,214 @@ +import requests +import json +import re +from random import shuffle, randint +from ..utils.parser import Api, better_object, parse_image, get_hostname + +Booru = Api() + + +class Realbooru(object): + """Realbooru wrapper + + Methods + ------- + search : function + Search and gets images from realbooru. + + get_image : function + Gets images, image urls only from realbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by realbooru. + + Returns + ------- + str + The image url. + """ + for i in range(len(raw_object)): + if raw_object[i]["directory"] and "id" in raw_object[i]: + raw_object[i][ + "file_url" + ] = f"{get_hostname(Booru.realbooru)}/images/{raw_object[i]['directory']}/{raw_object[i]['image']}" + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.realbooru)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + else: + raw_object[i]["file_url"] = Booru.error_handling_cantparse + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.realbooru)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes realbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images with raw data from realbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by realbooru. + + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.realbooru, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Realbooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Realbooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from realbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.realbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(Realbooru.append_obj(self.final)) + self.bad_array = [ + x for x in self.not_random if x != Booru.error_handling_cantparse + ] + shuffle(self.bad_array) + return better_object(self.bad_array) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") diff --git a/booru/client/rule34.py b/booru/client/rule34.py new file mode 100644 index 0000000..e07a922 --- /dev/null +++ b/booru/client/rule34.py @@ -0,0 +1,184 @@ +import requests +import json +import re +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Rule34(object): + """Rule34 wrapper + + Methods + ------- + search : function + Search and gets images from rule34. + + get_image : function + Gets images, image urls only from rule34. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by rule34. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.rule34)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from rule34. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by rule34. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.rule34, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Rule34.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Rule34.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from rule34. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by rule34. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.rule34, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + # print(self.data.elapsed.total_seconds()) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/safebooru.py b/booru/client/safebooru.py new file mode 100644 index 0000000..c40f87d --- /dev/null +++ b/booru/client/safebooru.py @@ -0,0 +1,204 @@ +import requests +import json +import re +from random import shuffle, randint +from ..utils.parser import Api, better_object, parse_image, get_hostname + +Booru = Api() + + +class Safebooru(object): + """Safebooru wrapper + + Methods + ------- + search : function + Search and gets images from safebooru. + + get_image : function + Gets images, image urls only from safebooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by safebooru. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "file_url" + ] = f"{get_hostname(Booru.safebooru)}/images/{raw_object[i]['directory']}/{raw_object[i]['image']}" + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.safebooru)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes safebooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from safebooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by safebooru. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.safebooru, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Safebooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Safebooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from safebooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.safebooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(Safebooru.append_obj(self.final)) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/tbib.py b/booru/client/tbib.py new file mode 100644 index 0000000..df15112 --- /dev/null +++ b/booru/client/tbib.py @@ -0,0 +1,205 @@ +import requests +import json +import re +from random import shuffle, randint +from ..utils.parser import Api, better_object, parse_image, get_hostname + +Booru = Api() + + +class Tbib(object): + """Tbib wrapper + + Methods + ------- + search : function + Search and gets images from tbib. + + get_image : function + Gets images, image urls only from tbib. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by tbib. + + Returns + ------- + str + The image url. + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "file_url" + ] = f"{get_hostname(Booru.tbib)}/images/{raw_object[i]['directory']}/{raw_object[i]['image']}" + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.tbib)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes tbib. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images with raw data from tbib. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by tbib. + + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.tbib, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Tbib.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Tbib.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from tbib. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.tbib, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(Tbib.append_obj(self.final)) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/xbooru.py b/booru/client/xbooru.py new file mode 100644 index 0000000..9524776 --- /dev/null +++ b/booru/client/xbooru.py @@ -0,0 +1,205 @@ +import requests +import json +import re +from random import shuffle, randint +from ..utils.parser import Api, better_object, parse_image, get_hostname + +Booru = Api() + + +class Xbooru(object): + """Xbooru wrapper + + Methods + ------- + search : function + Search and gets images from xbooru. + + get_image : function + Gets images, image urls only from xbooru. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by xbooru. + + Returns + ------- + str + The image url. + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "file_url" + ] = f"{get_hostname(Booru.xbooru)}/{raw_object[i]['directory']}/{raw_object[i]['image']}" + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.xbooru)}/index.php?page=post&s=view&id={raw_object[i]['id']}" + + return raw_object + + def __init__(self, api_key: str = "", user_id: str = ""): + """Initializes xbooru. + + Parameters + ---------- + 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. + """ + + if api_key and user_id == "": + self.api_key = None + self.user_id = None + else: + self.api_key = api_key + self.user_id = user_id + + self.specs = {"api_key": self.api_key, "user_id": self.user_id} + + async def search( + self, + query: str, + block: str = "", + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images with raw data from xbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + 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. + + Returns + ------- + dict + The json object returned by xbooru. + + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + self.data = requests.get(Booru.xbooru, params=self.specs) + + if not self.data.text: + raise ValueError(Booru.error_handling_null) + + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = Xbooru.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Xbooru.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image( + self, query: str, block: str = "", limit: int = 100, page: int = 1 + ): + + """Gets images, meant just image urls from xbooru. + + Parameters + ---------- + query : str + The query to search for. + + block : str + The disgusting query you want to block, + e.g: you want to search 'erza_scarlet' but dont want to gets furry, fill in 'furry' + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + list + The list of image urls. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + if block and re.findall(block, query): + raise ValueError(Booru.error_handling_sameval) + + if block != "": + self.query = f"{query} -{block}*" + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["pid"] = str(page) + self.specs["json"] = "1" + + try: + self.data = requests.get(Booru.xbooru, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(Xbooru.append_obj(self.final)) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/client/yandere.py b/booru/client/yandere.py new file mode 100644 index 0000000..548e532 --- /dev/null +++ b/booru/client/yandere.py @@ -0,0 +1,156 @@ +import requests +import json +from ..utils.parser import Api, better_object, parse_image, get_hostname +from random import shuffle, randint + +Booru = Api() + + +class Yandere(object): + """Yandere wrapper + + Methods + ------- + search : function + Search and gets images from yandere. + + get_image : function + Gets images, image urls only from yandere. + + """ + + @staticmethod + def append_obj(raw_object: dict): + """Extends new object to the raw dict + + Parameters + ---------- + raw_object : dict + The raw object returned by yandere. + + Returns + ------- + str + The new value of the raw object + """ + for i in range(len(raw_object)): + if "id" in raw_object[i]: + raw_object[i][ + "post_url" + ] = f"{get_hostname(Booru.yandere)}/post/show/{raw_object[i]['id']}" + + return raw_object + + def __init__(self): + self.specs = {} + + async def search( + self, + query: str, + limit: int = 100, + page: int = 1, + random: bool = True, + gacha: bool = False, + ): + + """Search and gets images from yandere. + + Parameters + ---------- + query : str + The query to search for. + + 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. + + Returns + ------- + dict + The json object returned by yandere. + """ + if gacha: + limit = 100 + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + self.data = requests.get(Booru.yandere, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + if not self.final: + raise ValueError(Booru.error_handling_null) + + self.not_random = Yandere.append_obj(self.final) + shuffle(self.not_random) + + try: + if gacha: + return better_object(self.not_random[randint(0, len(self.not_random))]) + + elif random: + return better_object(self.not_random) + + else: + return better_object(Yandere.append_obj(self.final)) + + except Exception as e: + raise ValueError(f"Failed to get data: {e}") + + async def get_image(self, query: str, limit: int = 100, page: int = 1): + + """Gets images, meant just image urls from yandere. + + Parameters + ---------- + query : str + The query to search for. + + limit : int + The limit of images to return. + + page : int + The number of desired page + + Returns + ------- + dict + The json object returned by yandere. + + """ + + if limit > 100: + raise ValueError(Booru.error_handling_limit) + + else: + self.query = query + + self.specs["tags"] = str(self.query) + self.specs["limit"] = str(limit) + self.specs["page"] = str(page) + + try: + self.data = requests.get(Booru.yandere, params=self.specs) + self.final = json.loads(better_object(self.data.json()), encoding="utf-8") + + self.not_random = parse_image(self.final) + shuffle(self.not_random) + return better_object(self.not_random) + + except: + raise ValueError(f"Failed to get data") diff --git a/booru/utils/__init__.py b/booru/utils/__init__.py new file mode 100644 index 0000000..e69de29