切换风格

默认Lavender Sky Flowers Wizard Snow Beige California City Dragon Black London Sunset glow Pink Cloud

31

主题

239

积分

0

精华

用户组 

易积分
815
热心
0
好评
1
谷歌翻译API破解 Java源代码[复制链接]
发表于 2022-10-1 21:02:55 | 显示全部楼层 |阅读模式
  1. package com.coding.easier.util;

  2. import kong.unirest.Header;
  3. import kong.unirest.HttpResponse;
  4. import kong.unirest.Unirest;

  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLEncoder;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Random;

  12. /**
  13. * @author :zhuYi
  14. * @date :Created in 2022/10/1 3:04
  15. */

  16. public class YoudaoFanyiUtil {
  17.     private static String COOKIE;
  18.     private static long COOKIE_VALIDITY_TIME;

  19.     private static String getCookie() {
  20.         if (COOKIE == null) {
  21.             COOKIE = createCookie();
  22.             if (COOKIE != null) {
  23.                 //3小时更新一次
  24.                 COOKIE_VALIDITY_TIME = System.currentTimeMillis() + (3 * 60 * 60 * 1000);
  25.                 return COOKIE;
  26.             }
  27.         } else if (System.currentTimeMillis() > COOKIE_VALIDITY_TIME) {
  28.             //过期了刷新COOKIE
  29.             COOKIE = null;
  30.             return getCookie();
  31.         } else {
  32.             return COOKIE;
  33.         }
  34.         return null;
  35.     }

  36.     private static String createCookie() {
  37.         HttpResponse<String> response = Unirest.get("https://fanyi.youdao.com/")
  38.                 .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/xxx.x.xxxx.xx")
  39.                 .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
  40.                 .asString();
  41.         for (Header header : response.getHeaders().all()) {
  42.             if ("Set-Cookie".equals(header.getName())) {
  43.                 String cookie = header.getValue();
  44.                 String txt = cookie.substring(cookie.indexOf("OUTFOX_SEARCH_USER_ID="));
  45.                 txt = txt.substring(0, txt.indexOf(";"));
  46.                 return String.format("%s; ___rl__test__cookies=", txt);
  47.             }
  48.         }
  49.         return null;
  50.     }

  51.     /**
  52.      * 欲翻译的内容
  53.      */
  54.     public static void translation(final String txt, Callback callback) {
  55.         new Thread(() -> {
  56.             try {
  57.                 //Cookie检查
  58.                 if (getCookie() == null) {
  59.                     return;
  60.                 }
  61.                 //统一编码,否则插件中无法使用
  62.                 String coding = getEncoding(txt);
  63.                 String str = coding.equals("UTF-8") ? txt : new String(txt.getBytes(), "GBK");
  64.                 String url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule";
  65.                 String u = "fanyideskweb";
  66.                 String ctime = System.currentTimeMillis() + "";
  67.                 //int random = (int) (Math.random() * 10 + 1); idea内置API有问题
  68.                 int random = new Random().nextInt(9);
  69.                 String f = ctime + "" + random;
  70.                 String c = "Ygy_4c=r#e#4EX^NUGUc5";
  71.                 //浏览器版本信息
  72.                 String bv = md5("5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/xxx.x.xxxx.xx");
  73.                 //插件内置编码不兼容 ,故改用byte进行md5处理
  74.                 byte[] strBytes = str.getBytes("UTF8");
  75.                 byte[] uBytes = u.getBytes();
  76.                 byte[] fBytes = f.getBytes();
  77.                 byte[] cBytes = c.getBytes();
  78.                 byte[] newMd5Bytes = new byte[strBytes.length + uBytes.length + fBytes.length + cBytes.length];
  79.                 System.arraycopy(uBytes, 0, newMd5Bytes, 0, uBytes.length);
  80.                 System.arraycopy(strBytes, 0, newMd5Bytes, uBytes.length, strBytes.length);
  81.                 System.arraycopy(fBytes, 0, newMd5Bytes, strBytes.length + uBytes.length, fBytes.length);
  82.                 System.arraycopy(cBytes, 0, newMd5Bytes, fBytes.length + uBytes.length + strBytes.length, cBytes.length);
  83.                 String sign = md5(newMd5Bytes);
  84.                 Map<String, String> params = new HashMap<>();
  85.                 params.put("i", URLEncoder.encode(str, "UTF-8"));
  86.                 params.put("from", "AUTO");
  87.                 params.put("to", "AUTO");
  88.                 params.put("smartresult", "dict");
  89.                 params.put("client", u);
  90.                 params.put("salt", f);
  91.                 params.put("lts", ctime);
  92.                 params.put("bv", bv);
  93.                 params.put("sign", sign);
  94.                 params.put("doctype", "json");
  95.                 params.put("version", "2.1");
  96.                 params.put("keyfrom", "fanyi.web");
  97.                 params.put("action", "FY_BY_REALTlME");
  98.                 StringBuilder stringBuilder = new StringBuilder();
  99.                 for (String key : params.keySet()) {
  100.                     stringBuilder.append("&" + key + "=" + params.get(key));
  101.                 }
  102.                 HttpResponse<String> response = Unirest.post(url)
  103.                         .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  104.                         .header("Cookie", getCookie() + ctime)
  105.                         .header("Referer", "http://fanyi.youdao.com/")
  106.                         .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/xxx.x.xxxx.xx")
  107.                         .body(stringBuilder.toString())
  108.                         .asString();
  109.                 int status = response.getStatus();
  110.                 if (status >= 200 && status < 300) {
  111.                     callback.OnResponse(response.getBody());
  112.                 } else if (status == 429) {
  113.                     System.err.println("请求次数过多,可能触发了限流,请稍候再试......");
  114.                 } else {
  115.                     System.err.println("Unexpected response status: " + status);
  116.                 }
  117.             } catch (Throwable e) {
  118.                 e.printStackTrace();
  119.             }

  120.         }).start();
  121.     }


  122.     /**
  123.      * 生成32位MD5摘要
  124.      *
  125.      * @param string
  126.      * @return
  127.      */
  128.     private static String md5(String string) {
  129.         if (string == null) {
  130.             return null;
  131.         }
  132.         char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  133.                 'A', 'B', 'C', 'D', 'E', 'F'};
  134.         byte[] btInput = string.getBytes();
  135.         try {
  136.             /** 获得MD5摘要算法的 MessageDigest 对象 */
  137.             MessageDigest mdInst = MessageDigest.getInstance("MD5");
  138.             /** 使用指定的字节更新摘要 */
  139.             mdInst.update(btInput);
  140.             /** 获得密文 */
  141.             byte[] md = mdInst.digest();
  142.             /** 把密文转换成十六进制的字符串形式 */
  143.             int j = md.length;
  144.             char str[] = new char[j * 2];
  145.             int k = 0;
  146.             for (byte byte0 : md) {
  147.                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
  148.                 str[k++] = hexDigits[byte0 & 0xf];
  149.             }
  150.             return new String(str).toLowerCase();
  151.         } catch (NoSuchAlgorithmException e) {
  152.             return null;
  153.         }
  154.     }

  155.     private static String md5(byte[] btInput) {
  156.         char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  157.                 'A', 'B', 'C', 'D', 'E', 'F'};
  158.         try {
  159.             /** 获得MD5摘要算法的 MessageDigest 对象 */
  160.             MessageDigest mdInst = MessageDigest.getInstance("MD5");
  161.             /** 使用指定的字节更新摘要 */
  162.             mdInst.update(btInput);
  163.             /** 获得密文 */
  164.             byte[] md = mdInst.digest();
  165.             /** 把密文转换成十六进制的字符串形式 */
  166.             int j = md.length;
  167.             char str[] = new char[j * 2];
  168.             int k = 0;
  169.             for (byte byte0 : md) {
  170.                 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
  171.                 str[k++] = hexDigits[byte0 & 0xf];
  172.             }
  173.             return new String(str).toLowerCase();
  174.         } catch (NoSuchAlgorithmException e) {
  175.             return null;
  176.         }
  177.     }


  178.     public static String getEncoding(String str) {
  179.         String encode = "GB2312";
  180.         try {
  181.             if (isEncoding(str, encode)) { // 判断是不是GB2312
  182.                 return encode;
  183.             }
  184.         } catch (Exception exception) {
  185.         }
  186.         encode = "ISO-8859-1";
  187.         try {
  188.             if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
  189.                 return encode;
  190.             }
  191.         } catch (Exception exception1) {
  192.         }
  193.         encode = "UTF-8";
  194.         try {
  195.             if (isEncoding(str, encode)) { // 判断是不是UTF-8
  196.                 return encode;
  197.             }
  198.         } catch (Exception exception2) {
  199.         }
  200.         encode = "GBK";
  201.         try {
  202.             if (isEncoding(str, encode)) { // 判断是不是GBK
  203.                 return encode;
  204.             }
  205.         } catch (Exception exception3) {
  206.         }
  207.         return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。
  208.     }

  209.     public static boolean isEncoding(String str, String encode) {
  210.         try {
  211.             if (str.equals(new String(str.getBytes(), encode))) {
  212.                 return true;
  213.             }
  214.         } catch (UnsupportedEncodingException e) {
  215.             e.printStackTrace();
  216.         }
  217.         return false;
  218.     }

  219.     public interface Callback {
  220.         void OnResponse(String str);

  221.     }

  222. }
复制代码


易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

31

主题

239

积分

0

精华

用户组 

易积分
815
热心
0
好评
1
发表于 2022-10-1 21:04:02 | 显示全部楼层
应该叫有道,标题整错了,随意吧,送给有缘人
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

3

主题

35

积分

0

精华

用户组 

易积分
16
热心
0
好评
0
发表于 2022-10-4 22:06:22 来自手机 | 显示全部楼层
学习一下,谢谢楼主分享
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

1

主题

21

积分

0

精华

用户组 

易积分
4
热心
0
好评
0
发表于 2022-11-1 11:17:09 | 显示全部楼层
学习了
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

0

主题

182

积分

0

精华
易积分
5
热心
0
好评
0
发表于 2023-8-21 18:46:15 | 显示全部楼层
看一下。,
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

QQ|sitemap|免责声明|RGB颜色对照表|手机版|小黑屋| 易如意 - E4A中文编程学习交流论坛

GMT+8, 2024-5-20 12:35 , Processed in 0.174162 second(s), 32 queries .

Powered by Discuz! X3.4

© 2001-2018 eruyi.cn

返回顶部