-
Notifications
You must be signed in to change notification settings - Fork 210
/
TencentTranslator.java
67 lines (54 loc) · 2.36 KB
/
TencentTranslator.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
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.entity.UrlEncodedFormEntity;
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 TencentTranslator extends AbstractTranslator {
private static final String url = "https://fanyi.qq.com/api/translate";
public TencentTranslator(){
super(url);
}
@Override
public void setLangSupport() {
langMap.put(LANG.ZH, "zh");
langMap.put(LANG.EN, "en");
langMap.put(LANG.JP, "jp");
langMap.put(LANG.KOR, "kr");
langMap.put(LANG.FRA, "fr");
langMap.put(LANG.RU, "ru");
langMap.put(LANG.DE, "de");
}
@Override
public void setFormData(LANG from, LANG to, String text) {
formData.put("source", langMap.get(from));
formData.put("target", langMap.get(to));
formData.put("sourceText", text);
formData.put("sessionUuid", "translate_uuid" + String.valueOf(System.currentTimeMillis()));
}
@Override
public String query() throws Exception {
HttpPost request = new HttpPost(url);
request.setEntity(new UrlEncodedFormEntity(Util.map2list(formData), "UTF-8"));
request.setHeader("Cookie", "fy_guid=d4480e20-1644-4a47-a98d-787cfa244fd2; qtv=bbbc7118b32d7a9a; qtk=DTmfpOAn6b6HWTGtjW7w5a/FOommFjJPAre3GpaRUzPCQSaqY3gOSzKYEFyRYwKnjUN3M9D0V59LVNGDKchtj+RBld2oqSAVvEaAQVVLApTHDB52kdQYQYKAsa2NLnl4lIUbr6pYKN5469mS5hjcmQ==;");
request.setHeader("Origin", "http://fanyi.qq.com");
CloseableHttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
EntityUtils.consume(entity);
response.getEntity().getContent().close();
response.close();
return result;
}
@Override
public String parses(String text) throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(text).path("translate").findPath("targetText").toString();
}
}