2023-03-15 22:27:30 +08:00
|
|
|
|
---@class Youdao: TransBackend
|
|
|
|
|
---@field uri string api uri
|
|
|
|
|
---@field salt string
|
|
|
|
|
---@field app_id string
|
|
|
|
|
---@field app_passwd string
|
|
|
|
|
---@field disable boolean
|
2023-03-16 00:03:13 +08:00
|
|
|
|
local M = {
|
|
|
|
|
uri = 'https://openapi.youdao.com/api',
|
|
|
|
|
salt = tostring(math.random(bit.lshift(1, 15))),
|
|
|
|
|
name = 'youdao',
|
|
|
|
|
}
|
2023-03-15 22:27:30 +08:00
|
|
|
|
|
2023-03-16 00:03:13 +08:00
|
|
|
|
---@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())
|
|
|
|
|
|
2023-03-16 16:07:44 +08:00
|
|
|
|
|
|
|
|
|
local chars = vim.str_utf_pos(str)
|
|
|
|
|
local count = #chars
|
|
|
|
|
local input = count <= 20 and str or
|
|
|
|
|
str:sub(1, chars[11] - 1) .. #chars .. str:sub(chars[count - 9])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- sign=sha256(应用ID+input+salt+curtime+应用密钥); 一二三四五六七八九十
|
|
|
|
|
local hash = app_id .. input .. salt .. curtime .. M.app_passwd
|
|
|
|
|
local sign = vim.fn.sha256(hash)
|
2023-03-16 00:03:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2023-03-16 11:49:26 +08:00
|
|
|
|
-- {
|
|
|
|
|
-- basic = {
|
|
|
|
|
-- explains = { "normal", "regular", "normality" },
|
|
|
|
|
-- phonetic = "zhèng cháng"
|
|
|
|
|
-- },
|
|
|
|
|
-- dict = {
|
|
|
|
|
-- url = "yddict://m.youdao.com/dict?le=eng&q=%E6%AD%A3%E5%B8%B8"
|
|
|
|
|
-- },
|
|
|
|
|
-- errorCode = "0",
|
|
|
|
|
-- isWord = true,
|
|
|
|
|
-- l = "zh-CHS2en",
|
|
|
|
|
-- mTerminalDict = {
|
|
|
|
|
-- url = "https://m.youdao.com/m/result?lang=zh-CHS&word=%E6%AD%A3%E5%B8%B8"
|
|
|
|
|
-- },
|
|
|
|
|
-- query = "正常",
|
|
|
|
|
-- requestId = "a8a40c0e-0d3b-49d5-a8fe-b1cd211ff5db",
|
|
|
|
|
-- returnPhrase = { "正常" },
|
|
|
|
|
-- speakUrl = "https://openapi.youdao.com/ttsapi?q=%E6%AD%A3%E5%B8%B8&langType=zh-CHS&sign=164F6EFF2EFFC7626FB70DBCF796AE70&salt=1678931501049&voice=4&format=mp3&appKey=1858465a8708c121&ttsVoiceStrict=false",
|
|
|
|
|
-- tSpeakUrl = "https://openapi.youdao.com/ttsapi?q=normal&langType=en-USA&sign=6A0CF2EF076EA8D82453956B33F69A51&salt=1678931501049&voice=4&format=mp3&appKey=1858465a8708c121&ttsVoiceStrict=false",
|
|
|
|
|
-- translation = { "normal" },
|
|
|
|
|
-- web = { {
|
|
|
|
|
-- key = "正常",
|
|
|
|
|
-- value = { "normal", "ordinary", "normo", "regular" }
|
|
|
|
|
-- }, {
|
|
|
|
|
-- key = "正常利润",
|
|
|
|
|
-- value = { "normal profits" }
|
|
|
|
|
-- }, {
|
|
|
|
|
-- key = "邦交正常化",
|
|
|
|
|
-- value = { "normalize relations", "normalization of diplomatic relations" }
|
|
|
|
|
-- } },
|
|
|
|
|
-- webdict = {
|
|
|
|
|
-- url = "http://mobile.youdao.com/dict?le=eng&q=%E6%AD%A3%E5%B8%B8"
|
|
|
|
|
-- }
|
|
|
|
|
-- }
|
|
|
|
|
|
2023-03-16 16:07:44 +08:00
|
|
|
|
-- {
|
|
|
|
|
-- basic = {
|
|
|
|
|
-- explains = { "normal profit" }
|
|
|
|
|
-- },
|
|
|
|
|
-- dict = {
|
|
|
|
|
-- url = "yddict://m.youdao.com/dict?le=eng&q=%E6%AD%A3%E5%B8%B8%E5%88%A9%E6%B6%A6"
|
|
|
|
|
-- },
|
|
|
|
|
-- errorCode = "0",
|
|
|
|
|
-- isWord = true,
|
|
|
|
|
-- l = "zh-CHS2en",
|
|
|
|
|
-- mTerminalDict = {
|
|
|
|
|
-- url = "https://m.youdao.com/m/result?lang=zh-CHS&word=%E6%AD%A3%E5%B8%B8%E5%88%A9%E6%B6%A6"
|
|
|
|
|
-- },
|
|
|
|
|
-- query = "正常利润",
|
|
|
|
|
-- requestId = "87a0b1bf-a5a2-46d1-8604-cd765cd06a90",
|
|
|
|
|
-- returnPhrase = { "正常利润" },
|
|
|
|
|
-- speakUrl = "https://openapi.youdao.com/ttsapi?q=%E6%AD%A3%E5%B8%B8%E5%88%A9%E6%B6%A6&langType=zh-CHS&sign=5DC3A57D7D4CB21892D0D77E6968F03D&salt=1678950274137&voice=4&format=mp3&appKey=1858465a8708c121&ttsVoiceStrict=false",
|
|
|
|
|
-- tSpeakUrl = "https://openapi.youdao.com/ttsapi?q=Normal+profit&langType=en-USA&sign=325FA5994D5D3B859DF21E3522577AFB&salt=1678950274137&voice=4&format=mp3&appKey=1858465a8708c121&ttsVoiceStrict=false",
|
|
|
|
|
-- translation = { "Normal profit" },
|
|
|
|
|
-- web = { {
|
|
|
|
|
-- key = "正常利润",
|
|
|
|
|
-- value = { "normal profits" }
|
|
|
|
|
-- }, {
|
|
|
|
|
-- key = "非正常利润",
|
|
|
|
|
-- value = { "abnormal profits" }
|
|
|
|
|
-- }, {
|
|
|
|
|
-- key = "正常利润率",
|
|
|
|
|
-- value = { "normal profit rate" }
|
|
|
|
|
-- } },
|
|
|
|
|
-- webdict = {
|
|
|
|
|
-- url = "http://mobile.youdao.com/dict?le=eng&q=%E6%AD%A3%E5%B8%B8%E5%88%A9%E6%B6%A6"
|
|
|
|
|
-- }
|
|
|
|
|
-- }
|
|
|
|
|
|
|
|
|
|
|
2023-03-16 11:49:26 +08:00
|
|
|
|
|
2023-03-16 00:03:13 +08:00
|
|
|
|
---@overload fun(TransData): TransResult
|
2023-03-16 16:07:44 +08:00
|
|
|
|
---Query Using Youdao API
|
2023-03-16 00:03:13 +08:00
|
|
|
|
---@param data TransData
|
|
|
|
|
function M.query(data)
|
|
|
|
|
local handle = function(res)
|
|
|
|
|
local status, body = pcall(vim.json.decode, res.body)
|
2023-03-16 17:17:30 +08:00
|
|
|
|
-- vim.print(body)
|
2023-03-16 16:07:44 +08:00
|
|
|
|
if not status or not body or body.errorCode ~= "0" then
|
2023-03-16 00:03:13 +08:00
|
|
|
|
data.result.youdao = false
|
2023-03-16 16:07:44 +08:00
|
|
|
|
data[#data + 1] = res
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if not body.isWord then
|
|
|
|
|
data.result.youdao = {
|
|
|
|
|
str = body.query,
|
|
|
|
|
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
|
|
|
|
}
|
2023-03-16 00:03:13 +08:00
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-16 17:17:30 +08:00
|
|
|
|
local tmp = {
|
2023-03-16 16:07:44 +08:00
|
|
|
|
title = {
|
2023-03-16 17:17:30 +08:00
|
|
|
|
word = body.query,
|
2023-03-16 16:07:44 +08:00
|
|
|
|
phonetic = body.basic.phonetic,
|
|
|
|
|
},
|
|
|
|
|
web = body.web,
|
|
|
|
|
phrases = body.phrases,
|
|
|
|
|
explains = body.basic.explains,
|
|
|
|
|
synonyms = body.synonyms,
|
|
|
|
|
translation = body.translation,
|
|
|
|
|
sentenceSample = body.sentenceSample,
|
2023-03-16 11:49:26 +08:00
|
|
|
|
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
|
|
|
|
}
|
2023-03-16 17:17:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data.result.youdao = tmp
|
2023-03-16 00:03:13 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
require('Trans').curl.get(M.uri, {
|
|
|
|
|
query = M.get_content(data),
|
|
|
|
|
callback = handle,
|
|
|
|
|
})
|
|
|
|
|
end
|
2023-03-09 22:55:04 +08:00
|
|
|
|
|
2023-03-16 00:03:13 +08:00
|
|
|
|
---@class TransBackend
|
|
|
|
|
---@field youdao Youdao
|
|
|
|
|
return M
|