fix: fix youdao input key calculate mistake (unicode uncode)

This commit is contained in:
JuanZoran 2023-03-16 16:07:44 +08:00
parent 020876d8c4
commit 870be41bee
7 changed files with 91 additions and 91 deletions

View File

@ -26,12 +26,17 @@ function M.get_content(data)
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)
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)
return {
@ -101,27 +106,78 @@ end
-- }
-- }
-- {
-- 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"
-- }
-- }
---@overload fun(TransData): TransResult
---Query Using Baidu API
---Query Using Youdao 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
vim.print(body)
if not status or not body or body.errorCode ~= "0" then
data.result.youdao = false
data.trace = res
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,
}
return
end
if true then
vim.print(body)
data.result.youdao = false
return
end
-- TEST :whether multi result
data.result.youdao = {
['title'] = body.src,
title = {
phonetic = body.basic.phonetic,
},
web = body.web,
phrases = body.phrases,
explains = body.basic.explains,
synonyms = body.synonyms,
translation = body.translation,
sentenceSample = body.sentenceSample,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}
end
@ -135,76 +191,3 @@ end
---@class TransBackend
---@field youdao Youdao
return M
-- local GET = require("Trans.util.curl").GET
-- return function(word)
-- local isEn = word:isEn()
-- local result = {}
-- local uri = ('https://v.api.aa1.cn/api/api-fanyi-yd/index.php?msg=%s&type=%d'):format(word, isEn and 2 or 1)
-- GET(uri, {
-- callback = function(str)
-- local ok, res = pcall(vim.json.decode, str)
-- if not ok or not res or not res.text or isEn and res.text:isEn() then
-- result[1] = false
-- return
-- end
-- result[1] = {
-- title = { word = word },
-- [isEn and 'translation' or 'definition'] = res.text,
-- }
-- if result.callback then
-- result.callback(result[1])
-- end
-- end
-- })
-- return result
-- end
-- local youdao = require("Trans").conf.engine.youdao
-- local uri = 'https://openapi.youdao.com/api'
-- local salt = tostring(math.random(bit.lshift(1, 15)))
-- local appid = youdao.appid
-- local appPasswd = youdao.appPasswd
-- local post = require('Trans.util.curl').POST
-- local function get_field(word)
-- -- local to = isEn and 'zh-'
-- local len = #word
-- local curtime = tostring(os.time())
-- local input = len > 20 and
-- word:sub(1, 10) .. len .. word:sub(-10) or word
-- -- sign=sha256(应用ID+input+salt+curtime+应用密钥)
-- local hash = appid .. input .. salt .. curtime .. appPasswd
-- local sign = vim.fn.sha256(hash)
-- return {
-- q = word,
-- from = 'auto',
-- to = 'zh-CHS',
-- signType = 'v3',
-- appKey = appid,
-- salt = salt,
-- curtime = curtime,
-- sign = sign,
-- }
-- end
-- return function(word)
-- -- return result
-- -- local field = get_field(word)
-- -- local output = post(uri, {
-- -- body = field,
-- -- })
-- -- if output.exit == 0 and output.status == 200 then
-- -- local result = vim.fn.json_decode(output.body)
-- -- if result and result.errorCode == 0 then
-- -- --- TODO :
-- -- end
-- -- end
-- end

View File

@ -71,6 +71,11 @@ return {
},
---@type table<string, string[]> order to display translate result
order = {
default = {
'str',
'translation',
'definition',
},
offline = {
'title',
'tag',

View File

@ -75,7 +75,7 @@ function curl.get(uri, opts)
}
end
-- vim.pretty_print(table.concat(cmd, ' '))
-- vim.print(table.concat(cmd, ' '))
vim.fn.jobstart(table.concat(cmd, ' '), {
stdin = 'null',
on_stdout = on_stdout,

View File

@ -54,9 +54,11 @@ local function process(opts)
local data = Trans.data.new(opts)
if strategy[Trans.conf.query](data) then
Trans.cache[data.str] = data
end
data.frontend:process(data)
data.frontend:process(data)
else
data.frontend:fallback()
end
end

View File

@ -143,6 +143,8 @@ function M.main_loop(func)
coroutine.yield()
end
---@class Trans
---@field util TransUtil
return M

View File

@ -93,6 +93,14 @@ function M:init_window(opts)
return self.window
end
---Get Formatted icon text
---@param format string format string
---@return string formatted text
---@return integer _ replaced count
function M:icon_format(format)
return format:gsub('{{(%w+)}}', self.opts.icon, 1)
end
---Get Check function for waiting
---@return function
function M:wait()
@ -131,9 +139,10 @@ function M:fallback()
local buffer = self.buffer
local opts = self.opts
buffer:wipe()
local fallback_msg = opts.fallback_message:gsub('{{(%w+)}}', opts.icon)
local opts = self.opts
local fallback_msg = self:icon_format(opts.fallback_message)
-- TODO :Center
buffer[1] = Trans.util.center(fallback_msg, opts.width)

View File

@ -13,7 +13,6 @@ end
---@type table<string, fun(hover:TransHover, result: TransResult)>
local default = {
str = function(hover, result)
-- TODO :
hover.buffer:setline(it(result.str, 'TransWord'))
end,
translation = function(hover, result)
@ -148,7 +147,7 @@ strategy.offline = {
---@class TransHover
---@field load fun(hover: TransHover, result: TransResult, name: string, order: string[])
return function(hover, result, name, order)
order = order or { 'str', 'translation', 'definition' }
order = order or hover.opts.order.default
local method = strategy[name]