refactor: use online query method template instead of backend.query todo online query

This commit is contained in:
JuanZoran
2023-03-18 21:31:14 +08:00
parent b2cafe3448
commit 52d2741804
7 changed files with 119 additions and 205 deletions

View File

@@ -1,16 +1,17 @@
---@class Baidu: TransBackend
---@class Baidu: TransOnlineBackend
---@field uri string api uri
---@field salt string
---@field app_id string
---@field app_passwd string
---@field disable boolean
local M = {
uri = "https://fanyi-api.baidu.com/api/trans/vip/translate",
salt = tostring(math.random(bit.lshift(1, 15))),
name = "baidu",
uri = "https://fanyi-api.baidu.com/api/trans/vip/translate",
salt = tostring(math.random(bit.lshift(1, 15))),
name = "baidu",
method = 'get',
}
local Trans = require("Trans")
---@class BaiduQuery
@@ -24,7 +25,7 @@ local Trans = require("Trans")
---Get content for query
---@param data TransData
---@return BaiduQuery
function M.get_content(data)
function M.get_query(data)
local tmp = M.app_id .. data.str .. M.salt .. M.app_passwd
local sign = Trans.util.md5.sumhexa(tmp)
@@ -38,42 +39,21 @@ function M.get_content(data)
}
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
-- }
---@overload fun(TransData): TransResult
---Query Using Baidu 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
data.trace = res
data.result.baidu = false
return
end
---@param body table BaiduQuery Response
---@return table|false
function M.formatter(body, data)
local result = body.trans_result
if not result then return false end
local result = body.trans_result
if result then
-- TEST :whether multi result
assert(#result == 1)
result = result[1]
data.result.baidu = {
["str"] = result.src,
[data.from == "en" and "translation" or "definition"] = { result.dst },
}
end
end
Trans.curl.get(M.uri, {
query = M.get_content(data),
callback = handle,
})
-- TEST :whether multi result
assert(#result == 1)
result = result[1]
return {
str = result.src,
[data.from == "en" and "translation" or "definition"] = { result.dst },
}
end
---@class TransBackend
@@ -83,3 +63,10 @@ return M
-- -- 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
-- {
-- 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
-- }

View File

@@ -1,9 +1,4 @@
---@class iCiba: TransBackend
---@field uri string api uri
---@field salt string
---@field app_id string
---@field app_passwd string
---@field disable boolean
---@class iCiba: TransOnlineBackend
local M = {
uri = 'https://dict-mobile.iciba.com/interface/index.php',
name = 'iciba',
@@ -16,7 +11,7 @@ local M = {
---@field appid string
---@field salt string
---@field sign string
function M.get_content(data)
function M.get_query(data)
return {
word = data.str,
is_need_mean = '1',
@@ -25,47 +20,14 @@ function M.get_content(data)
}
end
---@overload fun(TransData): TransResult
function M.query(data)
local handle = function(res)
local status, body = pcall(vim.json.decode, res.body)
vim.print(body)
if true and not status or not body or body.errorCode ~= "0" then
data.result.iciba = false
data[#data + 1] = res
return
end
-- if not body.isWord then
-- data.result.youdao = {
-- title = body.query,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
-- return
-- end
-- local tmp = {
-- title = {
-- word = body.query,
-- phonetic = body.basic.phonetic,
-- },
-- web = body.web,
-- explains = body.basic.explains,
-- -- phrases = body.phrases,
-- -- synonyms = body.synonyms,
-- -- sentenceSample = body.sentenceSample,
-- [data.from == 'en' and 'translation' or 'definition'] = body.translation,
-- }
-- data.result.iciba = tmp
end
require('Trans').curl.get(M.uri, {
query = M.get_content(data),
callback = handle,
})
function M.formatter(body, data)
print('TODO')
-- if true and not status or not body or body.errorCode ~= "0" then
-- data.result.iciba = false
-- data[#data + 1] = res
-- return
-- end
end
-- {

View File

@@ -1,13 +1,14 @@
---@class Youdao: TransBackend
---@class Youdao: TransOnlineBackend
---@field uri string api uri
---@field salt string
---@field app_id string
---@field app_passwd string
---@field disable boolean
local M = {
uri = 'https://openapi.youdao.com/api',
salt = tostring(math.random(bit.lshift(1, 15))),
name = 'youdao',
uri = 'https://openapi.youdao.com/api',
salt = tostring(math.random(bit.lshift(1, 15))),
name = 'youdao',
method = 'get',
}
---@class YoudaoQuery
@@ -22,7 +23,7 @@ local M = {
---Get content for query
---@param data TransData
---@return YoudaoQuery
function M.get_content(data)
function M.get_query(data)
local str = data.str
local app_id = M.app_id
local salt = M.salt
@@ -57,28 +58,28 @@ local function check_untracked_field(body)
"phonetic",
'usPhonetic',
"ukPhonetic",
"text", -- text 短语
"explain", -- String Array 词义解释列表
"wordFormats", -- Object Array 单词形式变化列表
"name", -- String 形式名称,例如:复数
"phrase", -- String 词组
"meaning", -- String 含义
"synonyms", -- JSONObject 近义词
"pos", -- String 词性
"words", -- String Array 近义词列表
"trans", -- String 释义
"antonyms", -- ObjectArray 反义词
"relatedWords", -- JSONArray 相关词
"wordNet", -- JSONObject 汉语词典网络释义
"phonetic", -- String 发音
"meanings", -- ObjectArray 释义
"meaning", -- String 释义
"example", -- array 示例
"text", -- text 短语
"explain", -- String Array 词义解释列表
"wordFormats", -- Object Array 单词形式变化列表
"name", -- String 形式名称,例如:复数
"phrase", -- String 词组
"meaning", -- String 含义
"synonyms", -- JSONObject 近义词
"pos", -- String 词性
"words", -- String Array 近义词列表
"trans", -- String 释义
"antonyms", -- ObjectArray 反义词
"relatedWords", -- JSONArray 相关词
"wordNet", -- JSONObject 汉语词典网络释义
"phonetic", -- String 发音
"meanings", -- ObjectArray 释义
"meaning", -- String 释义
"example", -- array 示例
"sentenceSample", -- text 例句
"sentence", -- text 例句
"sentenceBold", -- text 将查询内容加粗的例句
"wfs", -- text 单词形式变化
"exam_type", -- text 考试类型
"sentence", -- text 例句
"sentenceBold", -- text 将查询内容加粗的例句
"wfs", -- text 单词形式变化
"exam_type", -- text 考试类型
}
for _, f in ipairs(field) do
if body[f] then
@@ -129,58 +130,41 @@ end
---@overload fun(TransData): TransResult
---Query Using Youdao API
---@param data TransData
function M.query(data)
local handle = function(res)
local status, body = pcall(vim.json.decode, res.body)
-- vim.print(body)
if not status or not body or body.errorCode ~= "0" then
if not require('Trans').conf.debug then M.debug(body) end
data.result.youdao = false
data[#data + 1] = res
return
end
check_untracked_field(body)
if not body.isWord then
data.result.youdao = {
title = body.query,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}
return
end
local tmp = {
title = {
word = body.query,
phonetic = body.basic.phonetic,
},
web = body.web,
explains = body.basic.explains,
-- phrases = body.phrases,
-- synonyms = body.synonyms,
-- sentenceSample = body.sentenceSample,
---@param body table Youdao ouput
---@param data TransData Data obj
---@return table|false?
function M.formatter(body, data)
if body.errorCode ~= "0" then return false end
check_untracked_field(body)
if not body.isWord then
return {
title = body.query,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}
data.result.youdao = tmp
end
require('Trans').curl.get(M.uri, {
query = M.get_content(data),
callback = handle,
})
return {
title = {
word = body.query,
phonetic = body.basic.phonetic,
},
web = body.web,
explains = body.basic.explains,
-- phrases = body.phrases,
-- synonyms = body.synonyms,
-- sentenceSample = body.sentenceSample,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}
end
---@class TransBackend
---@field youdao Youdao
return M
-- INFO :Query Result Example
-- {
-- basic = {
-- explains = { "normal", "regular", "normality" },