Skip to content

Commit

Permalink
feat: add separated booru
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkaroid committed Apr 7, 2022
1 parent 2d903b3 commit c213472
Show file tree
Hide file tree
Showing 22 changed files with 3,542 additions and 0 deletions.
21 changes: 21 additions & 0 deletions booru/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
Empty file added booru/client/__init__.py
Empty file.
199 changes: 199 additions & 0 deletions booru/client/atfbooru.py
Original file line number Diff line number Diff line change
@@ -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")
167 changes: 167 additions & 0 deletions booru/client/behoimi.py
Original file line number Diff line number Diff line change
@@ -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")
Loading

0 comments on commit c213472

Please sign in to comment.