-
Notifications
You must be signed in to change notification settings - Fork 1
/
translate.bsh
61 lines (39 loc) · 1.33 KB
/
translate.bsh
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
// translate.bsh
String textToTranslate = "fish";
String fromLang = "en";
String toLang = "ru";
String hostURL = "http://translate.google.com";
String start = "<div id=result_box dir=ltr>";
String end = "</div>" ;
String url = hostURL + "/translate_t" +
"?" + "langpair=" + fromLang + "|" + toLang +
"&text=" + textToTranslate;
URLConnection urlConnection = new URL(url).openConnection();
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon; .NET CLR 1.1.4322)");
urlConnection.setRequestProperty("Pragma", "no-cache");
String result = "";
InputStream is = urlConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(true) {
int ch = is.read();
if(ch == -1) {
break;
}
baos.write(ch);
}
is.close();
baos.close();
String s = new String(baos.toByteArray(), "UTF-8");
int index1 = s.indexOf(start);
if(index1 != -1) {
String s2 = s.substring(index1 + start.length());
int index2 = s2.indexOf(end);
if(index2 != -1) {
result = s2.substring(0, index2);
}
}
FileOutputStream out = new FileOutputStream("out.txt");
System.out.println("result: " + result);
out.write(result.getBytes());
out.close();