BingTranslateLegitimateEndpoint.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Text;
  9. using Harmony;
  10. using SimpleJSON;
  11. using UnityEngine;
  12. using XUnity.AutoTranslator.Plugin.Core.Configuration;
  13. using XUnity.AutoTranslator.Plugin.Core.Constants;
  14. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  15. namespace XUnity.AutoTranslator.Plugin.Core.Web
  16. {
  17. public class BingTranslateLegitimateEndpoint : KnownHttpEndpoint
  18. {
  19. private static readonly string HttpsServicePointTemplateUrl = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={0}&to={1}";
  20. private static readonly string RequestTemplate = "[{{\"Text\":\"{0}\"}}]";
  21. private static readonly System.Random RandomNumbers = new System.Random();
  22. private static readonly string[] Accepts = new string[] { "application/json" };
  23. private static readonly string[] ContentTypes = new string[] { "application/json" };
  24. private static readonly string Accept = Accepts[ RandomNumbers.Next( Accepts.Length ) ];
  25. private static readonly string ContentType = ContentTypes[ RandomNumbers.Next( ContentTypes.Length ) ];
  26. private string _key;
  27. public BingTranslateLegitimateEndpoint( string key )
  28. {
  29. if( string.IsNullOrEmpty( key ) ) throw new ArgumentException( "The BingTranslateLegitimate endpoint requires an API key which has not been provided.", nameof( key ) );
  30. _key = key;
  31. // Configure service points / service point manager
  32. ServicePointManager.ServerCertificateValidationCallback += Security.AlwaysAllowByHosts( "api.cognitive.microsofttranslator.com" );
  33. SetupServicePoints( "https://api.cognitive.microsofttranslator.com" );
  34. }
  35. public override bool SupportsLineSplitting => false;
  36. public override void ApplyHeaders( WebHeaderCollection headers )
  37. {
  38. if( Accept != null )
  39. {
  40. headers[ HttpRequestHeader.Accept ] = Accept;
  41. }
  42. if( ContentType != null )
  43. {
  44. headers[ HttpRequestHeader.ContentType ] = ContentType;
  45. }
  46. headers[ "Ocp-Apim-Subscription-Key" ] = _key;
  47. }
  48. public override bool TryExtractTranslated( string result, out string translated )
  49. {
  50. try
  51. {
  52. var arr = JSON.Parse( result );
  53. var token = arr.AsArray[ 0 ]?.AsObject[ "translations" ]?.AsArray[ 0 ]?.AsObject[ "text" ]?.ToString();
  54. token = token.Substring( 1, token.Length - 2 ).UnescapeJson();
  55. translated = token;
  56. var success = !string.IsNullOrEmpty( translated );
  57. return success;
  58. }
  59. catch
  60. {
  61. translated = null;
  62. return false;
  63. }
  64. }
  65. public override string GetRequestObject( string untranslatedText, string from, string to )
  66. {
  67. return string.Format( RequestTemplate, untranslatedText.EscapeJson() );
  68. }
  69. public override string GetServiceUrl( string untranslatedText, string from, string to )
  70. {
  71. return string.Format( HttpsServicePointTemplateUrl, from, to );
  72. }
  73. }
  74. }