From da1f847bd0a5e42f2d83fffd4a85de822a5c7598 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Thu, 9 Mar 2023 22:55:04 +0800 Subject: [PATCH] refactor: support async online query engine:baidu --- lua/Trans/backend/baidu.lua | 79 +++++++++++++----------------- lua/Trans/backend/offline.lua | 2 +- lua/Trans/backend/youdao.lua | 46 +++++++++--------- lua/Trans/core/conf.lua | 2 +- lua/Trans/core/install.lua | 0 lua/Trans/core/translate.lua | 18 +++++-- lua/Trans/health.lua | 91 ++++++++++++++++++----------------- 7 files changed, 120 insertions(+), 118 deletions(-) create mode 100644 lua/Trans/core/install.lua diff --git a/lua/Trans/backend/baidu.lua b/lua/Trans/backend/baidu.lua index b521b23..833747d 100644 --- a/lua/Trans/backend/baidu.lua +++ b/lua/Trans/backend/baidu.lua @@ -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 diff --git a/lua/Trans/backend/offline.lua b/lua/Trans/backend/offline.lua index 14fdf24..6d3cdec 100644 --- a/lua/Trans/backend/offline.lua +++ b/lua/Trans/backend/offline.lua @@ -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 diff --git a/lua/Trans/backend/youdao.lua b/lua/Trans/backend/youdao.lua index ac9fe66..5106249 100644 --- a/lua/Trans/backend/youdao.lua +++ b/lua/Trans/backend/youdao.lua @@ -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' diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 819401e..c50fe7e 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -60,7 +60,7 @@ return { -- no = '❌' }, theme = 'default', - db_path = '$HOME/.vim/dict/ultimate.db', + dir = '$HOME/.vim/dict', -- float = { -- width = 0.8, -- height = 0.8, diff --git a/lua/Trans/core/install.lua b/lua/Trans/core/install.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 625b278..d8a492e 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -24,10 +24,22 @@ process = function(opts) end - local res = require('Trans.backend').offline.query(data) - -- vim.pretty_print(res) + require('Trans.backend').baidu.query(data) + local thread = coroutine.running() + local resume = function() + coroutine.resume(thread) + end + + local time = 0 + while data.result == nil do + vim.defer_fn(resume, 400) + time = time + 1 + print('waiting' .. ('.'):rep(time)) + coroutine.yield() + end + vim.pretty_print(data) M.translate = coroutine.wrap(process) end -return process +return coroutine.wrap(process) diff --git a/lua/Trans/health.lua b/lua/Trans/health.lua index eac5f3d..43ebaa5 100644 --- a/lua/Trans/health.lua +++ b/lua/Trans/health.lua @@ -1,79 +1,80 @@ -local M = {} - -M.check = function() +local check = function() local health = vim.health local ok = health.report_ok local warn = health.report_warn local error = health.report_error - local has = vim.fn.has local executable = vim.fn.executable -- INFO :Check neovim version if has('nvim-0.9') == 1 then - ok [[ - you have Trans.nvim with full features in neovim-nightly - ]] + ok [[You have [neovim-nightly] ]] else - warn [[ - Trans Title requires Neovim 0.9 or newer - See neovim-nightly: https://github.com/neovim/neovim/releases/tag/nightly + warn [[Trans Title requires Neovim 0.9 or newer + See neovim-nightly: [https://github.com/neovim/neovim/releases/tag/nightly] ]] end - -- INFO :Check Sqlite - local has_sqlite = pcall(require, 'sqlite') - if has_sqlite then - ok [[ - Dependency sqlite.lua is installed - ]] - else - error [[ - Dependency sqlite.lua can't work correctly - Please Read the doc in github carefully - ]] + -- INFO :Check plugin dependencies + local plugin_dependencies = { + 'plenary', + 'sqlite', + } + + for _, dep in ipairs(plugin_dependencies) do + if pcall(require, dep) then + ok(string.format('Dependency [%s] is installed', dep)) + else + error(string.format('Dependency [%s] is not installed', dep)) + end end - if executable('sqlite3') then - ok [[ - Dependency sqlite3 found - ]] + -- INFO :Check binary dependencies + local binary_dependencies = { + 'curl', + 'sqlite3', + } + + if has('linux') == 1 then + binary_dependencies[3] = 'festival' + elseif has('mac') == 1 then + binary_dependencies[3] = 'say' else - error [[ - Dependency sqlite3 not found - ]] + binary_dependencies[3] = 'node' + end + + for _, dep in ipairs(binary_dependencies) do + if executable(dep) == 1 then + ok(string.format('Binary dependency [%s] is installed', dep)) + else + error(string.format('Binary dependency [%s] is not installed', dep)) + end end - -- INFO :Check stardict - local db_path = vim.fn.expand(require('Trans').conf.db_path) + -- INFO :Check ultimate.db + local db_path = vim.fn.expand(require('Trans').conf.dir .. '/ultimate.db') if vim.fn.filereadable(db_path) == 1 then - ok [[ - Stardict database found - ]] + ok [[ultimate database found ]] else - error [[ - Stardict database not found - Please check the doc in github: https://github.com/JuanZoran/Trans.nvim + error [[Stardict database not found + Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim] ]] end -- INFO :Check Engine configuration file - local path = vim.fn.expand("$HOME/.vim/dict/Trans.json") + local path = vim.fn.expand(require('Trans').conf.dir .. '/Trans.json') local file = io.open(path, "r") - local valid = vim.json.decode(file:read("*a")) + local valid = file and pcall(vim.json.decode, file:read("*a")) if valid then - ok [[ - Engine configuration file found and valid - ]] + ok [[Engine configuration file found and valid ]] else - error [[ - Engine configuration file not found or invalid - Please check the doc in github: https://github.com/JuanZoran/Trans.nvim + error [[Engine configuration file not found or invalid + Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim] ]] end end -return M +return { check = check }