GoogleTranslateHackEndpoint.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using SimpleJSON;
  7. using UnityEngine;
  8. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  9. using XUnity.AutoTranslator.Plugin.Core.Constants;
  10. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  11. namespace XUnity.AutoTranslator.Plugin.Core.Web
  12. {
  13. public class GoogleTranslateHackEndpoint : KnownEndpoint
  14. {
  15. private static ServicePoint ServicePoint;
  16. private static readonly string HttpsServicePointTemplateUrl = "https://translate.google.com/m?hl=pl&sl={0}&tl={1}&ie=UTF-8&q={2}";
  17. // Author: Johnny Cee (https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp)
  18. private static string getBetween(string strSource, string strStart, string strEnd)
  19. {
  20. const int kNotFound = -1;
  21. var startIdx = strSource.IndexOf(strStart);
  22. if (startIdx != kNotFound)
  23. {
  24. startIdx += strStart.Length;
  25. var endIdx = strSource.IndexOf(strEnd, startIdx);
  26. if (endIdx > startIdx)
  27. {
  28. return strSource.Substring(startIdx, endIdx - startIdx);
  29. }
  30. }
  31. return String.Empty;
  32. }
  33. public GoogleTranslateHackEndpoint()
  34. : base(KnownEndpointNames.GoogleTranslateHack)
  35. {
  36. }
  37. public override void ApplyHeaders(Dictionary<string, string> headers)
  38. {
  39. headers["User-Agent"] = "Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/28.2725; U; en) Presto/2.8.119 Version/11.10";
  40. headers["Accept"] = "*/*";
  41. headers["Accept-Charset"] = "UTF-8";
  42. }
  43. public override void ApplyHeaders(WebHeaderCollection headers)
  44. {
  45. headers[HttpRequestHeader.UserAgent] = "Opera/9.80 (J2ME/MIDP; Opera Mini/5.1.21214/28.2725; U; en) Presto/2.8.119 Version/11.10";
  46. headers[HttpRequestHeader.Accept] = "*/*";
  47. headers[HttpRequestHeader.AcceptCharset] = "UTF-8";
  48. }
  49. public override void ConfigureServicePointManager()
  50. {
  51. try
  52. {
  53. ServicePoint = ServicePointManager.FindServicePoint(new Uri("https://translate.google.com"));
  54. ServicePoint.ConnectionLimit = Settings.MaxConcurrentTranslations;
  55. }
  56. catch
  57. {
  58. }
  59. }
  60. public override bool TryExtractTranslated(string result, out string translated)
  61. {
  62. try
  63. {
  64. String extracted = getBetween(result, "class=\"t0\">", "</div>");
  65. if (String.IsNullOrEmpty(extracted))
  66. {
  67. translated = null;
  68. return false;
  69. }
  70. else
  71. {
  72. translated = RestSharp.Contrib.HttpUtility.HtmlDecode(extracted);
  73. return true;
  74. }
  75. }
  76. catch
  77. {
  78. translated = null;
  79. return false;
  80. }
  81. }
  82. public override string GetServiceUrl(string untranslatedText, string from, string to)
  83. {
  84. return string.Format(HttpsServicePointTemplateUrl, from, to, WWW.EscapeURL(untranslatedText));
  85. }
  86. }
  87. }