Skip to content

Commit 5c1af8c

Browse files
committed
♻️ 去除不必要的 async/await
1 parent 201b973 commit 5c1af8c

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

YetAnotherPicSearch/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async def image_search(
6565
ensure_ascii=False,
6666
)
6767
image_md5 = re.search("[A-F0-9]{32}", url)[0] # type: ignore
68-
_result = await exist_in_cache(db, image_md5, mode)
68+
_result = exist_in_cache(db, image_md5, mode)
6969
cached = bool(_result)
7070
if purge or not _result:
7171
result_dict: Dict[str, Any] = {}
@@ -82,7 +82,7 @@ async def image_search(
8282
db.upsert(
8383
_result.__dict__, (Query().image_md5 == image_md5) & (Query().mode == mode)
8484
)
85-
await clear_expired_cache(db)
85+
clear_expired_cache(db)
8686
db.close()
8787
if mode == "a2d":
8888
final_res = _result.ascii2d
@@ -91,15 +91,15 @@ async def image_search(
9191
else:
9292
final_res = _result.saucenao
9393
if cached and not purge:
94-
return [f"[缓存] {i}" for i in final_res] # type: ignore
94+
return [f"[缓存] {i}" for i in final_res]
9595
return final_res
9696

9797

98-
async def get_image_urls(msg: Message) -> List[str]:
98+
def get_image_urls(msg: Message) -> List[str]:
9999
return [i.data["url"] for i in msg if i.type == "image" and i.data.get("url")]
100100

101101

102-
async def get_args(msg: Message) -> Tuple[str, bool]:
102+
def get_args(msg: Message) -> Tuple[str, bool]:
103103
mode = "all"
104104
plain_text = msg.extract_plain_text()
105105
args = ["a2d", "pixiv", "danbooru", "doujin", "anime", "ex"]
@@ -115,10 +115,10 @@ async def get_args(msg: Message) -> Tuple[str, bool]:
115115
@IMAGE_SEARCH_MODE.got("IMAGES", prompt="请发送图片及搜索类型(可选)")
116116
async def handle_image_search(bot: Bot, event: MessageEvent) -> None:
117117
message = event.reply.message if event.reply else event.message
118-
image_urls = await get_image_urls(message)
118+
image_urls = get_image_urls(message)
119119
if not image_urls:
120120
await IMAGE_SEARCH_MODE.reject()
121-
mode, purge = await get_args(event.message)
121+
mode, purge = get_args(event.message)
122122
for i in image_urls:
123123
msg_list = await image_search(i, mode, purge, config.proxy)
124124
if isinstance(event, PrivateMessageEvent):

YetAnotherPicSearch/ascii2d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def get_final_res(res: Ascii2DResponse) -> str:
2020
thumbnail = await handle_img(res.raw[0].thumbnail, proxy, hide_img)
2121
_url = ""
2222
if res.raw[0]:
23-
_url = await shorten_url(res.raw[0].url)
23+
_url = shorten_url(res.raw[0].url)
2424
res_list = [
2525
f"{thumbnail}",
2626
f"{res.raw[0].title}" if res.raw[0].title else "",

YetAnotherPicSearch/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .result import Result
1010

1111

12-
async def exist_in_cache(db: TinyDB, image_md5: str, mode: str) -> Optional[Result]:
12+
def exist_in_cache(db: TinyDB, image_md5: str, mode: str) -> Optional[Result]:
1313
result: Optional[Result] = None
1414
cache_result: List[Document] = db.search(
1515
(Query().image_md5 == image_md5) & (Query().mode == mode)
@@ -22,6 +22,6 @@ async def exist_in_cache(db: TinyDB, image_md5: str, mode: str) -> Optional[Resu
2222
return result
2323

2424

25-
async def clear_expired_cache(db: TinyDB) -> None:
25+
def clear_expired_cache(db: TinyDB) -> None:
2626
expired_date = arrow.now().shift(days=-config.cache_expire).for_json()
2727
db.remove(Query().update_at < expired_date)

YetAnotherPicSearch/result.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
class Result:
55
def __init__(self, mapping: Dict[str, Any]):
6-
self.ascii2d: Optional[List[str]] = []
7-
self.saucenao: Optional[List[str]] = []
8-
self.whatanime: Optional[List[str]] = []
9-
self.ex: Optional[List[str]] = []
6+
self.ascii2d: List[str] = []
7+
self.saucenao: List[str] = []
8+
self.ex: List[str] = []
109
self.image_md5: Optional[str] = None
1110
self.update_at: Optional[str] = None
1211
self.mode: str = "all"

YetAnotherPicSearch/saucenao.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ async def saucenao_search(
4444
selected_res.url = i
4545
thumbnail = await handle_img(selected_res.thumbnail, proxy, hide_img)
4646
if selected_res.origin["data"].get("source"):
47-
source = await shorten_url(selected_res.origin["data"]["source"])
47+
source = shorten_url(selected_res.origin["data"]["source"])
4848
else:
49-
source = await shorten_url(await get_source(selected_res.url, proxy))
49+
source = shorten_url(await get_source(selected_res.url, proxy))
5050
# 如果结果为 doujin ,尝试返回日文标题而不是英文标题
5151
if selected_res.index_id == saucenao_db["doujin"] and selected_res.origin[
5252
"data"
5353
].get("jp_name"):
5454
selected_res.title = selected_res.origin["data"]["jp_name"]
55-
_url = await shorten_url(selected_res.url)
55+
_url = shorten_url(selected_res.url)
5656
res_list = [
5757
f"SauceNAO({selected_res.similarity}%)",
5858
f"{thumbnail}",

YetAnotherPicSearch/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def get_pic_base64_by_url(
1717
async with aiohttp.ClientSession(headers=headers) as session:
1818
async with session.get(url, proxy=proxy) as resp:
1919
if resp.status == 200:
20-
return base64.b64encode(await resp.read()).decode("utf-8")
20+
return base64.b64encode(await resp.read()).decode()
2121
return ""
2222

2323

@@ -57,7 +57,7 @@ async def get_source(url: str, proxy: Optional[str]) -> str:
5757
return str(source)
5858

5959

60-
async def shorten_url(url: str) -> str:
60+
def shorten_url(url: str) -> str:
6161
pid_search = re.compile(
6262
r"(?:pixiv.+(?:illust_id=|artworks/)|/img-original/img/(?:\d+/){6})(\d+)"
6363
)

0 commit comments

Comments
 (0)