string DoInternalTranslate(string query, Result result, NetworkSetting networkSetting)
        {
            WebRequestHelper helper =
                new WebRequestHelper(result, new Uri(query),
                                     networkSetting,
                                     WebRequestContentType.UrlEncodedGet,
                                     true);

            helper.Referer = "http://translate-net.appspot.com/";

            string responseFromServer = helper.GetResponse();

            if (responseFromServer.Contains(", \"responseStatus\": 200}"))
            {
                string translation = StringParser.Parse("\"translatedText\":\"", "\"", responseFromServer);
                translation = HttpUtilityEx.HtmlDecode(translation);
                return(translation);
            }
            else
            {
                string error = StringParser.Parse("\"responseDetails\": \"", "\"", responseFromServer);
                string code  = StringParser.Parse("\"responseStatus\":", "}", responseFromServer);
                throw new TranslationException(error + ", error code : " + code);
            }
        }
Example #2
0
        public static string ExtractRight(string startTag, string data)
        {
            if (startTag == null)
            {
                throw new ArgumentNullException("startTag");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int startTagLength = startTag.Length;
            int resultIdxStart = data.IndexOf(startTag);

            if (resultIdxStart < 0)
            {
                throw new TranslationException("Can't found start tag :" + startTag + " in string : " + data);
            }
            resultIdxStart += startTagLength;

            String result_string = HttpUtilityEx.HtmlDecode(data.Substring(resultIdxStart));

            return(result_string.Trim());
        }
Example #3
0
 static string FixString(string data)
 {
     data = data.Replace("<quote_str>", "\"");
     data = data.Replace("\\\\", "\\");
     data = data.Replace("\\n", "\n");
     data = data.Replace("\\r", "\r");
     data = data.Replace("\\t", "\t");
     data = HttpUtilityEx.HtmlDecode(data);
     return(data);
 }
Example #4
0
        public string[] ReadItemsList(string startTag, string endTag, string stopTag)
        {
            List <string> res = new List <string>();
            string        item;

            while (internalData.IndexOf(startTag, pos) >= 0)
            {
                item = ReadItem(startTag, endTag, stopTag);
                if (string.IsNullOrEmpty(item))
                {
                    break;
                }
                res.Add(HttpUtilityEx.HtmlDecode(item));
            }
            return(res.ToArray());
        }
Example #5
0
        public static string ExtractLeft(string endTag, string data)
        {
            if (endTag == null)
            {
                throw new ArgumentNullException("endTag");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            int resultIdxEnd = data.IndexOf(endTag);

            if (resultIdxEnd < 0)
            {
                throw new TranslationException("Can't found end tag :" + endTag + " in string : " + data);
            }

            String result_string = HttpUtilityEx.HtmlDecode(data.Substring(0, resultIdxEnd));

            return(result_string.Trim());
        }
        protected override void DoTranslate(string phrase, LanguagePair languagesPair, string subject, Result result, NetworkSetting networkSetting)
        {
            lock (cookieContainer)
            {
                if (coockieTime < DateTime.Now.AddHours(-1))
                {                  //emulate first access to site
                    WebRequestHelper helpertop =
                        new WebRequestHelper(result, new Uri("http://translate.meta.ua/"),
                                             networkSetting,
                                             WebRequestContentType.UrlEncodedGet);
                    helpertop.CookieContainer = cookieContainer;
                    string responseFromServertop = helpertop.GetResponse();
                    coockieTime = DateTime.Now;
                }
            }

            string lang_from = ConvertLanguage(languagesPair.From);
            string lang_to   = ConvertLanguage(languagesPair.To);

            string responseFromServer = null;

            lock (cookieContainer)
            {
                WebRequestHelper helper =
                    new WebRequestHelper(result, new Uri("http://translate.meta.ua/ajax/?sn=save_source"),
                                         networkSetting,
                                         WebRequestContentType.UrlEncoded);
                helper.CookieContainer = cookieContainer;

                //query
                //text_source=проверка&lang_to=ua&lang_from=ru&dict=**
                StringBuilder queryBuilder = new StringBuilder();
                queryBuilder.AppendFormat("text_source={0}&", phrase);
                queryBuilder.AppendFormat("lang_to={0}&lang_from={1}&", lang_to, lang_from);
                queryBuilder.AppendFormat("dict=", GetSubject(subject));
                string query = queryBuilder.ToString();
                helper.AddPostData(query);
                responseFromServer = helper.GetResponse();
                coockieTime        = DateTime.Now;
            }

            if (!String.IsNullOrEmpty(responseFromServer))
            {
                //{"r":true,"pc":1,"ui":"4c1ea0e46198f"}
                string code = StringParser.Parse("ui\":\"", "\"}", responseFromServer);
                //http://translate.meta.ua/ajax/?sn=get_translate&translate_uniqid=4c1ea0e46198f&lang_to=ua&lang_from=ru&translate_part=0
                string query = "http://translate.meta.ua/ajax/?sn=get_translate&translate_uniqid={0}&lang_to={1}&lang_from={2}&translate_part=0";
                string url   = String.Format(query, code, lang_to, lang_from);
                lock (cookieContainer)
                {
                    WebRequestHelper helper =
                        new WebRequestHelper(result, new Uri(url),
                                             networkSetting,
                                             WebRequestContentType.UrlEncodedGet);
                    helper.CookieContainer = cookieContainer;
                    responseFromServer     = helper.GetResponse();
                    coockieTime            = DateTime.Now;
                }
                if (!String.IsNullOrEmpty(responseFromServer))
                {
                    //{"source":"\u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430","translate":" \u043f\u0435\u0440\u0435\u0432\u0456\u0440\u043a\u0430","translate_part":"0","type":"p","index":0,"r":true}
                    string translation = StringParser.Parse("translate\":\"", "\"", responseFromServer);
                    result.Translations.Add(HttpUtilityEx.HtmlDecode(translation));
                }
                else
                {
                    throw new TranslationException("Nothing returned from call to " + url);
                }
            }
            else
            {
                throw new TranslationException("Nothing returned from call to http://translate.meta.ua/ajax/?sn=save_source");
            }
        }