示例#1
0
        public async Task SetupTKK()
        {
            XUnityWebResponse response = null;

            _cookieContainer = new CookieContainer();

            try
            {
                var client  = new XUnityWebClient();
                var request = CreateWebSiteRequest();
                response = await client.SendAsync(request);
            }
            catch (Exception)
            {
                return;
            }

            // failure
            if (response.Error != null)
            {
                return;
            }

            // failure
            if (response.Data == null)
            {
                return;
            }

            InspectResponse(response);

            try
            {
                var html = response.Data;

                string[] lookups = new[] { "tkk:'", "TKK='" };
                foreach (var lookup in lookups)
                {
                    var index = html.IndexOf(lookup);
                    if (index > -1) // simple string approach
                    {
                        var startIndex = index + lookup.Length;
                        var endIndex   = html.IndexOf("'", startIndex);
                        var result     = html.Substring(startIndex, endIndex - startIndex);

                        var parts = result.Split('.');
                        if (parts.Length == 2)
                        {
                            m = long.Parse(parts[0]);
                            s = long.Parse(parts[1]);
                            break;
                        }
                    }
                }
            }
            catch (Exception)
            {
                // hard to warn...
            }
        }
        /// <summary>
        /// Attempt to translated the provided untranslated text. Will be used in a "coroutine",
        /// so it can be implemented in an asynchronous fashion.
        /// </summary>
        public IEnumerator Translate(ITranslationContext context)
        {
            var httpContext = new HttpTranslationContext(context);

            // allow implementer of HttpEndpoint to do anything before starting translation
            var setup = OnBeforeTranslate(httpContext);

            if (setup != null)
            {
                while (setup.MoveNext())
                {
                    yield return(setup.Current);
                }
            }

            // prepare request
            OnCreateRequest(httpContext);
            if (httpContext.Request == null)
            {
                httpContext.Fail("No request object was provided by the translator.");
            }

            // execute request
            var client   = new XUnityWebClient();
            var response = client.Send(httpContext.Request);

            // wait for completion
            var iterator = response.GetSupportedEnumerator();

            while (iterator.MoveNext())
            {
                yield return(iterator.Current);
            }

            if (response.IsTimedOut)
            {
                httpContext.Fail("Error occurred while retrieving translation. Timeout.");
            }

            httpContext.Response = response;
            OnInspectResponse(httpContext);

            // failure
            if (response.Error != null)
            {
                httpContext.Fail("Error occurred while retrieving translation.", response.Error);
            }

            // failure
            if (response.Data == null)
            {
                httpContext.Fail("Error occurred while retrieving translation. Nothing was returned.");
            }

            // extract text
            OnExtractTranslation(httpContext);
        }
示例#3
0
        private IEnumerator SetupVersion()
        {
            var client = new XUnityWebClient();

            string urlSource;

            // parse javascript url from main page
            {
                var response = client.Send(new XUnityWebRequest(UrlBase));

                var iterator = response.GetSupportedEnumerator();
                while (iterator.MoveNext())
                {
                    yield return(iterator.Current);
                }

                var match = PatternSource.Match(response.Data);
                if (!match.Success)
                {
                    XuaLogger.AutoTranslator.Warn("Could not parse papago page");
                    yield break;
                }

                urlSource = match.Value;
            }

            // parse version from javascript file
            {
                var response = client.Send(new XUnityWebRequest(UrlBase + urlSource));

                var iterator = response.GetSupportedEnumerator();
                while (iterator.MoveNext())
                {
                    yield return(iterator.Current);
                }

                var match = PatternVersion.Match(response.Data);
                if (!match.Success)
                {
                    XuaLogger.AutoTranslator.Warn("Could not parse papago version");
                    yield break;
                }

                XuaLogger.AutoTranslator.Debug($"Current papago version is {match.Value}");

                _version = match.Value;
            }
        }
        public override IEnumerator OnBeforeTranslate(IHttpTranslationContext context)
        {
            if (_translationCount % 133 == 0)
            {
                _cookies  = new CookieContainer();
                _deviceId = Guid.NewGuid().ToString();

                // terminate session?????

                var client  = new XUnityWebClient();
                var request = new XUnityWebRequest(Website);
                request.Cookies = _cookies;

                SetupDefaultHeaders(request);

                var response = client.Send(request);
                while (response.MoveNext())
                {
                    yield return(response.Current);
                }

                // dont actually cared about the response, just the cookies
            }
        }
