Skip to content

Commit

Permalink
新增 自定义命令板块,实现API调用解析获取数据,能动性很高,所以也会有点安全风险
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Mar 17, 2024
1 parent 9d91cdc commit 9b775f5
Show file tree
Hide file tree
Showing 5 changed files with 514 additions and 3 deletions.
108 changes: 108 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,113 @@
}
]
},
"custom_cmd": {
"enable": false,
"type": "弹幕",
"config": [
{
"keywords": [
"天蝎座今日运势",
"天蝎座运势",
"天蝎运势"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/horoscope?type=scorpio&time=today",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['fortunetext']['all']",
"resp_template": "{keyword}:{data}"
},
{
"keywords": [
"随机笑话",
"讲个笑话",
"来个笑话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/joke",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"随机情话",
"讲个情话",
"情话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/love",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"随机骚话",
"讲个骚话",
"骚话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/sao",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"疯狂星期四",
"星期四",
"KFC",
"kfc"
],
"similarity": 1.0,
"api_url": "https://api.shadiao.pro/kfc",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['text']",
"resp_template": "{data}"
},
{
"keywords": [
"狗屁不通",
"gpbt"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/Bullshit/?title=oiapi&length=200",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['message']",
"resp_template": "{data}"
},
{
"keywords": [
"网易热评",
"网易云热评"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/NeteaseHotReviews",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['content']",
"resp_template": "{data}"
},
{
"keywords": [
"发病"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/SickL/",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['message']",
"resp_template": "{data}"
}
]
},
"translate": {
"enable": false,
"type": "baidu",
Expand Down Expand Up @@ -1334,6 +1441,7 @@
"play_audio": true,
"web_captions_printer": true,
"key_mapping": true,
"custom_cmd": true,
"trends_config": true,
"abnormal_alarm": true
},
Expand Down
108 changes: 108 additions & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,113 @@
}
]
},
"custom_cmd": {
"enable": false,
"type": "弹幕",
"config": [
{
"keywords": [
"天蝎座今日运势",
"天蝎座运势",
"天蝎运势"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/horoscope?type=scorpio&time=today",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['fortunetext']['all']",
"resp_template": "{keyword}:{data}"
},
{
"keywords": [
"随机笑话",
"讲个笑话",
"来个笑话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/joke",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"随机情话",
"讲个情话",
"情话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/love",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"随机骚话",
"讲个骚话",
"骚话"
],
"similarity": 1.0,
"api_url": "https://api.vvhan.com/api/sao",
"api_type": "GET",
"resp_data_type": "content",
"data_analysis": "resp",
"resp_template": "{data}"
},
{
"keywords": [
"疯狂星期四",
"星期四",
"KFC",
"kfc"
],
"similarity": 1.0,
"api_url": "https://api.shadiao.pro/kfc",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['text']",
"resp_template": "{data}"
},
{
"keywords": [
"狗屁不通",
"gpbt"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/Bullshit/?title=oiapi&length=200",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['message']",
"resp_template": "{data}"
},
{
"keywords": [
"网易热评",
"网易云热评"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/NeteaseHotReviews",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['data']['content']",
"resp_template": "{data}"
},
{
"keywords": [
"发病"
],
"similarity": 1.0,
"api_url": "https://oiapi.net/API/SickL/",
"api_type": "GET",
"resp_data_type": "json",
"data_analysis": "resp['message']",
"resp_template": "{data}"
}
]
},
"translate": {
"enable": false,
"type": "baidu",
Expand Down Expand Up @@ -1334,6 +1441,7 @@
"play_audio": true,
"web_captions_printer": true,
"key_mapping": true,
"custom_cmd": true,
"trends_config": true,
"abnormal_alarm": true
},
Expand Down
58 changes: 56 additions & 2 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,9 +905,9 @@ def send_request(self, url, method='GET', json_data=None, resp_data_type="json",
headers = {'Content-Type': 'application/json'}

try:
if method == 'GET':
if method in ['GET', 'get']:
response = requests.get(url, headers=headers, timeout=timeout)
elif method == 'POST':
elif method in ['POST', 'post']:
response = requests.post(url, headers=headers, data=json.dumps(json_data), timeout=timeout)
else:
raise ValueError('无效 method. 支持的 methods 为 GET 和 POST.')
Expand All @@ -920,6 +920,8 @@ def send_request(self, url, method='GET', json_data=None, resp_data_type="json",
result = response.json()
else:
result = response.content
# 使用 'utf-8' 编码来解码字节串
result = result.decode('utf-8')

return result

Expand All @@ -928,6 +930,58 @@ def send_request(self, url, method='GET', json_data=None, resp_data_type="json",
logging.error(f"请求出错: {e}")
return None

async def send_async_request(self, url, method='GET', json_data=None, resp_data_type="json", timeout=60):
"""
发送异步 HTTP 请求并返回结果
Parameters:
url (str): 请求的 URL
method (str): 请求方法,'GET' 或 'POST'
json_data (dict): JSON 数据,用于 POST 请求
resp_data_type (str): 返回数据的类型(json | content)
timeout (int): 请求超时时间
Returns:
dict|str: 包含响应的 JSON数据 | 字符串数据
"""
import aiohttp

headers = {'Content-Type': 'application/json'}

try:
# 创建 aiohttp.ClientSession
async with aiohttp.ClientSession() as session:
if method in ['GET', 'get']:
async with session.get(url, headers=headers, timeout=timeout) as response:
# 检查请求是否成功
response.raise_for_status()

if resp_data_type == "json":
# 解析响应的 JSON 数据
result = await response.json()
else:
result = await response.read()

elif method in ['POST', 'post']:
async with session.post(url, headers=headers, data=json.dumps(json_data), timeout=timeout) as response:
# 检查请求是否成功
response.raise_for_status()

if resp_data_type == "json":
# 解析响应的 JSON 数据
result = await response.json()
else:
result = await response.read()

else:
raise ValueError('无效 method. 支持的 methods 为 GET 和 POST.')

return result

except aiohttp.ClientError as e:
logging.error("请求出错: %s", e)
return None

# 请求web字幕打印机
def send_to_web_captions_printer(self, api_ip_port, data):
"""请求web字幕打印机
Expand Down
Loading

0 comments on commit 9b775f5

Please sign in to comment.