-
Notifications
You must be signed in to change notification settings - Fork 210
/
YoudaoTranslator.java
73 lines (61 loc) · 2.66 KB
/
YoudaoTranslator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.swjtu.trans.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.swjtu.lang.LANG;
import com.swjtu.trans.AbstractTranslator;
import com.swjtu.util.Util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public final class YoudaoTranslator extends AbstractTranslator {
private static final String url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule";
public YoudaoTranslator(){
super(url);
}
@Override
public void setLangSupport() {
langMap.put(LANG.ZH, "zh-CHS");
langMap.put(LANG.EN, "en");
langMap.put(LANG.JP, "ja");
langMap.put(LANG.KOR, "ko");
langMap.put(LANG.FRA, "fr");
langMap.put(LANG.RU, "ru");
}
@Override
public void setFormData(LANG from, LANG to, String text) {
String slat = String.valueOf(System.currentTimeMillis() + (long)(Math.random() * 10 + 1));
String sign = Util.md5("fanyideskweb" + text + slat + "ebSeFb%=XZ%T[KZ)c(sy!");
formData.put("i", text);
formData.put("from", langMap.get(from));
formData.put("to", langMap.get(to));
formData.put("smartresult", "dict");
formData.put("client", "fanyideskweb");
formData.put("salt", slat);
formData.put("sign", sign);
formData.put("doctype", "json");
formData.put("version", "2.1");
formData.put("keyfrom", "fanyi.web");
formData.put("action", "FY_BY_CLICKBUTTION");
formData.put("typoResult", "false");
}
@Override
public String query() throws Exception {
HttpPost request = new HttpPost(Util.getUrlWithQueryString(url, formData));
request.setHeader("Cookie","[email protected];");
request.setHeader("Referer","http://fanyi.youdao.com/");
request.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
CloseableHttpResponse httpResponse = httpClient.execute(request);
HttpEntity httpEntity = httpResponse.getEntity();
String result = EntityUtils.toString(httpEntity, "UTF-8");
EntityUtils.consume(httpEntity);
httpResponse.close();
return result;
}
@Override
public String parses(String text) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(text).path("translateResult").findPath("tgt").toString();
}
}