generated from Fortunevale/ProjectMakoto.Plugins.Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleTranslateClient.cs
143 lines (111 loc) · 5.08 KB
/
GoogleTranslateClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Project Makoto
// Copyright (C) 2023 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using System.Net;
using Newtonsoft.Json;
using ProjectMakoto.Util;
namespace ProjectMakoto.Plugins.Translation;
public sealed class GoogleTranslateClient : RequiresParent<TranslationPlugin>
{
internal GoogleTranslateClient(TranslationPlugin plugin) : base(plugin.Bot, plugin)
{
this.QueueHandler();
}
~GoogleTranslateClient()
{
this._disposed = true;
}
bool _disposed = false;
internal DateTime LastRequest = DateTime.MinValue;
internal readonly Dictionary<string, WebRequestItem> Queue = [];
private void QueueHandler()
{
_ = Task.Run(async () =>
{
HttpClient client = new();
client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36");
while (!this._disposed)
{
if (this.Queue.Count == 0 || !this.Queue.Any(x => !x.Value.Resolved && !x.Value.Failed))
{
await Task.Delay(100);
continue;
}
var b = this.Queue.First(x => !x.Value.Resolved && !x.Value.Failed);
try
{
var response = await client.PostAsync(b.Value.Url, null);
this.Queue[b.Key].StatusCode = response.StatusCode;
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.NotFound)
throw new Exceptions.NotFoundException("");
if (response.StatusCode == HttpStatusCode.InternalServerError)
throw new Exceptions.InternalServerErrorException("");
if (response.StatusCode == HttpStatusCode.Forbidden)
throw new Exceptions.ForbiddenException("");
throw new Exception($"Unsuccessful request: {response.StatusCode}");
}
this.Queue[b.Key].Response = await response.Content.ReadAsStringAsync();
this.Queue[b.Key].Resolved = true;
}
catch (Exception ex)
{
this.Queue[b.Key].Failed = true;
this.Queue[b.Key].Exception = ex;
}
finally
{
this.LastRequest = DateTime.UtcNow;
await Task.Delay(10000);
}
}
}).Add(this.Parent.Bot);
}
private async Task<string> MakeRequest(string url)
{
var key = Guid.NewGuid().ToString();
this.Queue.Add(key, new WebRequestItem { Url = url });
while (this.Queue.ContainsKey(key) && !this.Queue[key].Resolved && !this.Queue[key].Failed)
await Task.Delay(100);
if (!this.Queue.TryGetValue(key, out var value))
throw new Exception("The request has been removed from the queue prematurely.");
var response = value;
_ = this.Queue.Remove(key);
if (response.Resolved)
return response.Response;
if (response.Failed)
throw response.Exception;
throw new Exception("This exception should be impossible to get.");
}
public async Task<Tuple<string, string>> Translate(string SourceLanguage, string TargetLanguage, string Query)
{
string query;
using (var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "sl", SourceLanguage },
{ "tl", TargetLanguage },
{ "q", Query },
}))
{
query = await content.ReadAsStringAsync();
}
var translateResponse = await this.MakeRequest($"https://translate.google.com/translate_a/single?client=gtx&{query}&dt=t&ie=UTF-8&oe=UTF-8");
var parsedResponse = JsonConvert.DeserializeObject<object[]>(translateResponse);
var parsedTextStep1 = JsonConvert.DeserializeObject<object[]>(parsedResponse![0].ToString()!);
var translatedText = string.Join(" ", parsedTextStep1!.Select(x => JsonConvert.DeserializeObject<object[]>(x.ToString()!)![0].ToString()));
var translationSource = "";
if (SourceLanguage == "auto")
{
var parsedLanguageStep1 = JsonConvert.DeserializeObject<object[]>(parsedResponse[8].ToString()!);
var parsedLanguageStep2 = JsonConvert.DeserializeObject<object[]>(parsedLanguageStep1![0].ToString()!);
translationSource = parsedLanguageStep2![0].ToString();
}
return new Tuple<string, string>(translatedText, translationSource!);
}
}