Flask接口签名sign原理与实例代码浅析
608
2022-06-07
由於Google translate API要收錢 ,因此想了一個偷機的方法
1. 用HttpClient發送一個request給http://translate.google.com
2. 再用Jsoup來parse html, 並取出翻譯後的文字
复制代码 代码如下:
/** * Copyright (c) blackbear, Inc All Rights Reserved. */package org.bb.util.i18n; import java.io.InputStream;import java.net.URLEncoder;import java.text.MessageFormat; import org.apache.commons.io.IOUtils; import org.bb.util.net.http.HttpClientUtil;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element; /** * TranslateUtil * *
翻譯工具 * PS: 透過google translate ** * @author catty * @version 1.0, Created on 2011/9/2 */public class TranslateUtil { protected static final String URL_TEMPLATE = "http://translate.google.com/?langpair={0}&text={1}"; protected static final String ID_RESULTBOX = "result_box"; protected static final String ENCODING = "UTF-8"; protected static final String AUTO = "auto"; // google自動判斷來源語系 protected static final String TAIWAN = "zh-TW"; // 繁中 protected static final String CHINA = "zh-CN"; // 簡中 protected static final String ENGLISH = "en"; // 英 protected static final String JAPAN = "ja"; // 日 /** *
Google翻譯 * PS: 交由google自動判斷來源語系 ** * @param text * @param target_lang 目標語系 * @return * @throws Exception */ public static String translate(final String text, final String target_lang) throws Exception { return translate(text, AUTO, target_lang); } /** *
Google翻譯* * @param text * @param src_lang 來源語系 * @param target_lang 目標語系 * @return * @throws Exception */ public static String translate(final String text, final String src_lang, final String target_lang) throws Exception { InputStream is = null; Document doc = null; Element ele = null; try { // create URL string String url = MessageFormat.format(URL_TEMPLATE, URLEncoder.encode(src_lang + "|" + target_lang, ENCODING), URLEncoder.encode(text, ENCODING)); // connect & download html is = HttpClientUtil.downloadAsStream(url); // parse html by Jsoup doc = Jsoup.parse(is, ENCODING, ""); ele = doc.getElementById(ID_RESULTBOX); String result = ele.text(); return result; } finally { IOUtils.closeQuietly(is); is = null; doc = null; ele = null; } } /** *
Google翻譯: 簡中-->繁中* * @param text * @return * @throws Exception */ public static String cn2tw(final String text) throws Exception { return translate(text, CHINA, TAIWAN); } /** *
Google翻譯: 繁中-->簡中* * @param text * @return * @throws Exception */ public static String tw2cn(final String text) throws Exception { return translate(text, TAIWAN, CHINA); } /** *
Google翻譯: 英文-->繁中* * @param text * @return * @throws Exception */ public static String en2tw(final String text) throws Exception { return translate(text, ENGLISH, TAIWAN); } /** *
Google翻譯: 繁中-->英文* * @param text * @return * @throws Exception */ public static String tw2en(final String text) throws Exception { return translate(text, TAIWAN, ENGLISH); } /** *
Google翻譯: 日文-->繁中* * @param text * @return * @throws Exception */ public static String jp2tw(final String text) throws Exception { return translate(text, JAPAN, TAIWAN); } /** *
Google翻譯: 繁中-->日* * @param text * @return * @throws Exception */ public static String tw2jp(final String text) throws Exception { return translate(text, TAIWAN, JAPAN); }}
HttpClientUtil.java
复制代码 代码如下:
/** * Copyright (c) Blackbear, Inc All Rights Reserved. */package org.bb.util.net.http; import java.io.BufferedInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Map; import org.apache.commons.io.IOUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpRequestBase;import org.apache.http.conn.scheme.PlainSocketFactory;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.entity.BufferedHttpEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;import org.apache.http.params.BasicHttpParams; /** * PostUtil.java * * @author catty * @version 1.0, Created on 2008/2/20 */public class HttpClientUtil { protected static Log log = LogFactory.getLog(HttpClientUtil.class); protected static HttpClient httpclient = null; protected static int maxTotal = 200; protected static int maxPerRoute = 20; protected static String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7"; static { if (httpclient == null) { // ~~~~~~~~~~~~~~~~~~~~ // create httpclient // ~~~~~~~~~~~~~~~~~~~~ SchemeRegistry reg = new SchemeRegistry(); reg.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); reg.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(reg); cm.setMaxTotal(maxTotal); cm.setDefaultMaxPerRoute(maxPerRoute); httpclient = new DefaultHttpClient(cm); } } /** *
下載後回傳Inputstream* * @param url * @return * @throws Exception */ public static InputStream downloadAsStream(String url) throws Exception { InputStream is = (InputStream) download(url, null, null, false); return is; } /** *
下載後儲存到File* * @param url * @param saveFile * @throws Exception */ public static void download(String url, File saveFile) throws Exception { download(url, saveFile, null, false); } /** *
下載* * @param url * @param saveFile * @param params * @param isPost * @return 如果saveFile==null則回傳inputstream, 否則回傳saveFile * @throws Exception */ public static Object download(final String url, final File saveFile, final Map
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~