示例#5
0
        public IEnumerator SetupIGAndIID()
        {
            XUnityWebResponse response = null;

            _cookieContainer  = new CookieContainer();
            _translationCount = 0;

            try
            {
                var client  = new XUnityWebClient();
                var request = CreateWebSiteRequest();
                response = client.Send(request);
            }
            catch (Exception e)
            {
                XuaLogger.Current.Warn(e, "An error occurred while setting up BingTranslate IG. Proceeding without...");
                yield break;
            }

            var iterator = response.GetSupportedEnumerator();

            while (iterator.MoveNext())
            {
                yield return(iterator.Current);
            }

            if (response.IsTimedOut)
            {
                XuaLogger.Current.Warn("A timeout error occurred while setting up BingTranslate IG. Proceeding without...");
                yield break;
            }

            // failure
            if (response.Error != null)
            {
                XuaLogger.Current.Warn(response.Error, "An error occurred while setting up BingTranslate IG. Proceeding without...");
                yield break;
            }

            // failure
            if (response.Data == null)
            {
                XuaLogger.Current.Warn(null, "An error occurred while setting up BingTranslate IG. Proceeding without...");
                yield break;
            }

            InspectResponse(response);

            try
            {
                var html = response.Data;

                _ig  = Lookup("ig\":\"", html);
                _iid = Lookup(".init(\"/feedback/submission?\",\"", html);

                if (_ig == null || _iid == null)
                {
                    XuaLogger.Current.Warn("An error occurred while setting up BingTranslate IG/IID. Proceeding without...");
                }
            }
            catch (Exception e)
            {
                XuaLogger.Current.Warn(e, "An error occurred while setting up BingTranslate IG. Proceeding without...");
            }
        }
        public IEnumerator SetupTKK()
        {
            XUnityWebResponse response = null;

            _cookieContainer = new CookieContainer();

            try
            {
                var client  = new XUnityWebClient();
                var request = CreateWebSiteRequest();
                response = client.Send(request);
            }
            catch (Exception e)
            {
                XuaLogger.AutoTranslator.Warn(e, "An error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead.");
                yield break;
            }

            // wait for response completion
            var iterator = response.GetSupportedEnumerator();

            while (iterator.MoveNext())
            {
                yield return(iterator.Current);
            }

            if (response.IsTimedOut)
            {
                XuaLogger.AutoTranslator.Warn("A timeout error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead.");
                yield break;
            }

            // failure
            if (response.Error != null)
            {
                XuaLogger.AutoTranslator.Warn(response.Error, "An error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead.");
                yield break;
            }

            // failure
            if (response.Data == null)
            {
                XuaLogger.AutoTranslator.Warn(null, "An error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead.");
                yield break;
            }

            InspectResponse(response);

            try
            {
                var html = response.Data;

                bool     found   = false;
                string[] lookups = new[] { "tkk:'", "TKK='" };
                foreach (var lookup in lookups)
                {
                    var index = html.IndexOf(lookup);
                    if (index > -1) // simple string approach
                    {
                        var startIndex = index + lookup.Length;
                        var endIndex   = html.IndexOf("'", startIndex);
                        var result     = html.Substring(startIndex, endIndex - startIndex);

                        var parts = result.Split('.');
                        if (parts.Length == 2)
                        {
                            m     = long.Parse(parts[0]);
                            s     = long.Parse(parts[1]);
                            found = true;
                            break;
                        }
                    }
                }

                if (!found)
                {
                    XuaLogger.AutoTranslator.Warn("An error occurred while setting up GoogleTranslate TKK. Could not locate TKK value. Using fallback TKK values instead.");
                }
            }
            catch (Exception e)
            {
                XuaLogger.AutoTranslator.Warn(e, "An error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead.");
            }
        }
        public IEnumerator SetupFSID()
        {
            XUnityWebResponse response = null;

            _cookieContainer = new CookieContainer();

            try
            {
                var client  = new XUnityWebClient();
                var request = CreateWebSiteRequest();
                response = client.Send(request);
            }
            catch (Exception e)
            {
                XuaLogger.AutoTranslator.Warn(e, "An error occurred while setting up GoogleTranslate FSID. Using random instead.");
                yield break;
            }

            // wait for response completion
            var iterator = response.GetSupportedEnumerator();

            while (iterator.MoveNext())
            {
                yield return(iterator.Current);
            }

            if (response.IsTimedOut)
            {
                XuaLogger.AutoTranslator.Warn("A timeout error occurred while setting up GoogleTranslate FSID. Using random instead.");
                yield break;
            }

            // failure
            if (response.Error != null)
            {
                XuaLogger.AutoTranslator.Warn(response.Error, "An error occurred while setting up GoogleTranslate FSID. Using random instead.");
                yield break;
            }

            // failure
            if (response.Data == null)
            {
                XuaLogger.AutoTranslator.Warn(null, "An error occurred while setting up GoogleTranslate FSID. Using random instead.");
                yield break;
            }

            try
            {
                var html = response.Data;

                bool     found   = false;
                string[] lookups = new[] { "FdrFJe\":\"" };
                foreach (var lookup in lookups)
                {
                    var index = html.IndexOf(lookup);
                    if (index > -1) // simple string approach
                    {
                        var startIndex = index + lookup.Length;
                        var endIndex   = html.IndexOf("\"", startIndex);
                        var result     = html.Substring(startIndex, endIndex - startIndex);

                        _FSID = long.Parse(result, CultureInfo.InvariantCulture);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    XuaLogger.AutoTranslator.Warn("An error occurred while setting up GoogleTranslate FSID. Could not locate FSID value. Using random instead.");
                }
            }
            catch (Exception e)
            {
                XuaLogger.AutoTranslator.Warn(e, "An error occurred while setting up GoogleTranslate FSID. Using random instead.");
            }
        }