Exemplo n.º 1
0
        public static string CreateURL(this Papago papago)
        {
            StringBuilder builder = new StringBuilder(255);

            builder.Append($"https://{Papago.RootURL}/");
            builder.Append($"?sk={papago.RequestLanguage.GetStringValue()}");
            builder.Append($"&tk={papago.ResponseLanguage.GetStringValue()}");
            builder.Append($"&st={papago.text.Trim()}");
            return(builder.ToString());
        }
Exemplo n.º 2
0
        public ReturnValues <Papago> Translation(Papago data, IWebClient wc)
        {
            var result = new ReturnValues <Papago>();

            if (string.IsNullOrWhiteSpace(data.text))
            {
                result.Error("번역 대상이 없습니다.");
            }
            else if (data.text.Trim().Length >= 5000)
            {
                result.Error("1회 번역 최대 문자열은 5000 글자 입니다.");
            }
            else
            {
                try
                {
                    string tmp = wc.DownloadString(data.CreateURL());
                    if (!string.IsNullOrWhiteSpace(tmp))
                    {
                        HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
                        document.LoadHtml(tmp);
                        HtmlAgilityPack.HtmlNode targetObject = document.GetElementbyId("txtTarget");
                        if (targetObject != null)
                        {
                            data.Result = targetObject.InnerText.Trim();
                            result.Success(1, data);
                        }
                        else
                        {
                            var nodes = document.DocumentNode.SelectSingleNode("//div[@id='txtTarget']");
                            if (nodes != null)
                            {
                                data.Result = nodes.InnerText.Trim();
                                result.Success(1, data);
                            }
                            else
                            {
                                result.Error("Not Found Target!!!!");
                                result.Value = tmp;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    result.Error(ex);
                    if (this.Logger != null)
                    {
                        this.Logger.Error(ex);
                    }
                }
            }

            return(result);
        }