Skip to content

Commit 67862c5

Browse files
committed
refactor(__init__): 重构获取图片 MD5 的逻辑
1 parent 17f90ab commit 67862c5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

YetAnotherPicSearch/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ async def handle_first_receive(
9595

9696
async def image_search(
9797
url: str,
98+
md5: str,
9899
mode: str,
99100
purge: bool,
100101
_cache: Cache,
101102
client: ClientSession,
102103
hide_img: bool = config.hide_img,
103104
) -> List[str]:
104105
url = await get_universal_img_url(url)
105-
image_md5 = re.search(r"[A-F\d]{32}", url)[0] # type: ignore
106-
if not purge and (result := exist_in_cache(_cache, image_md5, mode)):
106+
if not purge and (result := exist_in_cache(_cache, md5, mode)):
107107
return [f"[缓存] {i}" for i in result]
108108
result = []
109109
try:
@@ -122,7 +122,7 @@ async def image_search(
122122
else:
123123
result = await saucenao_search(url, mode, client, hide_img)
124124
# 仅对涉及到 saucenao 的搜图结果做缓存
125-
upsert_cache(_cache, image_md5, mode, result)
125+
upsert_cache(_cache, md5, mode, result)
126126
except Exception as e:
127127
logger.exception(f"该图 [{url}] 搜图失败")
128128
result = [f"该图搜图失败\nE: {repr(e)}"]
@@ -142,9 +142,13 @@ async def get_universal_img_url(url: str) -> str:
142142
return url
143143

144144

145-
def get_image_urls(event: MessageEvent) -> List[str]:
145+
def get_image_urls_with_md5(event: MessageEvent) -> List[Tuple[str, str]]:
146146
message = event.reply.message if event.reply else event.message
147-
return [i.data["url"] for i in message if i.type == "image" and i.data.get("url")]
147+
return [
148+
(i.data["url"], str(i.data["file"]).rstrip(".image").upper())
149+
for i in message
150+
if i.type == "image" and i.data.get("url")
151+
]
148152

149153

150154
def get_args(msg: Message) -> Tuple[str, bool]:
@@ -233,8 +237,8 @@ async def send_forward_msg(
233237
@IMAGE_SEARCH.handle()
234238
@IMAGE_SEARCH_MODE.got("IMAGES", prompt="请发送图片")
235239
async def handle_image_search(bot: Bot, event: MessageEvent, matcher: Matcher) -> None:
236-
image_urls = get_image_urls(event)
237-
if not image_urls:
240+
image_urls_with_md5 = get_image_urls_with_md5(event)
241+
if not image_urls_with_md5:
238242
await IMAGE_SEARCH_MODE.reject()
239243
if "ARGS" in matcher.state:
240244
mode, purge = matcher.state["ARGS"]
@@ -247,11 +251,11 @@ async def handle_image_search(bot: Bot, event: MessageEvent, matcher: Matcher) -
247251
)
248252
async with network as client:
249253
with Cache("picsearch_cache") as _cache:
250-
for index, value in enumerate(image_urls):
254+
for index, (url, md5) in enumerate(image_urls_with_md5):
251255
await send_result_message(
252256
bot,
253257
event,
254-
await image_search(value, mode, purge, _cache, client),
255-
index if len(image_urls) > 1 else None,
258+
await image_search(url, md5, mode, purge, _cache, client),
259+
index if len(image_urls_with_md5) > 1 else None,
256260
)
257261
_cache.expire()

0 commit comments

Comments
 (0)