public DownloadResult GetDownloadResult(Uri address) { var handle = new DownloadResult(); DownloadStringAsync(address, handle); return(handle); }
public DownloadResult GetDownloadResult(Uri address, string request) { var handle = new DownloadResult(); UploadStringAsync(address, "POST", request, handle); return(handle); }
public IEnumerator SetupTKK() { string error = null; DownloadResult downloadResult = null; _cookieContainer = new CookieContainer(); var client = GetClient(); try { ApplyHeaders(client.Headers); downloadResult = client.GetDownloadResult(new Uri(HttpsTranslateUserSite)); } catch (Exception e) { error = e.ToString(); } if (downloadResult != null) { yield return(downloadResult); error = downloadResult.Error; if (downloadResult.Succeeded) { try { var html = downloadResult.Result; const string lookup = "TKK=eval('"; var lookupIndex = html.IndexOf(lookup) + lookup.Length; var openClamIndex = html.IndexOf('{', lookupIndex); var closeClamIndex = html.IndexOf('}', openClamIndex); var functionIndex = html.IndexOf("function", lookupIndex); var script = html.Substring(functionIndex, closeClamIndex - functionIndex + 1); var decodedScript = script.Replace("\\x3d", "=").Replace("\\x27", "'").Replace("function", "function FuncName"); // https://github.com/paulbartrum/jurassic/wiki/Safely-executing-user-provided-scripts ScriptEngine engine = new ScriptEngine(); engine.Evaluate(decodedScript); var result = engine.CallGlobalFunction <string>("FuncName"); var parts = result.Split('.'); m = long.Parse(parts[0]); s = long.Parse(parts[1]); } catch (Exception e) { error = e.ToString(); } } } if (error != null) { Logger.Current.Error("An error occurred while setting up GoogleTranslate Cookie/TKK." + Environment.NewLine + error); } }
public IEnumerator Translate(string untranslatedText, string from, string to, Action <string> success, Action failure) { _clientLastUse = DateTime.UtcNow; try { _isSettingUp = true; var setup = OnBeforeTranslate(Settings.TranslationCount); if (setup != null) { while (setup.MoveNext()) { yield return(setup.Current); } } } finally { _isSettingUp = false; } Logger.Current.Debug("Starting translation for: " + untranslatedText); DownloadResult result = null; try { var client = GetClient(); var url = GetServiceUrl(untranslatedText, from, to); ApplyHeaders(client.Headers); result = client.GetDownloadResult(new Uri(url)); } catch (Exception e) { Logger.Current.Error(e, "Error occurred while setting up translation request."); } if (result != null) { try { _runningTranslations++; yield return(result); _runningTranslations--; try { if (result.Succeeded) { if (TryExtractTranslated(result.Result, out var translatedText)) { Logger.Current.Debug($"Translation for '{untranslatedText}' succeded. Result: {translatedText}"); translatedText = translatedText ?? string.Empty; success(translatedText); } else { Logger.Current.Error("Error occurred while extracting translation."); failure(); } } else { Logger.Current.Error("Error occurred while retrieving translation." + Environment.NewLine + result.Error); failure(); } } catch (Exception e) { Logger.Current.Error(e, "Error occurred while retrieving translation."); failure(); } } finally { _clientLastUse = DateTime.UtcNow; } } }
public IEnumerator Translate(string untranslatedText, string from, string to, Action <string> success, Action failure) { _isBusy = true; try { var setup = OnBeforeTranslate(Settings.TranslationCount); if (setup != null) { while (setup.MoveNext()) { yield return(setup.Current); } } Logger.Current.Debug("Starting translation for: " + untranslatedText); DownloadResult downloadResult = null; try { var client = GetClient(); var url = GetServiceUrl(untranslatedText, from, to); var request = GetRequestObject(untranslatedText, from, to); ApplyHeaders(client.Headers); if (request != null) { downloadResult = client.GetDownloadResult(new Uri(url), request); } else { downloadResult = client.GetDownloadResult(new Uri(url)); } } catch (Exception e) { Logger.Current.Error(e, "Error occurred while setting up translation request."); } if (downloadResult != null) { if (Features.SupportsCustomYieldInstruction) { yield return(downloadResult); } else { while (!downloadResult.IsCompleted) { yield return(new WaitForSeconds(0.2f)); } } try { if (downloadResult.Succeeded && downloadResult.Result != null) { if (TryExtractTranslated(downloadResult.Result, out var translatedText)) { Logger.Current.Debug($"Translation for '{untranslatedText}' succeded. Result: {translatedText}"); translatedText = translatedText ?? string.Empty; success(translatedText); } else { Logger.Current.Error("Error occurred while extracting translation."); failure(); } } else { Logger.Current.Error("Error occurred while retrieving translation." + Environment.NewLine + downloadResult.Error); failure(); } } catch (Exception e) { Logger.Current.Error(e, "Error occurred while retrieving translation."); failure(); } } else { failure(); } } finally { _clientLastUse = DateTime.UtcNow; _isBusy = false; } }
public IEnumerator SetupIGAndIID() { string error = null; DownloadResult downloadResult = null; _cookieContainer = new CookieContainer(); _translationCount = 0; var client = GetClient(); try { ApplyHeaders(client.Headers); client.Headers.Remove(HttpRequestHeader.Referer); client.Headers.Remove("Origin"); client.Headers.Remove(HttpRequestHeader.ContentType); downloadResult = client.GetDownloadResult(new Uri(HttpsTranslateUserSite)); } catch (Exception e) { error = e.ToString(); } if (downloadResult != null) { if (Features.SupportsCustomYieldInstruction) { yield return(downloadResult); } else { while (!downloadResult.IsCompleted) { yield return(new WaitForSeconds(0.2f)); } } error = downloadResult.Error; if (downloadResult.Succeeded && downloadResult.Result != null) { try { var html = downloadResult.Result; _ig = Lookup("ig\":\"", html); _iid = Lookup(".init(\"/feedback/submission?\",\"", html); if (_ig == null || _iid == null) { Logger.Current.Warn("An error occurred while setting up BingTranslate IG/IID. Proceeding without..."); } } catch (Exception e) { error = e.ToString(); } } } if (error != null) { Logger.Current.Warn("An error occurred while setting up BingTranslate IG. Proceeding without..." + Environment.NewLine + error); } }
//public IEnumerator SetupDynamicUserAgent() //{ // // have to use WWW for this because unity mono is broken // if( WwwConstructor != null ) // { // object www; // try // { // var headers = new Dictionary<string, string>(); // www = WwwConstructor.Invoke( new object[] { UserAgentRepository, null, headers } ); // } // catch( Exception e ) // { // Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." ); // yield break; // } // yield return www; // try // { // var error = (string)AccessTools.Property( Constants.Types.WWW, "error" ).GetValue( www, null ); // if( error == null ) // { // var text = (string)AccessTools.Property( Constants.Types.WWW, "text" ).GetValue( www, null ); // var userAgents = text.GetBetween( "<textarea rows=\"10\" class=\"get-the-list\" onclick=\"this.select()\" readonly=\"readonly\">", "</textarea>" ); // if( userAgents.Length > 42 ) // { // var reader = new StringReader( userAgents ); // var popularUserAgent = reader.ReadLine(); // if( popularUserAgent.Length > 30 && popularUserAgent.Length < 300 && popularUserAgent.StartsWith( "Mozilla/" ) ) // { // _popularUserAgent = popularUserAgent; // } // else // { // Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." ); // } // } // else // { // Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Could not find a user agent in returned html." ); // } // } // else // { // Logger.Current.Warn( "An error occurred while retrieving dynamic user agent. Request failed: " + Environment.NewLine + error ); // } // } // catch( Exception e ) // { // Logger.Current.Warn( e, "An error occurred while retrieving dynamic user agent." ); // } // } //} public IEnumerator SetupTKK() { string error = null; DownloadResult downloadResult = null; _cookieContainer = new CookieContainer(); var client = GetClient(); try { ApplyHeaders(client.Headers); client.Headers.Remove(HttpRequestHeader.Referer); downloadResult = client.GetDownloadResult(new Uri(HttpsTranslateUserSite)); } catch (Exception e) { error = e.ToString(); } if (downloadResult != null) { if (Features.SupportsCustomYieldInstruction) { yield return(downloadResult); } else { while (!downloadResult.IsCompleted) { yield return(new WaitForSeconds(0.2f)); } } error = downloadResult.Error; if (downloadResult.Succeeded && downloadResult.Result != null) { try { var html = downloadResult.Result; 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) { Logger.Current.Warn("An error occurred while setting up GoogleTranslate TKK. Could not locate TKK value. Using fallback TKK values instead."); } } catch (Exception e) { error = e.ToString(); } } } if (error != null) { Logger.Current.Warn("An error occurred while setting up GoogleTranslate TKK. Using fallback TKK values instead." + Environment.NewLine + error); } }