有道论文翻译接口,python版和lua版
论文翻译接口python版
import requests
import hashlib
from urllib.parse import quote
def get_md5(s,is_hex=True):
md5=hashlib.md5()
md5.update(s.encode())
if is_hex:
return md5.hexdigest()
return md5.digest()
def translate(source_url,from_='en',to='zh-CHS'):
params = {
'from': from_,
'to':to,
'field': '0',
'product':'arxivweb',
'appVersion': '1.0.0',
'client': 'web',
'keyid': 'arxivweb',
'keyfrom': 'web',
'version': '1.0.0',
'arxivUrl': source_url
}
# 签名字符串
sorted_params = sorted(params.items(), key=lambda x: x[0])
encoded_params = '&'.join([f"{key}={value}" for key, value in sorted_params])+'&key=r*bWYmKw0Z@$1^fEk3xIxKqb!3HMTyI'
# 获取签名
sign=get_md5(encoded_params)
# 请求参数中添加sign
params.update({'sign':sign})
return requests.get('https://fanyi.youdao.com/fanyilatexweb/arxiv/translate', params=params).json()
if __name__=='__main__':
url=input('请输入需要翻译的论文地址:')
result=translate(url)
print('翻译后的文件下载地址:'+result['data']['targetPdfPath'])
论文翻译接口lua版
使用
ManaluaX
打包成apk
- 布局代码
{
LinearLayout;
layout_height="fill";
orientation="vertical";
layout_width="fill";
gravity="left";
{
LinearLayout;
layout_height="wrap_content";
layout_width="match_parent";
{
EditText;
layout_weight="19";
layout_marginLeft="10";
id="source";
};
{
Button;
text="翻译";
id="transBtn";
layout_margin="10";
layout_weight="1";
};
};
{
LinearLayout;
layout_height="match_parent";
layout_width="match_parent";
orientation="vertical";
{
LinearLayout;
gravity="right";
layout_width="match_parent";
{
TextView;
text="浏览器打开";
textColor="#29B6F6";
id="web";
layout_marginRight="15";
};
};
{
TextView;
textIsSelectable=true;
padding="10";
id="result";
layout_height="match_parent";
layout_width="match_parent";
};
};
};
- 主要代码
require "import"
import "manalua"
import "layout.main"
activity.setTitle("有道论文翻译")
activity.setTheme(android.R.style.Theme_Material_Light)
activity.setContentView(loadlayout(main))
-- 字符串url编码
function urlEncode(s)
-- url编码
local s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return s:gsub(" ", "+"):gsub("%%2C",",")
end
-- 字符串序列化
function tableToStr1(tbl,sep,isEncode)
local str = ''
local sep= sep or '&'
local isEncode= isEncode==nil and false or true
for key, value in pairs(tbl) do
if isEncode then
str = str .. key .. '=' .. urlEncode(value) .. sep
else
str = str .. key .. '=' .. value .. sep
end
end
return string.sub(str, 1, -2)
end
-- 排序后的table序列化
function tableToStr(tbl)
local str = ''
for key, value in ipairs(tbl) do
str = str .. value[1] .. '=' .. value[2] .. '&'
end
return string.sub(str, 1, -2)
end
-- md5加密
function md5Encrypt(data,isHex)
local MessageDigest = luajava.bindClass "java.security.MessageDigest"
local String=luajava.bindClass "java.lang.String"
local md = MessageDigest.getInstance("MD5")
local bytes = md.digest(String(data).getBytes())
local isHex= (isHex==nil) and true or false
if isHex ==false then
return bytes
end
local result = ""
for i = 0, #bytes - 1 do
local temp = string.format("%02x", (bytes[i] & 0xff))
result = result .. temp
end
return result
end
-- 获得按键排序后的参数table
function getSortedParams(params)
local function compare(a, b)
return a[1] < b[1]
end
local sorted_params = {}
for key, value in pairs(params) do
table.insert(sorted_params, { key, value })
end
table.sort(sorted_params, compare)
return sorted_params
end
function getTransPdf(sourcePdf,from,to)
local Http=luajava.bindClass "com.androlua.Http"
local json=require "cjson"
local headers = {
-- 设置请求头信息
['authority'] = 'fanyi.youdao.com',
['accept'] = 'application/json, text/plain, */*',
['accept-language'] = 'zh-CN,zh;q=0.9',
['referer'] = 'https://fanyi.youdao.com/trans/',
['sec-ch-ua'] = '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
['sec-ch-ua-mobile'] = '?0',
['sec-ch-ua-platform'] = '"Windows"',
['usec-fetch-dest'] = 'empty',
['sec-fetch-mode'] = 'cors',
['sec-fetch-site'] = 'same-origin',
['user-agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
local params = {
-- 设置请求参数信息
['from'] = 'en',
['to'] = 'zh-CHS',
['field'] = '0',
['product'] = 'arxivweb',
['appVersion'] = '1.0.0',
['client'] = 'web',
['keyid'] = 'arxivweb',
['keyfrom'] = 'web',
['version'] = '1.0.0',
['arxivUrl'] = sourcePdf
}
local key="r*bWYmKw0Z@$1^fEk3xIxKqb!3HMTyI"
-- 获取按key排序后的table
local encodeParams=getSortedParams(params)
-- 拼接生成签名字符串
local signStr=tableToStr(encodeParams).."&key="..key
-- 生成签名
local sign=md5Encrypt(signStr)
-- 添加签名字符串到请求参数中
params["sign"]=sign
-- 发送请求
local url="https://fanyi.youdao.com/fanyilatexweb/arxiv/translate"
url=url.."?"..tableToStr1(params)
local httpTask=Http.get(url,nil,nil,headers,function() end)
local result=httpTask.get()
local code=result[0]
local content,cookie,header
if code==200 then
code,content,cookie,header=result[0],result[1],result[2],result[3]
content=json.decode(content)
end
return code,content,cookie,header
end
function 下载(url)
import "android.content.Intent"
import "android.net.Uri"
local viewIntent = Intent("android.intent.action.VIEW",Uri.parse(url))
activity.startActivity(viewIntent)
end
-- 设置翻译按钮点击事件
transBtn.onClick=function()
local sourceUrl=tostring(source.getText())
if sourceUrl==nil or sourceUrl=="" then
print("请输入翻译的pdf地址")
else
local code,content=getTransPdf(sourceUrl)
if code==200 then
result.setText("下载地址:"..content["data"]["targetPdfPath"])
else
print("网络错误")
end
end
end
-- 设置浏览器打开点击事件
web.onClick=function()
local url=tostring(result.getText())
下载(string.sub(url,string.find(url,":")+1))
end
源码及demo apk下载地址
- 源码: https://www.123pan.com/s/J5Nsjv-mXBN.html 提取码:gIY6
- apk: https://www.123pan.com/s/J5Nsjv-1XBN.html 提取码:t7NA
本文由【产品经理不是经理】gzh 同步发布,欢迎关注