Merge branch 'experimental'

This commit is contained in:
JuanZoran
2023-02-03 15:38:05 +08:00
15 changed files with 1118 additions and 981 deletions

View File

@@ -41,19 +41,25 @@ return function(word)
callback = function(str)
local ok, res = pcall(vim.json.decode, str)
if ok and res and res.trans_result then
result.value = {
word = word,
result[1] = {
title = { word = word },
[isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
}
if result.callback then
result.callback(result.value)
result.callback(result[1])
end
else
result.value = false
result[1] = false
end
end,
})
return result
end
-- 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

View File

@@ -8,7 +8,7 @@ local path = require('Trans').conf.db_path
local dict = db:open(path)
vim.api.nvim_create_autocmd('VimLeavePre', {
group = require("Trans").augroup,
once = true,
callback = function()
if db:isopen() then
db:close()
@@ -16,11 +16,10 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
end
})
return function(word)
local res = dict:select('stardict', {
where = {
word = word,
},
local res = (dict:select('stardict', {
where = { word = word, },
keys = {
'word',
'phonetic',
@@ -33,6 +32,16 @@ return function(word)
'exchange',
},
limit = 1,
})
return res[1]
}))[1]
if res then
res.title = {
word = res.word,
oxford = res.oxford,
collins = res.collins,
phonetic = res.phonetic,
}
end
return res
end

View File

@@ -1,48 +1,74 @@
local youdao = require("Trans").conf.engine.youdao
local appKey = youdao.appKey
local appPasswd = youdao.appPasswd
local uri = 'https://openapi.youdao.com/api'
local salt = tostring(math.random(bit.rshift(1, 5)))
local ok, curl = pcall(require, 'plenary.curl')
if not ok then
error('plenary not found')
end
local function get_field(word)
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 = appKey .. input .. salt .. curtime .. appPasswd
local sign = vim.fn.sha256(hash)
return {
q = word,
from = 'auto',
to = 'zh-CHS',
signType = 'v3',
appKey = appKey,
salt = salt,
curtime = curtime,
sign = sign,
}
end
local GET = require("Trans.util.curl").GET
return function(word)
-- return result
local field = get_field(word)
local output = curl.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 :
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
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