foxlog
3/27/2018 - 3:20 PM

有道在线api查询

有道在线api查询 翻译 youdao translate clj clojure

package youdao_api_translate_future_core_async;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

// 有道官方给的代码复制的时候好像不全, 有几个地方编译不通过, 稍微改一下就OK.
class Demo {

    public static void main(String[] args) throws Exception {
        String appKey ="您的appKey";
        String query = "你好";
        String salt = String.valueOf(System.currentTimeMillis());
        String from = "zh-CHS";
        String to = "EN";
        String sign = md5(appKey + query + salt+ "您的密钥");
        Map params = new HashMap();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("sign", sign);
        params.put("salt", salt);
        params.put("appKey", appKey);
        System.out.println(requestForHttp("http://openapi.youdao.com/api", params));
    }

    public static String requestForHttp(String url,Map requestParams) throws Exception{
        String result = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        /**HttpPost*/
        HttpPost httpPost = new HttpPost(url);
        System.out.println(new JSONObject(requestParams).toString());
        List params = new ArrayList();
        Iterator it = requestParams.entrySet().iterator();
        while (it.hasNext()) {
            Entry en = (Entry) it.next();
            String key = String.valueOf(en.getKey());
            String value = String.valueOf(en.getValue());
            if (value != null) {
                params.add(new BasicNameValuePair(key, value));
            }
        }
        httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
        /**HttpResponse*/
        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
        try{
            HttpEntity httpEntity = httpResponse.getEntity();
            result = EntityUtils.toString(httpEntity, "utf-8");
            EntityUtils.consume(httpEntity);
        }finally{
            try{
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 生成32位MD5摘要
     * @param string
     * @return
     */
    public static String md5(String string) {
        if(string == null){
            return null;
        }
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F'};

        try{
            byte[] btInput = string.getBytes("utf-8");
            /** 获得MD5摘要算法的 MessageDigest 对象 */
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            /** 使用指定的字节更新摘要 */
            mdInst.update(btInput);
            /** 获得密文 */
            byte[] md = mdInst.digest();
            /** 把密文转换成十六进制的字符串形式 */
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        }catch(NoSuchAlgorithmException | UnsupportedEncodingException e){
            return null;
        }
    }

    /**
     * 根据api地址和参数生成请求URL
     * @param url
     * @param params
     * @return
     */
    public static String getUrlWithQueryString(String url, Map params) {
        if (params == null) {
            return url;
        }

        StringBuilder builder = new StringBuilder(url);
        if (url.contains("?")) {
            builder.append("&");
        } else {
            builder.append("?");
        }

        int i = 0;
        for (Object key : params.keySet()) {
            String value = String.valueOf(params.get(key));
            if (value == null) { // 过滤空的key
                continue;
            }

            if (i != 0) {
                builder.append('&');
            }

            builder.append(key);
            builder.append('=');
            builder.append(encode(value));

            i++;
        }

        return builder.toString();
    }
    /**
     * 进行URL编码
     * @param input
     * @return
     */
    public static String encode(String input) {
        if (input == null) {
            return "";
        }

        try {
            return URLEncoder.encode(input, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return input;
    }
}
(require '[clojure.core.async :as async :refer [<! >! <!! >!! timeout chan alt! alts! alt!! alts!! go]])

(defn quicktranslatewithasync []
  (let [c1 (chan)
        c2 (chan)
        c3 (chan)
        c4 (chan)
        c5 (chan)
        c6 (chan)
        c7 (chan)
        c8 (chan)
        c9 (chan)
        c10 (chan)
        c11 (chan)
        c12 (chan)
        c13 (chan)
        c14 (chan)
        ]

    (go (translate 0 5000)
        (>! c1 "finish c1"))
    (go (translate 5000 10000)
        (>! c2 "finish c2")
        )
    (go (translate 10000 15000)
        (>! c3 "finish c3"))
    (go (translate 15000 20000)
        (>! c4 "finish c4"))
    (go (translate 20000 25000)
        (>! c5 "finish c5"))
    (go (translate 25000 30000)
        (>! c6 "finish c6"))
    (go (translate 30000 35000)
        (>! c7 "finish c7"))
    (go (translate 35000 40000)
        (>! c8 "finish c8"))
    (go (translate 40000 45000)
        (>! c9 "finish c9"))
    (go (translate 45000 50000)
        (>! c10 "finish c10"))
    (go (translate 50000 55000)
        (>! c11 "finish c11"))
    (go (translate 55000 60000)
        (>! c12 "finish c12"))
    (go (translate 60000 65000)
        (>! c13 "finish c13"))
    (go (translate 65000 70000)
        (>! c14 "finish c14"))



    (println (<!! c1))
    (println (<!! c2))
    (println (<!! c3))
    (println (<!! c4))
    (println (<!! c5))
    (println (<!! c6))
    (println (<!! c7))
    (println (<!! c8))
    (println (<!! c9))
    (println (<!! c10))
    (println (<!! c11))
    (println (<!! c12))
    (println (<!! c13))
    (println (<!! c14))

    )


  )
;; 直接用future也够用了
(defn quicktranslatewithfuture []
  (let [f1 (future (translate 0 5000))
        f2 (future (translate 5000 10000))
        f3 (future (translate 10000 15000))
        f4 (future (translate 15000 20000))
        f5 (future (translate 20000 25000))
        f6 (future (translate 25000 30000))
        f7 (future (translate 30000 35000))
        f8 (future (translate 35000 40000))
        f9 (future (translate 40000 45000))
        f10 (future (translate 45000 50000))
        f11 (future (translate 50000 55000))
        f12 (future (translate 55000 60000))
        f13 (future (translate 60000 65000))
        f14 (future (translate 65000 70000))
        ]

    (println @f1)
    (println @f2)
    (println @f3)
    (println @f4)
    (println @f5)
    (println @f6)
    (println @f7)
    (println @f8)
    (println @f9)
    (println @f10)
    (println @f11)
    (println @f12)
    (println @f13)
    (println @f14)
    )

  )

(ns alexcoding.sandbox.englishtranslate
  (:require [clojure.java.jdbc :as jdbc])
  (:import (alexcoding.util Translate)))

(require '[clojure.core.async :as async :refer [<! >! <!! >!! timeout chan alt! alts! alt!! alts!! go]])



(def db-spec {:classname "org.sqlite.JDBC",
              :subprotocol "sqlite",
              :subname "/Users/alex/Dropbox/Sqlite/AlexApps.sqlite"})


(defn updatewithchinese [chinese words]
  (jdbc/execute! db-spec ["update words SET chinese = ? WHERE words = ? " chinese words]))

(defn querywords [from to]
  (let [res (jdbc/query db-spec
                        ["select * from words where chinese is null and ordernum > ? and ordernum <= ? LIMIT 10000" from to])]
    (doall res))
  #_(jdbc/query db-spec
              ["select * from words where chinese is null and ordernum > ? and ordernum <= ? LIMIT 10000" from to])
  )



(defn translate [from to]

  (let [results (querywords from to)]
    ;; 之前是for语句有问题, 因为for是lazy, 导致没有执行直接跳过了
     (doseq [result results]
       (updatewithchinese (Translate/translate (:words result)) (:words result))
       )
    )
  )