var md5 = require(‘md5‘);
const axios = require(‘axios‘);
function api(query, source_langauge, target_language) {
var url = "http://fanyi.sogou.com:80/reventondc/api/sogouTranslate"
let pid = ‘自己填‘
let key = ‘自己填‘
var salt = Math.floor(Date.now() / 1000);
var q = query
var sign = md5(pid + q + salt + key)
var from = source_langauge
var to = target_language
var payload = "from=" + from + "&to=" + to + "&pid=" + pid + "&q=" + encodeURIComponent(q) + "&sign=" + sign + "&salt=" + salt
return axios({
method: ‘post‘,
url,
timeout: 5000,
headers: {
‘Accept‘: ‘application/json‘,
‘Content-Type‘: ‘application/x-www-form-urlencoded;charset=UTF-8‘,
"Cache-Control": "no-cache", // 避免 Chrome 把请求 Stalled 20 秒
},
data: payload
}).then(response => {
return response.data
})
}
exports.api = api
搜狗翻译 API nodejs