Skip to content

Commit d909504

Browse files
committed
up
1 parent fab368e commit d909504

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ plugins = [
126126
| `ASCII2D_BASE_URL` || `https://ascii2d.net` | Ascii2D Base URL \([#139](https://github.com/lgc-NB2Dev/YetAnotherPicSearch/issues/139)\) |
127127
| `HIDE_ASCII2D_BASE_URL` || `True` | 当自定义 Ascii2D Base URL 后,是否将自定义的 URL 替换回官方 URL |
128128
| `EXHENTAI_COOKIES` || `None` | ExHentai 的 Cookies,没有的情况下自动改用 E-Hentai 搜图,获取方式参考 请参考 [PicImageSearch 文档](https://pic-image-search.kituin.fun/wiki/picimagesearch/E-hentai/DataStructure/#cookies%E8%8E%B7%E5%8F%96) |
129+
| `NHENTAI_BASE_URL` || `https://ascii2d.net` | NHentai Base URL \([#139](https://github.com/lgc-NB2Dev/YetAnotherPicSearch/issues/139)\) |
130+
| `HIDE_NHENTAI_BASE_URL` || `True` | 当自定义 NHentai Base URL 后,是否将自定义的 URL 替换回官方 URL |
129131
| `NHENTAI_USERAGENT` || `None` | 用来绕过 NHentai Cloudflare 拦截的 User Agent,配置后在 E-Hentai 标题搜索无结果时会自动调用 NHentai 标题搜索<br />先用配置的 `PROXY` 做代理,使用浏览器访问 NHentai 通过 CloudFlare 检测后,获取 UA 和 Cookies 填到对应配置项 |
130132
| `NHENTAI_COOKIES` || `None` | 用来绕过 NHentai Cloudflare 拦截的 Cookies,同上 |
131133
| **行为配置** | | | |
@@ -194,6 +196,14 @@ Telegram:[@lgc2333](https://t.me/lgc2333)
194196

195197
## 📝 更新日志
196198

199+
### 2.0.6
200+
201+
- 添加配置 `NHENTAI_BASE_URL``HIDE_NHENTAI_BASE_URL`
202+
203+
### 2.0.5
204+
205+
- feat: 添加隐藏 ascii2d 网页反向代理地址的配置及功能 by [@iona-s](https://github.com/iona-s) in [#186](https://github.com/lgc-NB2Dev/YetAnotherPicSearch/pull/186)
206+
197207
### 2.0.4
198208

199209
- 兼容 HTTPX 0.28

YetAnotherPicSearch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
load_search_func()
1313

14-
__version__ = "2.0.5"
14+
__version__ = "2.0.6"
1515
__plugin_meta__ = PluginMetadata(
1616
name="YetAnotherPicSearch",
1717
description="基于 NoneBot2 及 PicImageSearch 的另一个 NoneBot 搜图插件",

YetAnotherPicSearch/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ConfigModel(BaseModel):
1616
exhentai_cookies: Optional[str] = None
1717
nhentai_useragent: Optional[str] = None
1818
nhentai_cookies: Optional[str] = None
19+
nhentai_base_url: Annotated[str, HttpUrl] = "https://nhentai.net"
20+
hide_nhentai_base_url: bool = True
1921

2022
saucenao_low_acc: int = 60
2123
auto_use_ascii2d: bool = True

YetAnotherPicSearch/data_source/nhentai.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
NHENTAI_COOKIES = parse_cookies(config.nhentai_cookies)
2424

2525

26+
def get_nh_display_base():
27+
return config.nhentai_base_url if config.hide_nhentai_base_url else "https://nhentai.net"
28+
29+
2630
async def update_nhentai_info(item: NHentaiItem) -> None:
2731
async with AsyncClient(
2832
headers=NHENTAI_HEADERS,
2933
cookies=NHENTAI_COOKIES,
3034
proxy=config.proxy,
3135
) as session:
32-
resp = await session.get(item.url)
36+
resp = await session.get(item.with_base_url(config.nhentai_base_url))
3337
uft8_parser = HTMLParser(encoding="utf-8")
3438
data = PyQuery(fromstring(resp.text, parser=uft8_parser))
3539
item.origin = data
@@ -44,15 +48,16 @@ async def update_nhentai_info(item: NHentaiItem) -> None:
4448

4549
async def nhentai_title_search(title: str) -> list[UniMessage]:
4650
query = preprocess_search_query(title)
47-
url = "https://nhentai.net/search/"
48-
params = {"q": query}
4951
async with AsyncClient(
5052
headers=NHENTAI_HEADERS,
5153
cookies=NHENTAI_COOKIES,
5254
proxy=config.proxy,
5355
) as session:
54-
resp = await session.get(url, params=params)
55-
if res := NHentaiResponse(resp.text, str(resp.url)):
56+
resp = await session.get(
57+
f"{config.nhentai_base_url}/search/",
58+
params={"q": query},
59+
)
60+
if res := NHentaiResponse(resp.text, str(resp.url).replace(config.nhentai_base_url, "", 1)):
5661
# 只保留标题和搜索关键词相关度较高的结果,并排序,以此来提高准确度
5762
if res.raw:
5863
res.raw = filter_results_with_ratio(res, title)
@@ -62,7 +67,11 @@ async def nhentai_title_search(title: str) -> list[UniMessage]:
6267

6368

6469
async def search_result_filter(res: NHentaiResponse) -> list[UniMessage]:
65-
url = await shorten_url(res.url)
70+
display_base = get_nh_display_base()
71+
url = await shorten_url(
72+
res.with_base_url(display_base),
73+
force_shorten=True,
74+
)
6675
if not res.raw:
6776
return [UniMessage.text(f"NHentai 搜索结果为空\n搜索页面:{url}")]
6877

@@ -85,7 +94,7 @@ async def search_result_filter(res: NHentaiResponse) -> list[UniMessage]:
8594
selected_res.title,
8695
f"类型:{selected_res.type}",
8796
f"日期:{date}",
88-
f"来源:{selected_res.url}",
97+
f"来源:{selected_res.with_base_url(display_base)}",
8998
f"搜索页面:{url}",
9099
]
91100
return [combine_message(res_list)]

YetAnotherPicSearch/nhentai_model.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ def __init__(self, data: PyQuery):
99
self.origin: PyQuery = data # 原始数据
1010
self.title: str = cast("str", data.find(".caption").text())
1111
cover = data.find(".cover")
12-
self.url: str = f"https://nhentai.net{cover.attr('href')}"
12+
self.href: str = cast("str", cover.attr("href"))
1313
self.thumbnail: str = cast("str", cover.find("img").attr("data-src"))
1414
self.type: str = ""
1515
self.date: str = ""
1616
self.tags: list[str] = []
1717

18+
def with_base_url(self, base: str) -> str:
19+
return f"{base}/{self.href}"
20+
1821

1922
class NHentaiResponse:
20-
def __init__(self, resp_text: str, resp_url: str):
23+
def __init__(self, resp_text: str, resp_href: str):
2124
self.origin: str = resp_text # 原始数据
2225
uft8_parser = HTMLParser(encoding="utf-8")
2326
data = PyQuery(fromstring(self.origin, parser=uft8_parser))
2427
self.raw: list[NHentaiItem] = [NHentaiItem(i) for i in data.find(".gallery").items()]
25-
self.url: str = resp_url
28+
self.href: str = resp_href
29+
30+
def with_base_url(self, base: str) -> str:
31+
return f"{base}/{self.href}"

YetAnotherPicSearch/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def confuse_url(url: str) -> str:
157157
)
158158

159159

160-
async def shorten_url(url: str) -> str:
160+
async def shorten_url(url: str, force_shorten: bool = False) -> str:
161161
pid_search = re.compile(
162162
r"(?:pixiv.+(?:illust_id=|artworks/)|/img-original/img/(?:\d+/){6})(\d+)",
163163
)
@@ -172,7 +172,7 @@ async def shorten_url(url: str) -> str:
172172
if host == "danbooru.donmai.us":
173173
return confuse_url(url.replace("/post/show/", "/posts/"))
174174

175-
if host in {
175+
if force_shorten or host in {
176176
"e-hentai.org",
177177
"exhentai.org",
178178
"graph.baidu.com",

0 commit comments

Comments
 (0)