实现功能

main
zggsong 6 months ago
parent 54f4173ea8
commit a7ffcb37b8

@ -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<string> 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")]

Loading…
Cancel
Save