refactor: support async online query engine:baidu
This commit is contained in:
@@ -1,74 +1,63 @@
|
||||
local M = {}
|
||||
|
||||
local baidu = require('Trans').conf.engine.baidu
|
||||
local appid = baidu.appid
|
||||
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'
|
||||
|
||||
|
||||
M.request_headers = function(data)
|
||||
local tmp = appid .. data.str .. salt .. app_passwd
|
||||
M.get_content = function(data)
|
||||
local tmp = app_id .. data.str .. salt .. app_passwd
|
||||
local sign = require('Trans.util.md5').sumhexa(tmp)
|
||||
return {
|
||||
q = data.str,
|
||||
from = data.from,
|
||||
to = data.to,
|
||||
appid = appid,
|
||||
appid = app_id,
|
||||
salt = salt,
|
||||
sign = sign,
|
||||
}
|
||||
end
|
||||
|
||||
-- {
|
||||
-- body = '{"from":"en","to":"zh","trans_result":[{"src":"require","dst":"\\u8981\\u6c42"}]}',
|
||||
-- exit = 0,
|
||||
-- headers = { "Content-Type: application/json", "Date: Thu, 09 Mar 2023 14:01:09 GMT", 'P3p: CP=" OTI DSP COR IVA OUR IND COM "', "Server: Apache", "Set-Cookie: BAIDUID=CB6D99CCD3B5F5278B5BE9428F002FC3:FG=1; expires=Fri, 08-Mar-24 14:01:09 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1", "Tracecode: 00696104432377504778030922", "Content-Length: 79", "", "" },
|
||||
-- status = 200
|
||||
-- }
|
||||
|
||||
|
||||
M.query = function(data)
|
||||
data.engine = 'baidu'
|
||||
|
||||
require('Trans.wrapper.curl').POST {
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
if status and body then
|
||||
local result = body.trans_result
|
||||
if result then
|
||||
-- TEST :whether multi result
|
||||
assert(#result == 1, 'multi result :' .. vim.inspect(result))
|
||||
result = result[1]
|
||||
data.result = {
|
||||
title = result.src,
|
||||
translation = result.dst,
|
||||
}
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
}
|
||||
data.result = false
|
||||
data.error = res
|
||||
end
|
||||
|
||||
require('plenary.curl').get(uri, {
|
||||
query = M.get_content(data),
|
||||
callback = handle,
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
||||
-- local post = require('Trans.util.curl').POST
|
||||
|
||||
-- ---返回一个channel
|
||||
-- ---@param word string
|
||||
-- ---@return table
|
||||
-- return function(word)
|
||||
-- local isEn = word:isEn()
|
||||
-- local query = get_field(word, isEn)
|
||||
-- local result = {}
|
||||
|
||||
-- post(uri, {
|
||||
-- data = query,
|
||||
-- headers = {
|
||||
-- content_type = "application/x-www-form-urlencoded",
|
||||
-- },
|
||||
-- callback = function(str)
|
||||
-- local ok, res = pcall(vim.json.decode, str)
|
||||
-- if ok and res and res.trans_result then
|
||||
-- result[1] = {
|
||||
-- title = { word = word },
|
||||
-- [isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
|
||||
-- }
|
||||
|
||||
-- if result.callback then
|
||||
-- result.callback(result[1])
|
||||
-- end
|
||||
-- else
|
||||
-- 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
|
||||
|
@@ -13,7 +13,7 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
M.query = function(data)
|
||||
if data.is_word == false or data.from == 'zh' then return end
|
||||
|
||||
data.path = vim.fn.expand(data.path or require('Trans').conf.db_path)
|
||||
data.path = vim.fn.expand(data.path or require('Trans').conf.dir .. '/ultimate.db')
|
||||
data.engine = 'offline'
|
||||
data.formatter = data.formatter or M.formatter
|
||||
data.query_field = data.query_field or M.query_field
|
||||
|
@@ -1,31 +1,31 @@
|
||||
local GET = require("Trans.util.curl").GET
|
||||
-- local GET = require("Trans.util.curl").GET
|
||||
|
||||
return function(word)
|
||||
local isEn = word:isEn()
|
||||
local result = {}
|
||||
-- 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
|
||||
-- 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,
|
||||
}
|
||||
-- result[1] = {
|
||||
-- title = { word = word },
|
||||
-- [isEn and 'translation' or 'definition'] = res.text,
|
||||
-- }
|
||||
|
||||
if result.callback then
|
||||
result.callback(result[1])
|
||||
end
|
||||
end
|
||||
})
|
||||
-- if result.callback then
|
||||
-- result.callback(result[1])
|
||||
-- end
|
||||
-- end
|
||||
-- })
|
||||
|
||||
return result
|
||||
end
|
||||
-- return result
|
||||
-- end
|
||||
|
||||
-- local youdao = require("Trans").conf.engine.youdao
|
||||
-- local uri = 'https://openapi.youdao.com/api'
|
||||
|
Reference in New Issue
Block a user