Skip to content

Commit

Permalink
refactor: 重构 search 参数 file 为 str / Path 类型
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 如果有用到上传文件搜索,请跟进这个变化
  • Loading branch information
NekoAria committed Aug 24, 2022
1 parent 2d1c0c7 commit 6f31ffe
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 64 deletions.
10 changes: 4 additions & 6 deletions PicImageSearch/ascii2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, BinaryIO, Optional

from aiohttp import FormData
from pathlib import Path
from typing import Any, Optional, Union

from .model import Ascii2DResponse
from .network import HandOver
Expand All @@ -24,7 +23,7 @@ def __init__(self, bovw: bool = False, **request_kwargs: Any):
self.bovw: bool = bovw

async def search(
self, url: Optional[str] = None, file: Optional[BinaryIO] = None
self, url: Optional[str] = None, file: Union[str, Path, None] = None
) -> Ascii2DResponse:
"""
Ascii2D
Expand All @@ -48,8 +47,7 @@ async def search(
resp_text, resp_url, _ = await self.post(ascii2d_url, data={"uri": url})
elif file:
ascii2d_url = "https://ascii2d.net/search/file"
data = FormData()
data.add_field("file", file, filename="file.png")
data = {"file": open(file, "rb")}
resp_text, resp_url, _ = await self.post(ascii2d_url, data=data)
else:
raise ValueError("url or file is required")
Expand Down
10 changes: 4 additions & 6 deletions PicImageSearch/baidu.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from json import loads as json_loads
from typing import Any, BinaryIO, Optional

from aiohttp import FormData
from pathlib import Path
from typing import Any, Optional, Union

from .model import BaiDuResponse
from .network import HandOver
Expand All @@ -12,15 +11,14 @@ def __init__(self, **request_kwargs: Any):
super().__init__(**request_kwargs)

async def search(
self, url: Optional[str] = None, file: Optional[BinaryIO] = None
self, url: Optional[str] = None, file: Union[str, Path, None] = None
) -> BaiDuResponse:
params = {"from": "pc"}
data = None
if url:
params["image"] = url
elif file:
data = FormData()
data.add_field("image", file, filename="file.png")
data = {"image": open(file, "rb")}
else:
raise ValueError("url or file is required")
resp_text, resp_url, _ = await self.post(
Expand Down
21 changes: 10 additions & 11 deletions PicImageSearch/ehentai.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import io
from typing import Any, BinaryIO, Optional

from aiohttp import FormData
from pathlib import Path
from typing import Any, Dict, Optional, Union

from .model import EHentaiResponse
from .network import HandOver
Expand All @@ -23,26 +22,26 @@ def __init__(
async def search(
self,
url: Optional[str] = None,
file: Optional[BinaryIO] = None,
file: Union[str, Path, None] = None,
ex: bool = False,
) -> EHentaiResponse:
_url = (
"https://exhentai.org/upld/image_lookup.php"
if ex
else "https://upld.e-hentai.org/image_lookup.php"
)
data = FormData({"f_sfile": "search"})
data: Dict[str, Any] = {"f_sfile": "search"}
if url:
file = io.BytesIO(await self.download(url))
if file:
data.add_field("sfile", file, filename="file.png")
data["sfile"] = io.BytesIO(await self.download(url))
elif file:
data["sfile"] = open(file, "rb")
else:
raise ValueError("url or file is required")
if self.covers:
data.add_field("fs_covers", "on")
data["fs_covers"] = "on"
if self.similar:
data.add_field("fs_similar", "on")
data["fs_similar"] = "on"
if self.exp:
data.add_field("fs_exp", "on")
data["fs_exp"] = "on"
resp_text, resp_url, _ = await self.post(url=_url, data=data)
return EHentaiResponse(resp_text, resp_url)
9 changes: 4 additions & 5 deletions PicImageSearch/google.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, BinaryIO, Optional
from pathlib import Path
from typing import Any, Optional, Union
from urllib.parse import quote

from aiohttp import FormData
from lxml.html import HTMLParser, fromstring
from pyquery import PyQuery

Expand Down Expand Up @@ -38,7 +38,7 @@ async def goto_page(self, url: str, index: int) -> GoogleResponse:
return self._slice(resp_text, index)

async def search(
self, url: Optional[str] = None, file: Optional[BinaryIO] = None
self, url: Optional[str] = None, file: Union[str, Path, None] = None
) -> GoogleResponse:
"""
Google
Expand All @@ -60,8 +60,7 @@ async def search(
params = {"image_url": encoded_image_url}
resp_text, _, _ = await self.get(self.url, params=params)
elif file:
data = FormData()
data.add_field("encoded_image", file, filename="file.png")
data = {"encoded_image": open(file, "rb")}
resp_text, _, _ = await self.post(f"{self.url}/upload", data=data)
else:
raise ValueError("url or file is required")
Expand Down
14 changes: 7 additions & 7 deletions PicImageSearch/iqdb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, BinaryIO, Optional
from pathlib import Path
from typing import Any, Dict, Optional, Union

from aiohttp import FormData
from lxml.html import HTMLParser, fromstring
from pyquery import PyQuery

Expand Down Expand Up @@ -32,19 +32,19 @@ def _slice(resp: str) -> IqdbResponse:
async def search(
self,
url: Optional[str] = None,
file: Optional[BinaryIO] = None,
file: Union[str, Path, None] = None,
force_gray: bool = False,
is_3d: bool = False,
) -> IqdbResponse:
iqdb_url = "https://3d.iqdb.org/" if is_3d else "https://iqdb.org/"
data = FormData()
data: Dict[str, Any] = {}
if force_gray: # 忽略颜色
data.add_field("forcegray", "on")
data["forcegray"] = "on"
if url:
data.add_field("url", url)
data["url"] = url
resp_text, _, _ = await self.post(iqdb_url, data=data)
elif file:
data.add_field("file", file, filename="file.png")
data["file"] = open(file, "rb")
resp_text, _, _ = await self.post(iqdb_url, data=data)
else:
raise ValueError("url or file is required")
Expand Down
8 changes: 4 additions & 4 deletions PicImageSearch/saucenao.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from json import loads as json_loads
from typing import Any, BinaryIO, Dict, List, Optional, Union
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

from aiohttp import FormData
from multidict import MultiDict

from .model import SauceNAOResponse
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(
self.params.add("dbs[]", i)

async def search(
self, url: Optional[str] = None, file: Optional[BinaryIO] = None
self, url: Optional[str] = None, file: Union[str, Path, None] = None
) -> SauceNAOResponse:
"""
SauceNAO
Expand Down Expand Up @@ -103,7 +103,7 @@ async def search(
if url:
params.add("url", url)
elif file:
data = FormData({"file": file})
data = {"file": open(file, "rb")}
else:
raise ValueError("url or file is required")
resp_text, _, resp_status = await self.post(
Expand Down
17 changes: 4 additions & 13 deletions PicImageSearch/tracemoe.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
from json import loads as json_loads
from typing import Any, BinaryIO, Dict, Optional, Union

from aiohttp import FormData
from pathlib import Path
from typing import Any, Dict, Optional, Union

from .model import TraceMoeItem, TraceMoeMe, TraceMoeResponse
from .network import HandOver
Expand Down Expand Up @@ -56,13 +55,6 @@ def __init__(
self.size: Optional[str] = size
self.mute: bool = mute

# @staticmethod
# def _base_64(filename):
# with open(filename, 'rb') as f:
# coding = base64.b64encode(f.read()) # 读取文件内容,转换为base64编码
# # print('本地base64转码~')
# return coding.decode()

# 获取自己的信息
async def me(self, key: Optional[str] = None) -> TraceMoeMe:
params = {"key": key} if key else None
Expand Down Expand Up @@ -116,7 +108,7 @@ async def update_anime_info(
async def search(
self,
url: Optional[str] = None,
file: Optional[BinaryIO] = None,
file: Union[str, Path, None] = None,
key: Optional[str] = None,
anilist_id: Optional[int] = None,
chinese_title: bool = True,
Expand All @@ -136,8 +128,7 @@ async def search(
params = self.set_params(url, anilist_id, cut_borders)
elif file:
params = self.set_params(None, anilist_id, cut_borders)
data = FormData()
data.add_field("file", file, filename="file.png")
data = {"file": open(file, "rb")}
else:
raise ValueError("url or file is required")
resp_text, _, _ = await self.post(
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async with Network() as client: # 可以设置代理 Network(proxies='scheme://
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test01.jpg"
resp = await saucenao.search(url=url)
# 搜索本地图片
# file = open(r"demo/images/test01.jpg", "rb")
# file = "demo/images/test01.jpg"
# resp = await saucenao.search(file=file)

logger.info(resp.status_code) # HTTP 状态码
Expand All @@ -81,7 +81,7 @@ saucenao = SauceNAO(api_key="your api key") # api_key 不能少
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test01.jpg"
resp = saucenao.search(url=url)
# 搜索本地图片
# file = open(r"demo/images/test01.jpg", "rb")
# file = "demo/images/test01.jpg"
# resp = saucenao.search(file=file)
# 下面操作与异步方法一致
```
Expand Down
2 changes: 1 addition & 1 deletion demo/demo_ascii2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test01.jpg"
file = open(r"images/test01.jpg", "rb")
file = "images/test01.jpg"
bovw = True # 是否使用特征检索
bypass = False # 是否绕过DNS污染

Expand Down
2 changes: 1 addition & 1 deletion demo/demo_baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test02.jpg"
file = open(r"images/test02.jpg", "rb")
file = "images/test02.jpg"


@logger.catch()
Expand Down
2 changes: 1 addition & 1 deletion demo/demo_ehentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
proxies = "http://127.0.0.1:1081"
# proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test06.jpg"
file = open(r"images/test06.jpg", "rb")
file = "images/test06.jpg"
cookies = None # 注意:如果要使用 EXHentai 搜索,需要提供 cookies
ex = False # 是否使用 EXHentai 搜索

Expand Down
2 changes: 1 addition & 1 deletion demo/demo_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
proxies = "http://127.0.0.1:1081"
# proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test03.jpg"
file = open(r"images/test03.jpg", "rb")
file = "images/test03.jpg"


@logger.catch()
Expand Down
2 changes: 1 addition & 1 deletion demo/demo_iqdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test01.jpg"
file = open(r"images/test01.jpg", "rb")
file = "images/test01.jpg"
bypass = False # 绕过DNS污染


Expand Down
2 changes: 1 addition & 1 deletion demo/demo_iqdb_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test04.jpg"
file = open(r"images/test04.jpg", "rb")
file = "images/test04.jpg"
bypass = False # 是否绕过DNS污染


Expand Down
6 changes: 3 additions & 3 deletions demo/demo_saucenao.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test01.jpg"
file = open(r"images/test01.jpg", "rb")
file = "images/test01.jpg"
api_key = "a4ab3f81009b003528f7e31aed187fa32a063f58"
bypass = True # 是否绕过DNS污染

Expand All @@ -18,8 +18,8 @@
async def test() -> None:
async with Network(proxies=proxies, bypass=bypass) as client:
saucenao = SauceNAO(client=client, api_key=api_key, hide=3)
resp = await saucenao.search(url=url)
# resp = await saucenao.search(file=file)
# resp = await saucenao.search(url=url)
resp = await saucenao.search(file=file)
show_result(resp)


Expand Down
2 changes: 1 addition & 1 deletion demo/demo_tracemoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# proxies = "http://127.0.0.1:1081"
proxies = None
url = "https://raw.githubusercontent.com/kitUIN/PicImageSearch/main/demo/images/test05.jpg"
file = open(r"images/test05.jpg", "rb")
file = "images/test05.jpg"
bypass = False # 是否绕过DNS污染


Expand Down

0 comments on commit 6f31ffe

Please sign in to comment.