实现功能

main
zggsong 6 months ago
parent 54f4173ea8
commit a7ffcb37b8

@ -1,35 +1,34 @@
using System.IO.Compression; using System.IO.Compression;
using System.Text.Json;
using System.Text; using System.Text;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace STranslateDLL; 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() private static void Initial()
{ {
var rand = new Random(); var rand = new Random();
var num = rand.NextInt64(99999) + 8300000; var number = rand.NextInt64(99999) + 8300000;
NextId = num * 1000; _nextId = number * 1000;
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)
{ {
Initial(); Initial();
} }
var getToken = token ?? CancellationToken.None; var getToken = token ?? CancellationToken.None;
long timeSpan = GenerateTimestamp(content); var timeSpan = GenerateTimestamp(content);
long id = CreateId(); var id = CreateId();
string reqStr = GenerateRequestStr(content, sourceLang, targetLang, timeSpan, id); var reqStr = GenerateRequestStr(content, sourceLang, targetLang, timeSpan, id);
using var client = new HttpClient(); using var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://www2.deepl.com/jsonrpc") 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("Referer", "https://www.deepl.com/");
request.Headers.Add("Connection", "keep-alive"); request.Headers.Add("Connection", "keep-alive");
HttpResponseMessage resp = await client.SendAsync(request, getToken); var resp = await client.SendAsync(request, getToken);
//resp.EnsureSuccessStatusCode(); //resp.EnsureSuccessStatusCode();
string responseBody; string responseBody;
if (resp.Content.Headers.ContentEncoding.Contains("br")) if (resp.Content.Headers.ContentEncoding.Contains("br"))
{ {
using var responseStream = await resp.Content.ReadAsStreamAsync(getToken); await using var responseStream = await resp.Content.ReadAsStreamAsync(getToken);
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);
} }
@ -81,13 +80,27 @@ public class LocalMode
private static long GenerateTimestamp(string texts) private static long GenerateTimestamp(string texts)
{ {
long iCount = texts.Split('i').Length - 1; 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; return iCount != 0 ? ts - ts % (iCount + 1) + (iCount + 1) : ts;
} }
private static long CreateId() 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) private static string GenerateRequestStr(string text, string? sourceLang, string targetLang, long timeSpan, long id)
@ -116,13 +129,19 @@ public class LocalMode
CommonJobParams = new ReqParamsCommonJobParams CommonJobParams = new ReqParamsCommonJobParams
{ {
WasSpoken = false, WasSpoken = false,
TranscribeAS = null TranscribeAS = ""
} }
}, },
Id = id 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; return json;
} }
} }
@ -188,7 +207,7 @@ public class DeepLResponse
[JsonProperty("jsonrpc")] [JsonProperty("jsonrpc")]
public string Jsonrpc { get; set; } = ""; public string Jsonrpc { get; set; } = "";
[JsonProperty("id")] [JsonProperty("id")]
public int Id { get; set; } public long Id { get; set; }
[JsonProperty("result")] [JsonProperty("result")]
public RespResult? Result { get; set; } public RespResult? Result { get; set; }
[JsonProperty("error")] [JsonProperty("error")]

Loading…
Cancel
Save