Esempio n. 1
0
        protected virtual async Task <IAPIResponse> Execute(IApiRequest request)
        {
            string result    = string.Empty;
            var    apiResult = new ApiResponse();

            apiResult.Code    = -1;
            apiResult.Message = "";
            apiResult.Data    = "";

            string txt = Encoding.UTF8.GetString(request.Body);

            if (request.Url == "google")
            {
                try
                {
                    string mess      = txt;
                    string tkk       = request.TKK;
                    var    tk        = GetTKHelper.GetTK(txt, tkk);
                    string paramData = "client=webapp&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=3&tsel=3&kc=1&tk=" + tk + "&q=" + mess;

                    HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create("https://translate.google.cn/translate_a/single");
                    wbRequest.Method      = "POST";
                    wbRequest.ContentType = "application/x-www-form-urlencoded";
                    wbRequest.Accept      = "*/*";
                    wbRequest.Referer     = "https://translate.google.cn/";
                    wbRequest.Host        = "translate.google.cn";
                    wbRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br");
                    wbRequest.Headers.Add("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.5");
                    wbRequest.UserAgent     = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36";
                    wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
                    using (Stream requestStream = wbRequest.GetRequestStream())
                    {
                        using (StreamWriter swrite = new StreamWriter(requestStream))
                        {
                            swrite.Write(paramData);
                        }
                    }

                    using (HttpWebResponse response = (HttpWebResponse)wbRequest.GetResponse())
                    {
                        string ContentEncoding = response.ContentEncoding.ToLower();
                        Stream stream          = null;
                        if (ContentEncoding.Contains("gzip"))
                        {
                            stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                        }
                        else if (ContentEncoding.Contains("deflate"))
                        {
                            stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress);
                        }
                        else
                        {
                            stream = response.GetResponseStream();
                        }
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            result = reader.ReadToEnd();
                        }
                        dynamic tempResult = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
                        //解决“企业\r\n学习”多行文本没有显示全部翻译
                        var           resarry = Newtonsoft.Json.JsonConvert.DeserializeObject(tempResult[0].ToString());
                        var           length  = (resarry.Count) - 1;
                        StringBuilder str     = new StringBuilder();
                        for (int i = 0; i < length; i++)
                        {
                            var res = Newtonsoft.Json.JsonConvert.DeserializeObject(resarry[i].ToString());
                            str.Append(res[0].ToString());
                        }

                        apiResult.Code    = (int)response.StatusCode;
                        apiResult.Message = response.StatusCode.ToString();
                        apiResult.Data    = str.ToString();
                        apiResult.Tags.Add("from-language", "en");
                        apiResult.Tags.Add("to-language", "zh-CN");
                        apiResult.Tags.Add("translate-success", "true");
                    }
                }
                catch (Exception ex)
                {
                    apiResult.Code    = -1;
                    apiResult.Message = ex.Message;
                    apiResult.Data    = "";
                }
            }
            else if (request.Url == "baidu")
            {
                try
                {
                    Request r = new Request();
                    r.from  = "en";
                    r.to    = "zh";
                    r.q     = txt;
                    r.appid = request.APPID;
                    r.key   = request.KEY;
                    Result res    = r.Post <Result>();
                    int    length = res.trans_result.Count;

                    StringBuilder str = new StringBuilder();
                    for (int i = 0; i < length; i++)
                    {
                        str.Append(res.trans_result[i].dst);
                        if (i < length - 1)
                        {
                            str.Append("\r\n");
                        }
                    }

                    apiResult.Code    = (int)0;
                    apiResult.Message = "";
                    apiResult.Data    = str.ToString();
                    apiResult.Tags.Add("from-language", "en");
                    apiResult.Tags.Add("to-language", "zh-CN");
                    apiResult.Tags.Add("translate-success", "true");
                }
                catch (Exception ex)
                {
                    apiResult.Code    = -1;
                    apiResult.Message = ex.Message;
                    apiResult.Data    = "";
                }
            }
            return(apiResult);
        }
        protected virtual async Task <IAPIResponse> Execute(IApiRequest request)
        {
            CookieContainer cookie       = new CookieContainer();
            string          text         = Encoding.UTF8.GetString(request.Body);
            string          tkk          = request.TKK;
            string          fromLanguage = request.Headers["from-language"].ToString();
            string          toLanguage   = request.Headers["to-language"].ToString();
            string          referer      = "https://translate.google.cn/";
            var             html         = "";
            var             tk           = GetTKHelper.GetTK(text, tkk);
            StringBuilder   url          = new StringBuilder();

            url.Append("https://translate.google.cn/translate_a/single?client=webapp");
            url.AppendFormat("&sl={0}", fromLanguage);
            url.AppendFormat("&tl={0}", toLanguage);
            url.Append("&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&otf=1&ssel=0&tsel=0&kc=1");
            url.AppendFormat("&tk={0}", tk);
            url.AppendFormat("&q={0}", HttpUtility.UrlEncode(text));

            //Create empty result
            var apiResult = new ApiResponse();

            try
            {
                var webRequest = WebRequest.Create(url.ToString()) as HttpWebRequest;
                webRequest.Method          = "GET";
                webRequest.CookieContainer = cookie;
                webRequest.Referer         = referer;
                webRequest.Timeout         = 20000;
                webRequest.Headers.Add("X-Requested-With:XMLHttpRequest");
                webRequest.Accept    = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

                using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        html = reader.ReadToEnd();

                        dynamic tempResult = Newtonsoft.Json.JsonConvert.DeserializeObject(html);
                        //解决“企业\r\n学习”多行文本没有显示全部翻译
                        var           resarry = Newtonsoft.Json.JsonConvert.DeserializeObject(tempResult[0].ToString());
                        var           length  = (resarry.Count) - 1;
                        StringBuilder str     = new StringBuilder();
                        for (int i = 0; i < length; i++)
                        {
                            var res = Newtonsoft.Json.JsonConvert.DeserializeObject(resarry[i].ToString());
                            str.Append(res[0].ToString());
                        }

                        apiResult.Code    = (int)webResponse.StatusCode;
                        apiResult.Message = webResponse.StatusCode.ToString();
                        apiResult.Data    = str.ToString();
                        apiResult.Tags.Add("from-language", fromLanguage);
                        apiResult.Tags.Add("to-language", toLanguage);
                        apiResult.Tags.Add("translate-success", "true");

                        reader.Close();
                        webResponse.Close();
                    }
                }
            }
            catch (Exception e)
            {
                apiResult.Code    = -1;
                apiResult.Message = e.Message;
                apiResult.Data    = "";
            }
            return(apiResult);
        }