微软小冰的颜值鉴定接口
把我的老铁机器人的颜值鉴定接口源码放出来,逻辑都在execute函数里面,看不懂也不解释了。
const sharp = require('sharp')
const _ = require('lodash')
const { sendMsg, Recent, localPic } = require('../qq_api')
const { rp, UA, auraCdChk, num } = require('../utils')
module.exports = {
name: '颜值',
tags: ['exec', 'aura', 'group', 'cd'],
keywords: ['好看吗'],
usage: ['(发照片后) [botNick_]颜值鉴定', '我好看吗?', '@某用户 好看吗'],
value: { 冷却时间: { def: '20秒', chk: auraCdChk } },
help: '给照片中人物的颜值打分。AT某用户可鉴定他/她的头像,发送“我好看吗”则鉴定自己的头像。\n☍ 数据接口:微软小冰 https://ux.xiaoice.com/beautyv3',
execute: async ({ name, bot, user, msg: { content: cont, ats = [], pics: [pic] = [] }, g }) => {
cont = cont.replace(/(鉴定|的|\?|?)/, '')
if (cont === '我' || ats.length) {
const q = cont === '我' ? user : ats[0]
pic = { url: `http://q1.qlogo.cn/g?b=qq&nk=${q}&s=640` }
} else if (!pic) {
pic = await Recent.get('pic', user, g)
if (!pic) { // 需要补发照片
Recent.set('cmd', user, g, name)
return sendMsg(bot, g, user, '[at]颜值鉴定需要你提供包含人脸的照片,请在20秒内发出'), 'WAITING_PIC' // eslint-disable-line
}
Recent.del('pic', user, g)
}
;(async () => { // 异步执行,避免指令执行时间过长
try {
const opts = {
jar: rp.jar(),
headers: { 'User-Agent': UA }
}
let [res, buf] = await Promise.all([
rp.head('https://ux.xiaoice.com/beautyv3', opts), // 小冰颜值首页,访问此页面仅用于填充cookies,HEAD比GET速度更快
rp.get(pic.url, { encoding: null })]) // 获取图像数据
// 从cookies中提取TraceId参数
const TraceId = JSON.parse(decodeURIComponent(opts.jar.getCookies('http://ux.xiaoice.com').find(c => c.key === 'logInfo').value)).tid
// 对非动画gif,大于200K或大于30万像素的,则缩小至30万像素以节省流量
const img = sharp(buf)
const { format, width, height } = await img.metadata()
if (format !== 'gif' && ((buf.length > 200000) || width * height > 300000)) {
const nw = ~~Math.sqrt(width * 300000 / height)
if (nw < width) buf = await img.resize(nw).jpeg({ quality: 75, chromaSubsampling: '4:2:0' }).toBuffer()
}
// base64方式上传图片
_.merge(opts, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded', Referer: 'https://ux.xiaoice.com/beautyv3' },
body: buf.toString('base64')
})
res = await rp.post('https://ux.xiaoice.com/api/image/UploadBase64?exp=1', opts)
res = JSON.parse(res) // 应答体JSON格式,包含上传后的图片host和uri
const MsgId = Date.now()
// 调用颜值鉴定接口,一般需要10秒以上,需设定较长的超时时间
_.merge(opts, {
json: true,
timeout: 45000,
headers: { 'Content-Type': 'application/json' },
body: { MsgId, CreateTime: ~~(MsgId / 1000), TraceId, Content: { imageUrl: res.Host + res.Url } }
})
res = await rp.post('https://ux.xiaoice.com/api/imageAnalyze/Process?service=beauty', opts)
// 成功获取到鉴定结果
const { imageUrl, metadata, text } = res.content
let txt = text
if (metadata.score) {
txt = `👆上面${{ male: '帅哥', female: '美女' }[metadata.gender]}的颜值有${metadata.score}分!👏`
if (metadata.FBR_Cnt) {
const fbr = Object.entries(metadata).reduce((o, [k, v]) => {
const mch = /^FBR_(Key|Score)(\d+)$/.exec(k)
if (mch) (o[+mch[2]] = o[+mch[2]] || {})[mch[1]] = v
return o
}, [])
txt += `\n评分详情:\n${num(fbr.map(f => `${f.Key}:${f.Score}分`)).join('\n')}`
}
txt += `\n⭐${text}`
if (metadata.Comment_Improve) txt += `\n💡${metadata.Comment_Improve}`
}
const lpic = await localPic(imageUrl)
sendMsg(bot, g, user, txt, lpic)
} catch (e) {
sendMsg(bot, g, user, `[at]颜值鉴定失败,错误:${e.message}`)
}
})()
return sendMsg(bot, g, user, `[at]正在鉴定颜值,大约需要20~30秒,请稍候`), 'OK' // eslint-disable-line
}
}
玩儿编程
编程也是可以玩儿的!微信公众号“玩儿编程”
推荐阅读
Cython + PyInstaller 创建 EXE 文件
参考Github上的例程简介引入Cython的目的是:将部分python源码转译成c,然后编译成.pyd格式的动态链接库,避免软件核心逻辑被轻易反编译引入PyInstaller的目的,打包成exe,可选单个文件或目录Python2.7 安装依赖...
rockswang阅读 2.5k
大数据 + VR 全景技术重塑“二手车买车场景”
行内人都知道,二手车交易的核心问题在于车况信息不透明。中国二手车交易市场制度尚不完善,长期以来缺少行业公认的车辆估值标准和车况检测标准,二手车商提供的估值和车况信息不够透明。这导致用户和车商交易双...
之家技术赞 13阅读 12k
Vue 实现人机五子棋
预览效果github源码核心代码 {代码...} 原文地址:[链接]
jigsaw赞 4阅读 3.1k
2022 星策 Summit 峰会首批嘉宾确认,火热报名中!
Start Together, Star Together ,一起开始,一起闪耀!星策社区年度最大峰会来 啦!2022 星策 Summit 是由星策开源社区主办、思否社区协办,面向企业管理层、CTO、CEO、AI 工程师、开发者的线上直播活动。本次...
MissD阅读 9.7k
「2022 中国开源创新大赛」报名进入倒计时!还没上车的小伙伴抓紧时间啦
2022 年中国开源创新大赛,是在中央网信办指导下,由中国互联网发展基金会、中国网络空间研究院、中国互联网投资基金联合主办,北京长风信息技术产业联盟承办。
MissD阅读 6.5k
预告|2022 星策 Summit FeatureStore 分论坛议程公布!
由星策开源社区主办、思否社区协办的首届“ 星策 Summit ” 即将来袭!本次大会主要面向企业管理层、CTO、CEO、AI 工程师、开发者,共涉及企业智能化转型、MLOps、FeatureStore 等多个领域,现场将特邀星策社区成员...
MissD阅读 6k
第十六届中国大数据技术大会五大分论坛顺利举办!
1 月 8 日下午,由苏州市人民政府指导、中国计算机学会主办、苏州市吴江区人民政府支持,CCF 大数据专家委员会、苏州市吴江区工信局、吴江区东太湖度假区管委会、苏州市吴江区科技局、苏州大学未来科学与工程学院...
MissD阅读 5.7k
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。