fix: better backend configuration file parser

This commit is contained in:
JuanZoran
2023-03-12 15:18:27 +08:00
parent ded28b68bc
commit 5a36ffad1c
10 changed files with 161 additions and 197 deletions

View File

@@ -1,25 +1,20 @@
local M = {}
local M = {
uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate',
salt = tostring(math.random(bit.lshift(1, 15))),
}
local Trans = require('Trans')
local baidu = Trans.conf.keys.baidu
local app_id = baidu.app_id
local app_passwd = baidu.app_passwd
local salt = tostring(math.random(bit.lshift(1, 15)))
local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
function M.get_content(data)
local tmp = app_id .. data.str .. salt .. app_passwd
local tmp = M.app_id .. data.str .. M.salt .. M.app_passwd
local sign = Trans.util.md5.sumhexa(tmp)
return {
q = data.str,
from = data.from,
to = data.to,
appid = app_id,
salt = salt,
appid = M.app_id,
salt = M.salt,
sign = sign,
}
end
@@ -33,7 +28,11 @@ end
function M.query(data)
data.engine = 'baidu'
if M.disable then
data.result.baidu = false
return
end
local handle = function(res)
local status, body = pcall(vim.json.decode, res.body)
@@ -41,7 +40,7 @@ function M.query(data)
local result = body.trans_result
if result then
-- TEST :whether multi result
assert(#result == 1, 'multi result :' .. vim.inspect(result))
assert(#result == 1)
result = result[1]
data.result.baidu = {
title = result.src,
@@ -56,7 +55,7 @@ function M.query(data)
end
Trans.wrapper.curl.get(uri, {
Trans.wrapper.curl.get(M.uri, {
query = M.get_content(data),
callback = handle,
})

View File

@@ -1,4 +1,4 @@
local M = { no_wait = true }
local M = { no_wait = true, }
local db = require 'sqlite.db'
vim.api.nvim_create_autocmd('VimLeavePre', {
@@ -13,7 +13,6 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
function M.query(data)
if data.is_word == false or data.from == 'zh' then return end
data.path = data.path or require('Trans').conf.dir .. '/ultimate.db'
data.engine = 'offline'
data.formatter = data.formatter or M.formatter