|
from .Extension import Extension |
|
import requests |
|
|
|
|
|
ext_config:dict = { |
|
"name": "AnimePic", |
|
"arguments": { |
|
'keyword': 'str', |
|
}, |
|
"description": "send 1 anime picture by keyword. (usage in response: /#AnimePic&萝莉#/)", |
|
|
|
"refer_word": ["图", "pic", "Pic", "再", "还", "涩", "色", "萝莉", "元"], |
|
|
|
"max_call_times_per_msg": 3, |
|
|
|
"author": "KroMiose", |
|
|
|
"version": "0.0.1", |
|
|
|
"intro": "Sexnyan二次元图片", |
|
} |
|
|
|
class CustomExtension(Extension): |
|
async def call(self, arg_dict: dict, ctx_data: dict) -> dict: |
|
""" 当扩展被调用时执行的函数 *由扩展自行实现* |
|
|
|
参数: |
|
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值(类型为str)} |
|
""" |
|
custom_config:dict = self.get_custom_config() |
|
|
|
keyword = arg_dict.get('keyword', '') |
|
|
|
r18 = custom_config.get('r18', False) |
|
|
|
params = { |
|
'r18': r18, |
|
'keyword': keyword, |
|
} if keyword else { |
|
'r18': r18, |
|
} |
|
|
|
url = "https://sex.nyan.xyz/api/v2" |
|
|
|
try: |
|
res = requests.get(url, params=params, verify=False, timeout=10).json().get("data") |
|
|
|
if not res: |
|
params['keyword'] = None |
|
res = requests.get(url, params=params, verify=False, timeout=10).json().get("data")[0] |
|
else: |
|
res = res[0] |
|
|
|
img_data = { |
|
'url': res.get('url'), |
|
'title': res.get('title'), |
|
'author': res.get('author'), |
|
} |
|
return { |
|
'text': f"{img_data['title']} by: {img_data['author']}", |
|
'image': img_data['url'], |
|
'voice': None, |
|
} |
|
except Exception as e: |
|
return { |
|
'text': f"[sexnyan] 找不到关于 \"{keyword}\" 的图片", |
|
'image': None, |
|
'voice': None, |
|
} |
|
|
|
def __init__(self, custom_config: dict): |
|
super().__init__(ext_config.copy(), custom_config) |
|
|