Skip to content

Commit

Permalink
feat(parameter)!:
Browse files Browse the repository at this point in the history
add base_url as a parameter in multiple classes initialization (close #115)
  • Loading branch information
NekoAria committed Apr 17, 2024
1 parent 87cc107 commit f5ff3ab
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 33 deletions.
17 changes: 12 additions & 5 deletions PicImageSearch/ascii2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Ascii2D(HandOver):
Used for performing reverse image searches using Ascii2D service.
Attributes:
base_url: The base URL for Ascii2D searches.
bovw: A flag to use feature search over color combination search.
Note:
Expand All @@ -19,14 +20,21 @@ class Ascii2D(HandOver):
Feature search may not yield accurate results with significantly different images.
"""

def __init__(self, bovw: bool = False, **request_kwargs: Any):
def __init__(
self,
base_url: str = "https://ascii2d.net",
bovw: bool = False,
**request_kwargs: Any,
):
"""Initializes an Ascii2D API client with specified configurations.
Args:
base_url: The base URL for Ascii2D searches.
bovw: If True, use feature search; otherwise, use color combination search.
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.base_url = f"{base_url}/search"
self.bovw = bovw

async def search(
Expand All @@ -48,13 +56,12 @@ async def search(
Raises:
ValueError: If neither 'url' nor 'file' is provided.
"""
_url = f"{self.base_url}/uri" if url else f"{self.base_url}/file"
if url:
ascii2d_url = "https://ascii2d.net/search/uri"
resp = await self.post(ascii2d_url, data={"uri": url})
resp = await self.post(_url, data={"uri": url})
elif file:
ascii2d_url = "https://ascii2d.net/search/file"
files = {"file": file if isinstance(file, bytes) else open(file, "rb")}
resp = await self.post(ascii2d_url, files=files)
resp = await self.post(_url, files=files)
else:
raise ValueError("Either 'url' or 'file' must be provided")

Expand Down
14 changes: 11 additions & 3 deletions PicImageSearch/ehentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ class EHentai(HandOver):
Used for performing reverse image searches using EHentai service.
Attributes:
base_url: The base URL for EHentai searches.
base_url_ex: The base URL for EXHentai searches.
covers: A flag to search only for covers.
similar: A flag to enable similarity scanning.
exp: A flag to include results from expunged galleries.
"""

def __init__(
self,
base_url: str = "https://e-hentai.org",
base_url_ex: str = "https://exhentai.org",
covers: bool = False,
similar: bool = True,
exp: bool = False,
**request_kwargs: Any
**request_kwargs: Any,
):
"""Initializes an EHentai API client with specified configurations.
Args:
base_url: The base URL for EHentai searches.
base_url_ex: The base URL for EXHentai searches.
covers: If True, search only for covers; otherwise, False.
similar: If True, enable similarity scanning; otherwise, False.
exp: If True, include results from expunged galleries; otherwise, False.
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.base_url = base_url
self.base_url_ex = base_url_ex
self.covers: bool = covers
self.similar: bool = similar
self.exp: bool = exp
Expand Down Expand Up @@ -63,9 +71,9 @@ async def search(
Searching on exhentai.org requires logged-in status via cookies in `EHentai.request_kwargs`.
"""
_url: str = (
"https://exhentai.org/upld/image_lookup.php"
f"{self.base_url_ex}/upld/image_lookup.php"
if ex
else "https://upld.e-hentai.org/image_lookup.php"
else f"{self.base_url}/upld/image_lookup.php"
)
data: Dict[str, Any] = {"f_sfile": "search"}
if url:
Expand Down
13 changes: 7 additions & 6 deletions PicImageSearch/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ class Google(HandOver):
Attributes:
base_url: The base URL for Google searches, configurable for different regions.
Example: `https://www.google.co.jp/searchbyimage` for searches in Japan.
Example: `https://www.google.co.jp` for searches in Japan.
"""

def __init__(
self,
base_url: str = "https://www.google.com/searchbyimage",
base_url: str = "https://www.google.com",
**request_kwargs: Any,
):
"""Initializes a Google API client with specified configurations.
Args:
base_url: The base URL for Google searcher, defaults to the international version.
base_url: The base URL for Google searches, defaults to the international version.
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.base_url = base_url
self.base_url = f"{base_url}/searchbyimage"

async def _navigate_page(
self, resp: GoogleResponse, offset: int
Expand Down Expand Up @@ -89,15 +89,16 @@ async def search(
Raises:
ValueError: If neither 'url' nor 'file' is provided.
"""
_url = self.base_url if url else f"{self.base_url}/upload"
params: Dict[str, Any] = {"sbisrc": 1, "safe": "off"}
if url:
params["image_url"] = url
resp = await self.get(self.base_url, params=params)
resp = await self.get(_url, params=params)
elif file:
files = {
"encoded_image": file if isinstance(file, bytes) else open(file, "rb")
}
resp = await self.post(f"{self.base_url}/upload", data=params, files=files)
resp = await self.post(_url, data=params, files=files)
else:
raise ValueError("Either 'url' or 'file' must be provided")
return GoogleResponse(resp.text, resp.url)
17 changes: 15 additions & 2 deletions PicImageSearch/iqdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ class Iqdb(HandOver):
"""API client for the Iqdb image search engine.
Used for performing reverse image searches using Iqdb service.
Attributes:
base_url: The base URL for Iqdb searches.
base_url_3d: The base URL for Iqdb 3D searches.
"""

def __init__(self, **request_kwargs: Any):
def __init__(
self,
base_url: str = "https://iqdb.org",
base_url_3d: str = "https://3d.iqdb.org",
**request_kwargs: Any
):
"""Initializes an Iqdb API client with request configuration.
Args:
base_url: The base URL for Iqdb searches.
base_url_3d: The base URL for Iqdb 3D searches.
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.base_url = base_url
self.base_url_3d = base_url_3d

async def search(
self,
Expand Down Expand Up @@ -47,7 +60,7 @@ async def search(
Note:
Search can be tailored for anime or real-life images using `is_3d` parameter.
"""
iqdb_url = "https://3d.iqdb.org/" if is_3d else "https://iqdb.org/"
iqdb_url = self.base_url_3d if is_3d else self.base_url
data: Dict[str, Any] = {}
if force_gray:
data["forcegray"] = "on"
Expand Down
8 changes: 5 additions & 3 deletions PicImageSearch/saucenao.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
from .model import SauceNAOResponse
from .network import HandOver

BASE_URL = "https://saucenao.com/search.php"


class SauceNAO(HandOver):
"""API client for the SauceNAO image search engine.
Used for performing reverse image searches using SauceNAO service.
Attributes:
base_url: The base URL for SauceNAO searches.
params: The query parameters for SauceNAO search.
"""

def __init__(
self,
base_url: str = "https://saucenao.com",
api_key: Optional[str] = None,
numres: int = 5,
hide: int = 0,
Expand All @@ -36,6 +36,7 @@ def __init__(
"""Initializes a SauceNAO API client with specified configurations.
Args:
base_url: The base URL for SauceNAO searches.
api_key: API key for SauceNAO API access.
numres: Number of results to return from search.
hide: Option to hide results based on content rating.
Expand All @@ -55,6 +56,7 @@ def __init__(
https://saucenao.com/tools/examples/api/index_details.txt
"""
super().__init__(**request_kwargs)
self.base_url = f"{base_url}/search.php"
params: Dict[str, Any] = {
"testmode": testmode,
"numres": numres,
Expand Down Expand Up @@ -106,7 +108,7 @@ async def search(
)
else:
raise ValueError("Either 'url' or 'file' must be provided")
resp = await self.post(BASE_URL, params=params, files=files)
resp = await self.post(self.base_url, params=params, files=files)
resp_json = json_loads(resp.text)
resp_json.update({"status_code": resp.status_code})
return SauceNAOResponse(resp_json)
20 changes: 14 additions & 6 deletions PicImageSearch/tracemoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,36 @@ class TraceMoe(HandOver):
Used for performing reverse image searches using TraceMoe service.
Attributes:
base_url: The base URL for TraceMoe searches.
search_url: URL for TraceMoe API endpoint for image search.
me_url: URL for TraceMoe API endpoint to retrieve user info.
size: Optional string indicating preview size ('s', 'm', 'l').
mute: A flag to mute preview video in search results.
"""

search_url = "https://api.trace.moe/search"
me_url = "https://api.trace.moe/me"

def __init__(
self, mute: bool = False, size: Optional[str] = None, **request_kwargs: Any
self,
base_url: str = "https://trace.moe",
base_url_api: str = "https://api.trace.moe",
mute: bool = False,
size: Optional[str] = None,
**request_kwargs: Any,
):
"""Initializes a TraceMoe API client with specified configurations.
Args:
base_url: The base URL for TraceMoe searches.
base_url_api: The base URL for TraceMoe API searches.
mute: If True, mutes preview video in search results.
size: Specifies preview video size ('s', 'm', 'l').
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.size: Optional[str] = size
self.base_url = base_url
self.search_url = f"{base_url_api}/search"
self.me_url = f"{base_url_api}/me"
self.mute: bool = mute
self.size: Optional[str] = size

async def me(self, key: Optional[str] = None) -> TraceMoeMe:
"""Retrieves information about the user's API key usage from TraceMoe.
Expand Down Expand Up @@ -115,7 +123,7 @@ async def update_anime_info(
chinese_title: If True, includes Chinese title in item info.
"""
variables = {"id": item.anilist}
url = "https://trace.moe/anilist/"
url = f"{self.base_url}/anilist/"
item.anime_info = json_loads(
(
await self.post(
Expand Down
17 changes: 12 additions & 5 deletions PicImageSearch/yandex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@
from .model import YandexResponse
from .network import HandOver

BASE_URL = "https://yandex.com/images/search"


class Yandex(HandOver):
"""API client for the Yandex image search engine.
Used for performing reverse image searches using Yandex service.
Attributes:
base_url: The base URL for Yandex searches.
"""

def __init__(self, **request_kwargs: Any):
def __init__(
self,
base_url: str = "https://yandex.com",
**request_kwargs: Any,
):
"""Initializes a Yandex API client with specified configurations.
Args:
base_url: The base URL for Yandex searches.
**request_kwargs: Additional arguments for network requests.
"""
super().__init__(**request_kwargs)
self.base_url = f"{base_url}/images/search"

async def search(
self, url: Optional[str] = None, file: Union[str, bytes, Path, None] = None
Expand All @@ -43,13 +50,13 @@ async def search(
params = {"rpt": "imageview", "cbir_page": "sites"}
if url:
params["url"] = url
resp = await self.get(BASE_URL, params=params)
resp = await self.get(self.base_url, params=params)
elif file:
files: Dict[str, Any] = {
"upfile": file if isinstance(file, bytes) else open(file, "rb")
}
resp = await self.post(
BASE_URL, params=params, data={"prg": 1}, files=files
self.base_url, params=params, data={"prg": 1}, files=files
)
else:
raise ValueError("Either 'url' or 'file' must be provided")
Expand Down
2 changes: 1 addition & 1 deletion demo/cn/demo_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test03.jpg"
file = "../images/test03.jpg"
base_url = "https://www.google.co.jp/searchbyimage"
base_url = "https://www.google.co.jp"


@logger.catch()
Expand Down
2 changes: 1 addition & 1 deletion demo/en/demo_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test03.jpg"
file = "../images/test03.jpg"
base_url = "https://www.google.co.jp/searchbyimage"
base_url = "https://www.google.co.jp"


@logger.catch()
Expand Down
2 changes: 1 addition & 1 deletion demo/ru/demo_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test03.jpg"
file = "../images/test03.jpg"
base_url = "https://www.google.co.jp/searchbyimage"
base_url = "https://www.google.co.jp"


@logger.catch()
Expand Down

0 comments on commit f5ff3ab

Please sign in to comment.