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