You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
using System.Text.Encodings.Web;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace STranslateDLL;
|
|
|
|
public static class Utilities
|
|
{
|
|
public const string Url = "https://www2.deepl.com/jsonrpc";
|
|
private static long _nextId;
|
|
private static bool _hasInit;
|
|
|
|
public static readonly Dictionary<bool, string> TextTypeDic = new()
|
|
{
|
|
{ true, "richtext" },
|
|
{ false, "plaintext" }
|
|
};
|
|
|
|
private static readonly JsonSerializerOptions GetOptions =
|
|
new()
|
|
{
|
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
|
|
WriteIndented = true
|
|
};
|
|
|
|
public static string Serialize(object data)
|
|
{
|
|
return JsonSerializer.Serialize(data, GetOptions);
|
|
}
|
|
|
|
public static void Initial()
|
|
{
|
|
if (_hasInit) return;
|
|
|
|
var rand = new Random();
|
|
var number = rand.NextInt64(99999) + 8300000;
|
|
_nextId = number * 1000;
|
|
|
|
_hasInit = true;
|
|
}
|
|
|
|
public static bool IsRichText(string text)
|
|
{
|
|
return text.Contains('<') && text.Contains('>');
|
|
}
|
|
|
|
public static long GenerateTimestamp(string texts)
|
|
{
|
|
long iCount = texts.Split('i').Length - 1;
|
|
var ts = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
return iCount != 0 ? ts - ts % (iCount + 1) + iCount + 1 : ts;
|
|
}
|
|
|
|
public static long CreateId()
|
|
{
|
|
return Interlocked.Increment(ref _nextId);
|
|
}
|
|
|
|
public 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);
|
|
}
|
|
} |