lintonxue00 commited on
Commit
a111a56
1 Parent(s): 6d9d8e0

Upload ext_sexnyan_pic.py

Browse files
不知道/回收站/1/ext_sexnyan_pic.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .Extension import Extension
2
+ import requests
3
+
4
+ # 扩展的配置信息,用于ai理解扩展的功能 *必填*
5
+ ext_config:dict = {
6
+ "name": "AnimePic", # 扩展名称,用于标识扩展
7
+ "arguments": {
8
+ 'keyword': 'str', # 关键字
9
+ },
10
+ "description": "send 1 anime picture by keyword. (usage in response: /#AnimePic&萝莉#/)",
11
+ # 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
12
+ "refer_word": ["图", "pic", "Pic", "再", "还", "涩", "色", "萝莉", "元"],
13
+ # 每次消息回复中最大调用次数,不填则默认为99
14
+ "max_call_times_per_msg": 3,
15
+ # 作者信息
16
+ "author": "KroMiose",
17
+ # 版本
18
+ "version": "0.0.1",
19
+ # 扩展简介
20
+ "intro": "Sexnyan二次元图片",
21
+ }
22
+
23
+ class CustomExtension(Extension):
24
+ async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
25
+ """ 当扩展被调用时执行的函数 *由扩展自行实现*
26
+
27
+ 参数:
28
+ arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值(类型为str)}
29
+ """
30
+ custom_config:dict = self.get_custom_config() # 获取yaml中的配置信息
31
+
32
+ keyword = arg_dict.get('keyword', '')
33
+
34
+ r18 = custom_config.get('r18', False)
35
+
36
+ params = {
37
+ 'r18': r18,
38
+ 'keyword': keyword,
39
+ } if keyword else {
40
+ 'r18': r18,
41
+ }
42
+
43
+ url = "https://sex.nyan.xyz/api/v2"
44
+
45
+ try:
46
+ res = requests.get(url, params=params, verify=False, timeout=10).json().get("data")
47
+
48
+ if not res:
49
+ params['keyword'] = None
50
+ res = requests.get(url, params=params, verify=False, timeout=10).json().get("data")[0]
51
+ else:
52
+ res = res[0]
53
+
54
+ img_data = {
55
+ 'url': res.get('url'),
56
+ 'title': res.get('title'),
57
+ 'author': res.get('author'),
58
+ }
59
+ return {
60
+ 'text': f"{img_data['title']} by: {img_data['author']}", # 文本信息
61
+ 'image': img_data['url'], # 图片url
62
+ 'voice': None, # 语音url
63
+ }
64
+ except Exception as e:
65
+ return {
66
+ 'text': f"[sexnyan] 找不到关于 \"{keyword}\" 的图片",
67
+ 'image': None, # 图片url
68
+ 'voice': None, # 语音url
69
+ }
70
+
71
+ def __init__(self, custom_config: dict):
72
+ super().__init__(ext_config.copy(), custom_config)