Skip to content

Commit 415d7a6

Browse files
xunxun2001jerrita
andauthored
[feature] Add batch send functionality for anime images; introduce ne… (#9)
* [feature] Add batch send functionality for anime images; introduce new cat image plugin --------- Co-authored-by: jerrita <[email protected]>
1 parent 7674930 commit 415d7a6

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

plugins/example/anime_picture.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import json
2+
import re
3+
from typing import Optional
24

35
import aiohttp
6+
import chinese2digits as c2d
47

58
from anon import Storage
69
from anon.event import MessageEvent
710
from anon.logger import logger
8-
from anon.message import Image, ImageCategory
11+
from anon.message import Image
912
from anon.plugin import PluginManager
1013

1114
pm = PluginManager()
@@ -15,12 +18,13 @@
1518
"r18": 0,
1619
"ai": 0,
1720
"level": "0-2",
18-
"taste": "1,2"
21+
"taste": "1,2",
1922
}
2023

2124

22-
async def fetch_and_save_image(tag: list):
25+
async def fetch_and_save_image(tag: list, num: int = 1) -> Optional[list]:
2326
params = storage['param'].copy()
27+
params["num"] = num if 1 < num <= 5 else 1
2428
if tag:
2529
params["tag"] = tag
2630
logger.info(f"tag:{tag},params:{params}")
@@ -34,9 +38,12 @@ async def fetch_and_save_image(tag: list):
3438
if not datas["data"]:
3539
return None
3640

37-
img_url = datas["data"][0]["urls"]["original"]
38-
img_url = img_url.replace('i.pixiv.cat', 'i.pixiv.re')
39-
return img_url
41+
img_url_list = []
42+
for i_url in range(num):
43+
img_url = datas["data"][i_url]["urls"]["original"]
44+
img_url = img_url.replace('i.pixiv.cat', 'i.pixiv.re')
45+
img_url_list.append(img_url)
46+
return img_url_list
4047

4148

4249
@pm.register_event([MessageEvent])
@@ -59,4 +66,23 @@ async def handle_message(event: MessageEvent):
5966
if img_url is None:
6067
await event.reply("图库查找无结果", quote=True)
6168
else:
62-
await event.reply(Image(img_url), quote=False)
69+
await event.reply(Image(img_url[0]), quote=False)
70+
elif parts:
71+
match = re.match(r'来(\d+|零|两|一|二|三|四|五|六|七|八|九)(张|份)(色图|涩图)', parts[0])
72+
if match:
73+
quantity = match.group(1)
74+
try:
75+
num = int(quantity)
76+
except:
77+
num = int(c2d.chineseToDigits(quantity))
78+
tag = parts[1:] if len(parts) > 1 else None
79+
logger.info(f"数量: {num}, 标签: {tag}")
80+
img_url = await fetch_and_save_image(tag, num)
81+
if img_url is None:
82+
await event.reply("图库查找无结果", quote=True)
83+
else:
84+
index = 1
85+
for i_url in img_url:
86+
logger.info(f"第{index}份色图发送中")
87+
index += 1
88+
await event.reply(Image(i_url), quote=False)

plugins/example/cat.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import re
2+
3+
import aiohttp
4+
5+
from anon.event import MessageEvent
6+
from anon.logger import logger
7+
from anon.message import Image
8+
from anon.plugin import PluginManager
9+
10+
pm = PluginManager()
11+
12+
13+
async def fetch_and_save_image(limit: bool = False) -> list:
14+
params = {}
15+
num = 10 if limit else 1
16+
params["limit"] = num
17+
18+
async with aiohttp.ClientSession() as session:
19+
async with session.get("https://api.thecatapi.com/v1/images/search", params=params) as response:
20+
logger.info(f"params:{params}")
21+
datas = await response.json()
22+
logger.info(datas)
23+
24+
img_url_list = []
25+
for i_url in range(num):
26+
img_url = datas[i_url]["url"]
27+
img_url_list.append(img_url)
28+
return img_url_list
29+
30+
31+
@pm.register_event([MessageEvent])
32+
async def cat(event: MessageEvent):
33+
message = event.msg.text()
34+
if re.fullmatch(r'喵+', message):
35+
# 计算“喵”的数量
36+
count = len(message) // len('喵')
37+
if count == 1:
38+
img_url = await fetch_and_save_image(False)
39+
logger.info('喵')
40+
elif count > 1:
41+
img_url = await fetch_and_save_image(True)
42+
await event.reply("不管多少个喵,只要大于1个都会返回10张图的喵~", quote=True)
43+
logger.info(f'收到了{count}个喵!')
44+
else:
45+
return
46+
index = 1
47+
for i_url in img_url:
48+
logger.info(f"第{index}份猫图发送中")
49+
index += 1
50+
await event.reply(Image(i_url), quote=False)

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ aiocron~=1.8
44

55
# Example plugins dependencies
66
pytz~=2024.1
7+
aiohttp~=3.9.3
8+
chinese2digits~=6.0.2
79

810
# Extras
9-
aiohttp~=3.9.3
11+
zhipuai~=2.0.1

0 commit comments

Comments
 (0)