2023-03-14 22:30:25 +08:00
|
|
|
|
---@class Baidu: TransBackend
|
|
|
|
|
---@field uri string api uri
|
|
|
|
|
---@field salt string
|
|
|
|
|
---@field app_id string
|
|
|
|
|
---@field app_passwd string
|
|
|
|
|
---@field disable boolean
|
2023-03-12 15:18:27 +08:00
|
|
|
|
local M = {
|
|
|
|
|
uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate',
|
|
|
|
|
salt = tostring(math.random(bit.lshift(1, 15))),
|
2023-03-12 21:33:00 +08:00
|
|
|
|
name = 'baidu',
|
2023-03-12 15:18:27 +08:00
|
|
|
|
}
|
2023-01-30 12:01:58 +08:00
|
|
|
|
|
2023-03-12 11:23:29 +08:00
|
|
|
|
local Trans = require('Trans')
|
|
|
|
|
|
2023-03-14 22:30:25 +08:00
|
|
|
|
|
|
|
|
|
---@class BaiduQuery
|
|
|
|
|
---@field q string
|
|
|
|
|
---@field from string
|
|
|
|
|
---@field to string
|
|
|
|
|
---@field appid string
|
|
|
|
|
---@field salt string
|
|
|
|
|
---@field sign string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---Get content for query
|
|
|
|
|
---@param data TransData
|
|
|
|
|
---@return BaiduQuery
|
2023-03-11 00:24:48 +08:00
|
|
|
|
function M.get_content(data)
|
2023-03-12 15:18:27 +08:00
|
|
|
|
local tmp = M.app_id .. data.str .. M.salt .. M.app_passwd
|
2023-03-12 11:23:29 +08:00
|
|
|
|
local sign = Trans.util.md5.sumhexa(tmp)
|
|
|
|
|
|
2023-01-30 12:01:58 +08:00
|
|
|
|
return {
|
2023-03-09 19:42:41 +08:00
|
|
|
|
q = data.str,
|
|
|
|
|
from = data.from,
|
|
|
|
|
to = data.to,
|
2023-03-12 15:18:27 +08:00
|
|
|
|
appid = M.app_id,
|
|
|
|
|
salt = M.salt,
|
2023-01-30 12:01:58 +08:00
|
|
|
|
sign = sign,
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-09 22:55:04 +08:00
|
|
|
|
-- {
|
|
|
|
|
-- body = '{"from":"en","to":"zh","trans_result":[{"src":"require","dst":"\\u8981\\u6c42"}]}',
|
|
|
|
|
-- exit = 0,
|
|
|
|
|
-- headers = { "Content-Type: application/json", "Date: Thu, 09 Mar 2023 14:01:09 GMT", 'P3p: CP=" OTI DSP COR IVA OUR IND COM "', "Server: Apache", "Set-Cookie: BAIDUID=CB6D99CCD3B5F5278B5BE9428F002FC3:FG=1; expires=Fri, 08-Mar-24 14:01:09 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1", "Tracecode: 00696104432377504778030922", "Content-Length: 79", "", "" },
|
|
|
|
|
-- status = 200
|
|
|
|
|
-- }
|
|
|
|
|
|
2023-03-14 22:30:25 +08:00
|
|
|
|
---@overload fun(TransData): TransResult
|
|
|
|
|
---Query Using Baidu API
|
|
|
|
|
---@param data TransData
|
2023-03-11 00:24:48 +08:00
|
|
|
|
function M.query(data)
|
2023-03-12 15:18:27 +08:00
|
|
|
|
if M.disable then
|
|
|
|
|
data.result.baidu = false
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-09 19:42:41 +08:00
|
|
|
|
|
2023-03-09 22:55:04 +08:00
|
|
|
|
local handle = function(res)
|
|
|
|
|
local status, body = pcall(vim.json.decode, res.body)
|
2023-03-14 22:30:25 +08:00
|
|
|
|
if not status or not body then
|
|
|
|
|
data.result.baidu = false
|
|
|
|
|
data.trace = res
|
|
|
|
|
return
|
2023-03-09 22:55:04 +08:00
|
|
|
|
end
|
|
|
|
|
|
2023-03-14 22:30:25 +08:00
|
|
|
|
|
|
|
|
|
local result = body.trans_result
|
|
|
|
|
if result then
|
|
|
|
|
-- TEST :whether multi result
|
|
|
|
|
assert(#result == 1)
|
|
|
|
|
result = result[1]
|
|
|
|
|
data.result.baidu = {
|
|
|
|
|
['title'] = result.src,
|
|
|
|
|
[data.from == 'en' and 'translation' or 'definition'] = result.dst,
|
|
|
|
|
}
|
|
|
|
|
end
|
2023-03-09 22:55:04 +08:00
|
|
|
|
end
|
|
|
|
|
|
2023-03-12 11:23:29 +08:00
|
|
|
|
|
2023-03-13 19:50:28 +08:00
|
|
|
|
Trans.curl.get(M.uri, {
|
2023-03-09 22:55:04 +08:00
|
|
|
|
query = M.get_content(data),
|
|
|
|
|
callback = handle,
|
|
|
|
|
})
|
2023-01-30 12:01:58 +08:00
|
|
|
|
end
|
2023-02-07 16:44:22 +08:00
|
|
|
|
|
2023-03-14 22:30:25 +08:00
|
|
|
|
---@class TransBackend
|
|
|
|
|
---@field baidu Baidu
|
2023-03-09 19:42:41 +08:00
|
|
|
|
return M
|
|
|
|
|
|
|
|
|
|
-- -- NOTE :free tts:
|
|
|
|
|
-- -- https://zj.v.api.aa1.cn/api/baidu-01/?msg=我爱你&choose=0&su=100&yd=5
|
|
|
|
|
-- -- 选择转音频的人物,女生1 输入0 | 女生2输入:5|男生1 输入:1|男生2 输入:2|男生3 输入:3
|