From 54f4173ea8d11b795030f40b958fe65ab654904d Mon Sep 17 00:00:00 2001 From: zggsong Date: Sun, 12 May 2024 14:39:00 +0800 Subject: [PATCH 1/2] update ignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8eb39dd..627dea1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ bin/ obj/ *.user - +.DS_Store From a7ffcb37b82c85ef5e2c3f0089329aa13170385f Mon Sep 17 00:00:00 2001 From: zggsong Date: Sun, 12 May 2024 17:51:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STranslateDLL/LocalMode.cs | 57 +++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/STranslateDLL/LocalMode.cs b/STranslateDLL/LocalMode.cs index 4e59c1a..9d0edfd 100644 --- a/STranslateDLL/LocalMode.cs +++ b/STranslateDLL/LocalMode.cs @@ -1,35 +1,34 @@ using System.IO.Compression; -using System.Text.Json; using System.Text; using Newtonsoft.Json; namespace STranslateDLL; -public class LocalMode +public static class LocalMode { - private static long NextId; + private static long _nextId; - private static bool HasInit = false; + private static bool _hasInit = false; private static void Initial() { var rand = new Random(); - var num = rand.NextInt64(99999) + 8300000; - NextId = num * 1000; + var number = rand.NextInt64(99999) + 8300000; + _nextId = number * 1000; - HasInit = true; + _hasInit = true; } public static async Task ExecuteAsync(string content, string? sourceLang = null, string targetLang = "ZH", CancellationToken? token = null) { - if (!HasInit) + if (!_hasInit) { Initial(); } var getToken = token ?? CancellationToken.None; - long timeSpan = GenerateTimestamp(content); - long id = CreateId(); - string reqStr = GenerateRequestStr(content, sourceLang, targetLang, timeSpan, id); + var timeSpan = GenerateTimestamp(content); + var id = CreateId(); + var reqStr = GenerateRequestStr(content, sourceLang, targetLang, timeSpan, id); using var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Post, "https://www2.deepl.com/jsonrpc") @@ -51,15 +50,15 @@ public class LocalMode request.Headers.Add("Referer", "https://www.deepl.com/"); request.Headers.Add("Connection", "keep-alive"); - HttpResponseMessage resp = await client.SendAsync(request, getToken); + var resp = await client.SendAsync(request, getToken); //resp.EnsureSuccessStatusCode(); string responseBody; if (resp.Content.Headers.ContentEncoding.Contains("br")) { - using var responseStream = await resp.Content.ReadAsStreamAsync(getToken); - using var decompressionStream = new BrotliStream(responseStream, CompressionMode.Decompress); + await using var responseStream = await resp.Content.ReadAsStreamAsync(getToken); + await using var decompressionStream = new BrotliStream(responseStream, CompressionMode.Decompress); using var streamReader = new StreamReader(decompressionStream); responseBody = await streamReader.ReadToEndAsync(getToken); } @@ -81,13 +80,27 @@ public class LocalMode private static long GenerateTimestamp(string texts) { long iCount = texts.Split('i').Length - 1; - long ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); return iCount != 0 ? ts - ts % (iCount + 1) + (iCount + 1) : ts; } private static long CreateId() { - return Interlocked.Increment(ref NextId); + return Interlocked.Increment(ref _nextId); + } + + private static string AdjustJsonContent(string json, long id) + { + string method; + if ((id + 3) % 13 == 0 || (id + 5) % 29 == 0) + { + method = "\"method\" : \""; + } + else + { + method = "\"method\": \""; + } + return json.Replace("\"method\":\"", method); } private static string GenerateRequestStr(string text, string? sourceLang, string targetLang, long timeSpan, long id) @@ -116,13 +129,19 @@ public class LocalMode CommonJobParams = new ReqParamsCommonJobParams { WasSpoken = false, - TranscribeAS = null + TranscribeAS = "" } }, Id = id }; - string json = JsonConvert.SerializeObject(req); + var json = JsonConvert.SerializeObject(req); + + var count = json.Length > 300 ? 0 : 3; + req.Params.Texts.First().RequestAlternatives = count; + + json = JsonConvert.SerializeObject(req); + json = AdjustJsonContent(json, id); return json; } } @@ -188,7 +207,7 @@ public class DeepLResponse [JsonProperty("jsonrpc")] public string Jsonrpc { get; set; } = ""; [JsonProperty("id")] - public int Id { get; set; } + public long Id { get; set; } [JsonProperty("result")] public RespResult? Result { get; set; } [JsonProperty("error")]