feat: add youdao backend file
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
---@field app_id string
|
||||
---@field app_passwd string
|
||||
---@field disable boolean
|
||||
|
||||
local M = {
|
||||
uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate',
|
||||
salt = tostring(math.random(bit.lshift(1, 15))),
|
||||
@ -12,7 +13,6 @@ local M = {
|
||||
|
||||
local Trans = require('Trans')
|
||||
|
||||
|
||||
---@class BaiduQuery
|
||||
---@field q string
|
||||
---@field from string
|
||||
@ -50,12 +50,6 @@ end
|
||||
---Query Using Baidu API
|
||||
---@param data TransData
|
||||
function M.query(data)
|
||||
if M.disable then
|
||||
data.result.baidu = false
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
if not status or not body then
|
||||
|
@ -4,8 +4,86 @@
|
||||
---@field app_id string
|
||||
---@field app_passwd string
|
||||
---@field disable boolean
|
||||
local M = {
|
||||
uri = 'https://openapi.youdao.com/api',
|
||||
salt = tostring(math.random(bit.lshift(1, 15))),
|
||||
name = 'youdao',
|
||||
}
|
||||
|
||||
---@class YoudaoQuery
|
||||
---@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 YoudaoQuery
|
||||
function M.get_content(data)
|
||||
local str = data.str
|
||||
local app_id = M.app_id
|
||||
local salt = M.salt
|
||||
local curtime = tostring(os.time())
|
||||
local input = #str > 20 and
|
||||
str:sub(1, 10) .. #str .. str:sub(-10) or str
|
||||
|
||||
-- sign=sha256(应用ID+input+salt+curtime+应用密钥);
|
||||
local hash = app_id .. input .. salt .. curtime .. M.app_passwd
|
||||
local sign = vim.fn.sha256(hash)
|
||||
|
||||
|
||||
return {
|
||||
q = str,
|
||||
to = data.from == 'zh' and 'en' or 'zh-CHS',
|
||||
from = 'auto',
|
||||
signType = 'v3',
|
||||
appKey = app_id,
|
||||
salt = M.salt,
|
||||
curtime = curtime,
|
||||
sign = sign,
|
||||
}
|
||||
end
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---Query Using Baidu API
|
||||
---@param data TransData
|
||||
function M.query(data)
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
if not status or not body then
|
||||
data.result.youdao = false
|
||||
data.trace = res
|
||||
return
|
||||
end
|
||||
|
||||
if true then
|
||||
vim.print(body)
|
||||
return
|
||||
end
|
||||
|
||||
local result = body.trans_result
|
||||
if result then
|
||||
-- TEST :whether multi result
|
||||
assert(#result == 1)
|
||||
result = result[1]
|
||||
data.result.youdao = {
|
||||
['title'] = result.src,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = { result.dst },
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
require('Trans').curl.get(M.uri, {
|
||||
query = M.get_content(data),
|
||||
callback = handle,
|
||||
})
|
||||
end
|
||||
|
||||
---@class TransBackend
|
||||
---@field youdao Youdao
|
||||
return M
|
||||
-- local GET = require("Trans.util.curl").GET
|
||||
-- return function(word)
|
||||
-- local isEn = word:isEn()
|
||||
|
Reference in New Issue
Block a user