|
|
@ -1,6 +1,8 @@
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace STranslateDLL;
|
|
|
|
namespace STranslateDLL;
|
|
|
|
|
|
|
|
|
|
|
@ -19,7 +21,12 @@ public static class LocalMode
|
|
|
|
_hasInit = true;
|
|
|
|
_hasInit = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task<string> ExecuteAsync(string content, string? sourceLang = null, string targetLang = "ZH", CancellationToken? token = null)
|
|
|
|
public static async Task<string> ExecuteAsync(
|
|
|
|
|
|
|
|
string content,
|
|
|
|
|
|
|
|
string? sourceLang = null,
|
|
|
|
|
|
|
|
string targetLang = "ZH",
|
|
|
|
|
|
|
|
CancellationToken? token = null
|
|
|
|
|
|
|
|
)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!_hasInit)
|
|
|
|
if (!_hasInit)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -58,7 +65,10 @@ public static class LocalMode
|
|
|
|
if (resp.Content.Headers.ContentEncoding.Contains("br"))
|
|
|
|
if (resp.Content.Headers.ContentEncoding.Contains("br"))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await using var responseStream = await resp.Content.ReadAsStreamAsync(getToken);
|
|
|
|
await using var responseStream = await resp.Content.ReadAsStreamAsync(getToken);
|
|
|
|
await using var decompressionStream = new BrotliStream(responseStream, CompressionMode.Decompress);
|
|
|
|
await using var decompressionStream = new BrotliStream(
|
|
|
|
|
|
|
|
responseStream,
|
|
|
|
|
|
|
|
CompressionMode.Decompress
|
|
|
|
|
|
|
|
);
|
|
|
|
using var streamReader = new StreamReader(decompressionStream);
|
|
|
|
using var streamReader = new StreamReader(decompressionStream);
|
|
|
|
responseBody = await streamReader.ReadToEndAsync(getToken);
|
|
|
|
responseBody = await streamReader.ReadToEndAsync(getToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -67,17 +77,20 @@ public static class LocalMode
|
|
|
|
responseBody = await resp.Content.ReadAsStringAsync(getToken);
|
|
|
|
responseBody = await resp.Content.ReadAsStringAsync(getToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var deeplResp = JsonConvert.DeserializeObject<DeepLResponse>(responseBody);
|
|
|
|
var deeplResp = JsonSerializer.Deserialize<DeepLResponse>(responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
var response = new Response
|
|
|
|
var response = new Response { Code = resp.StatusCode.GetHashCode() };
|
|
|
|
{
|
|
|
|
|
|
|
|
Code = resp.StatusCode.GetHashCode()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string defaultMessage = resp.StatusCode == System.Net.HttpStatusCode.OK ? "Empty Result" : "Empty Error Message";
|
|
|
|
string defaultMessage =
|
|
|
|
response.Data = deeplResp?.Result?.Texts?.FirstOrDefault()?.Text ?? deeplResp?.Error?.Message ?? defaultMessage;
|
|
|
|
resp.StatusCode == System.Net.HttpStatusCode.OK
|
|
|
|
|
|
|
|
? "Empty Result"
|
|
|
|
|
|
|
|
: "Empty Error Message";
|
|
|
|
|
|
|
|
response.Data =
|
|
|
|
|
|
|
|
deeplResp?.Result?.Texts?.FirstOrDefault()?.Text
|
|
|
|
|
|
|
|
?? deeplResp?.Error?.Message
|
|
|
|
|
|
|
|
?? defaultMessage;
|
|
|
|
|
|
|
|
|
|
|
|
return JsonConvert.SerializeObject(response);
|
|
|
|
return JsonSerializer.Serialize(response, GetOptions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static long GenerateTimestamp(string texts)
|
|
|
|
private static long GenerateTimestamp(string texts)
|
|
|
@ -91,7 +104,7 @@ public static class LocalMode
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Interlocked.Increment(ref _nextId);
|
|
|
|
return Interlocked.Increment(ref _nextId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string AdjustJsonContent(string json, long id)
|
|
|
|
private static string AdjustJsonContent(string json, long id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string method;
|
|
|
|
string method;
|
|
|
@ -106,7 +119,20 @@ public static class LocalMode
|
|
|
|
return json.Replace("\"method\":\"", method);
|
|
|
|
return json.Replace("\"method\":\"", method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GenerateRequestStr(string text, string? sourceLang, string targetLang, long timeSpan, long id)
|
|
|
|
private static JsonSerializerOptions GetOptions =>
|
|
|
|
|
|
|
|
new()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
|
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string GenerateRequestStr(
|
|
|
|
|
|
|
|
string text,
|
|
|
|
|
|
|
|
string? sourceLang,
|
|
|
|
|
|
|
|
string targetLang,
|
|
|
|
|
|
|
|
long timeSpan,
|
|
|
|
|
|
|
|
long id
|
|
|
|
|
|
|
|
)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var req = new DeepLRequest
|
|
|
|
var req = new DeepLRequest
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -114,14 +140,7 @@ public static class LocalMode
|
|
|
|
Method = "LMT_handle_texts",
|
|
|
|
Method = "LMT_handle_texts",
|
|
|
|
Params = new ReqParams
|
|
|
|
Params = new ReqParams
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Texts =
|
|
|
|
Texts = [new ReqParamsTexts { Text = text, RequestAlternatives = 0 }],
|
|
|
|
[
|
|
|
|
|
|
|
|
new ReqParamsTexts
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Text = text,
|
|
|
|
|
|
|
|
RequestAlternatives = 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
Splitting = "newlines",
|
|
|
|
Splitting = "newlines",
|
|
|
|
Lang = new ReqParamsLang
|
|
|
|
Lang = new ReqParamsLang
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -138,12 +157,11 @@ public static class LocalMode
|
|
|
|
Id = id
|
|
|
|
Id = id
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var json = JsonConvert.SerializeObject(req);
|
|
|
|
var json = JsonSerializer.Serialize(req, GetOptions);
|
|
|
|
|
|
|
|
|
|
|
|
var count = json.Length > 300 ? 0 : 3;
|
|
|
|
var count = json.Length > 300 ? 0 : 3;
|
|
|
|
req.Params.Texts.First().RequestAlternatives = count;
|
|
|
|
req.Params.Texts.First().RequestAlternatives = count;
|
|
|
|
|
|
|
|
json = JsonSerializer.Serialize(req, GetOptions);
|
|
|
|
json = JsonConvert.SerializeObject(req);
|
|
|
|
|
|
|
|
json = AdjustJsonContent(json, id);
|
|
|
|
json = AdjustJsonContent(json, id);
|
|
|
|
return json;
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -157,83 +175,100 @@ public class Response
|
|
|
|
|
|
|
|
|
|
|
|
public class DeepLRequest
|
|
|
|
public class DeepLRequest
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("jsonrpc")]
|
|
|
|
[JsonPropertyName("jsonrpc")]
|
|
|
|
public string Jsonrpc { get; set; } = "";
|
|
|
|
public string Jsonrpc { get; set; } = "";
|
|
|
|
[JsonProperty("method")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("method")]
|
|
|
|
public string Method { get; set; } = "";
|
|
|
|
public string Method { get; set; } = "";
|
|
|
|
[JsonProperty("params")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("params")]
|
|
|
|
public ReqParams? Params { get; set; }
|
|
|
|
public ReqParams? Params { get; set; }
|
|
|
|
[JsonProperty("id")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("id")]
|
|
|
|
public long Id { get; set; }
|
|
|
|
public long Id { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ReqParams
|
|
|
|
public class ReqParams
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("texts")]
|
|
|
|
[JsonPropertyName("texts")]
|
|
|
|
public ReqParamsTexts[]? Texts { get; set; }
|
|
|
|
public ReqParamsTexts[]? Texts { get; set; }
|
|
|
|
[JsonProperty("splitting")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("splitting")]
|
|
|
|
public string Splitting { get; set; } = "";
|
|
|
|
public string Splitting { get; set; } = "";
|
|
|
|
[JsonProperty("lang")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("lang")]
|
|
|
|
public ReqParamsLang? Lang { get; set; }
|
|
|
|
public ReqParamsLang? Lang { get; set; }
|
|
|
|
[JsonProperty("timestamp")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("timestamp")]
|
|
|
|
public long Timestamp { get; set; }
|
|
|
|
public long Timestamp { get; set; }
|
|
|
|
[JsonProperty("commonJobParams")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("commonJobParams")]
|
|
|
|
public ReqParamsCommonJobParams? CommonJobParams { get; set; }
|
|
|
|
public ReqParamsCommonJobParams? CommonJobParams { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ReqParamsTexts
|
|
|
|
public class ReqParamsTexts
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("text")]
|
|
|
|
[JsonPropertyName("text")]
|
|
|
|
public string Text { get; set; } = "";
|
|
|
|
public string Text { get; set; } = "";
|
|
|
|
[JsonProperty("requestAlternatives")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("requestAlternatives")]
|
|
|
|
public int RequestAlternatives { get; set; }
|
|
|
|
public int RequestAlternatives { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ReqParamsLang
|
|
|
|
public class ReqParamsLang
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("source_lang_user_selected", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
|
|
|
[JsonPropertyName("source_lang_user_selected")]
|
|
|
|
public string? SourceLangUserSelected { get; set; }
|
|
|
|
public string? SourceLangUserSelected { get; set; }
|
|
|
|
[JsonProperty("target_lang")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("target_lang")]
|
|
|
|
public string TargetLang { get; set; } = "";
|
|
|
|
public string TargetLang { get; set; } = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class ReqParamsCommonJobParams
|
|
|
|
public class ReqParamsCommonJobParams
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("wasSpoken")]
|
|
|
|
[JsonPropertyName("wasSpoken")]
|
|
|
|
public bool WasSpoken { get; set; }
|
|
|
|
public bool WasSpoken { get; set; }
|
|
|
|
[JsonProperty("transcribe_as", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("transcribe_as")]
|
|
|
|
public string? TranscribeAS { get; set; }
|
|
|
|
public string? TranscribeAS { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class DeepLResponse
|
|
|
|
public class DeepLResponse
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("jsonrpc")]
|
|
|
|
[JsonPropertyName("jsonrpc")]
|
|
|
|
public string Jsonrpc { get; set; } = "";
|
|
|
|
public string Jsonrpc { get; set; } = "";
|
|
|
|
[JsonProperty("id")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("id")]
|
|
|
|
public long Id { get; set; }
|
|
|
|
public long Id { get; set; }
|
|
|
|
[JsonProperty("result")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("result")]
|
|
|
|
public RespResult? Result { get; set; }
|
|
|
|
public RespResult? Result { get; set; }
|
|
|
|
[JsonProperty("error")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("error")]
|
|
|
|
public RespError? Error { get; set; }
|
|
|
|
public RespError? Error { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class RespResult
|
|
|
|
public class RespResult
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("texts")]
|
|
|
|
[JsonPropertyName("texts")]
|
|
|
|
public RespResultText[]? Texts { get; set; }
|
|
|
|
public RespResultText[]? Texts { get; set; }
|
|
|
|
[JsonProperty("lang")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("lang")]
|
|
|
|
public string Lang { get; set; } = "";
|
|
|
|
public string Lang { get; set; } = "";
|
|
|
|
[JsonProperty("lang_is_confident")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("lang_is_confident")]
|
|
|
|
public bool LangIsConfident { get; set; }
|
|
|
|
public bool LangIsConfident { get; set; }
|
|
|
|
[JsonProperty("detectedLanguages")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("detectedLanguages")]
|
|
|
|
public RespResultDetectedLanguages? DetectedLanguages { get; set; }
|
|
|
|
public RespResultDetectedLanguages? DetectedLanguages { get; set; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class RespResultText
|
|
|
|
public class RespResultText
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("alternatives")]
|
|
|
|
[JsonPropertyName("alternatives")]
|
|
|
|
public object[]? Alternatives { get; set; }
|
|
|
|
public object[]? Alternatives { get; set; }
|
|
|
|
[JsonProperty("text")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("text")]
|
|
|
|
public string Text { get; set; } = "";
|
|
|
|
public string Text { get; set; } = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -270,8 +305,9 @@ public class RespResultDetectedLanguages
|
|
|
|
|
|
|
|
|
|
|
|
public class RespError
|
|
|
|
public class RespError
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[JsonProperty("code")]
|
|
|
|
[JsonPropertyName("code")]
|
|
|
|
public int Code { get; set; }
|
|
|
|
public int Code { get; set; }
|
|
|
|
[JsonProperty("message")]
|
|
|
|
|
|
|
|
|
|
|
|
[JsonPropertyName("message")]
|
|
|
|
public string Message { get; set; } = "";
|
|
|
|
public string Message { get; set; } = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|