-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_source.py
80 lines (71 loc) · 2.65 KB
/
data_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
from utils.http_utils import AsyncHttpx
from hashlib import md5
import time
import random
async def getData(imageUrl, model="anime", mode=0):
"""
:param imageUrl: input Image
:param model: select Model [game, anime]
:param mode: 0-1, 0 for single, 1 for multiple
:return: result
"""
save_name = md5(imageUrl.encode("utf-8")).hexdigest() + ".png"
await AsyncHttpx.download_file(imageUrl, save_name)
files = {
'image': open(save_name, 'rb')
}
content = await AsyncHttpx.post(f"https://aiapiv2.animedb.cn/ai/api/detect?model={model}&force_one={mode}",
data=None, files=files)
content = json.loads(content.text)
return content
def buildMessage(result, mode, Model):
modelName = ""
if Model == 'game':
modelName = "游戏"
else:
modelName = "动漫"
"""
:param Model:
:param result: result from AnimeTrace
:param mode: mode
:return: message_build
"""
if mode == 0:
char = ""
for i in result['data']:
message = "人物:" + i['name'] + f"--来自{modelName}《" + i['cartoonname'] + "》" + "\n"
char += message
return char
else:
char = ""
count = 0
message_list = []
message_list.append(link(f"感谢使用AnimeTrace动漫查询引擎,这是您的{modelName}图片预测结果"))
for i in result['data']:
count += 1
message = f"第{count}个人物:" + i['char'][0]['name'] + f"--来自{modelName}《" + i['char'][0][
'cartoonname'] + "》" + "\n"
if (len(i['char']) != 1):
for idx, d in enumerate(i['char']):
if (idx == 0):
continue
message += "--其他可能性"
message += "-" + d['name'] + f"--来自{modelName}《" + d['cartoonname'] + "》" + "\n"
char += message
message_list.append(link(char, i['char'][0]['name']))
char = ""
message_list.append(link("共预测到" + str(len(result['data'])) + "个角色\n" + "感谢使用,支持列表请到官网查看!"))
return message_list
def link(string, name=None):
if name == None:
namearray = ['式宫 舞菜', '香风 智乃', "保登 心爱", "越谷 小鞠", "稻森 光香", "雪村 葵", "龙宫 礼奈", "北条 沙都子", "间宫 乃乃香"]
name = random.choice(namearray)
return {
"type": "node",
"data": {
"name": f"{name}",
"uin": f"1398087940", # 这里可以自定义
"content": string,
},
}