Random rand = new Random();
var input_list = project.Lists["Ваш текст"];
var output_list = project.Lists["Результат"];
var proxy_list = project.Lists["Прокси"];
string proxy = project.Variables["Proxy"].Value;
string proxy_kind = project.Variables["Proxy_Kind"].Value;
string source_language = project.Variables["Source_language"].Value;
string target_language = project.Variables["Target_language"].Value;
string regional_language = string.Empty;
int l_delay = Convert.ToInt32(project.Variables["Delay_L"].Value);
int h_delay = Convert.ToInt32(project.Variables["Delay_H"].Value);
int global_error_counter = Convert.ToInt32(project.Variables["Global_Error_Counter"].Value);
int error_counter = Convert.ToInt32(project.Variables["Error_Counter"].Value);
int num_beams = 1;
source_language = source_language.Substring(0,2);
target_language = target_language.Substring(0,2);
if (target_language.Substring(0,2) == "EN")
{
regional_language = "en-GB";
}
else if (target_language.Substring(0,2) == "PT")
{
regional_language = "pt-PT";
}
else if (target_language.Substring(0,2) == "US")
{
regional_language = "en-US";
target_language = "EN";
}
else if (target_language.Substring(0,2) == "BR")
{
regional_language = "pt-BR";
target_language = "PT";
}
project.SendInfoToLog($"Начали работу. Всего строк: {input_list.Count()}. Всего прокси: {proxy_list.Count()}.", true);
for (int i = 0; i < input_list.Count; i++)
{
if (string.IsNullOrWhiteSpace(input_list[i].Trim()))
{
project.SendInfoToLog($"Строка {i} пустая, пропускаем!", true);
output_list.Add("\r\n");
continue;
}
var sentence_parts = input_list[i].Split(new string[] { ".", "?", "!" }, StringSplitOptions.None);
project.SendInfoToLog($"В строкe {i} имеем {sentence_parts.Count()} предложений!", true);
bool success = false;
for (int j = 0; j < global_error_counter; j++)
{
if (success) break;
for (int k = 0; k < error_counter; k++)
{
if (success) break;
var secret_id = ((long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString();
var number = rand.Next(10000000, 99999999).ToString();
string tail = string.Empty;
string head = $"{{\"jsonrpc\":\"2.0\",\"method\": \"LMT_handle_jobs\",\"params\":{{\"jobs\":[";
if (string.IsNullOrWhiteSpace(regional_language))
{
tail = $"],\"lang\":{{\"preference\":{{\"weight\":{{}},\"default\":\"default\"}},\"source_lang_user_selected\":\"{source_language}\",\"target_lang\":\"{target_language}\"}},\"priority\":1,\"commonJobParams\":{{\"mode\":\"translate\",\"browserType\":1}},\"timestamp\":{secret_id}}},\"id\":{number}}}";
}
else
{
tail = $"],\"lang\":{{\"preference\":{{\"weight\":{{}},\"default\":\"default\"}},\"source_lang_user_selected\":\"{source_language}\",\"target_lang\":\"{target_language}\"}},\"priority\":1,\"commonJobParams\":{{\"regionalVariant\":\"{regional_language}\",\"mode\":\"translate\",\"browserType\":1,\"formality\":null}},\"timestamp\":{secret_id}}},\"id\":{number}}}";
}
string fullBody = string.Empty;
string rowsBefore = string.Empty;
for (int m = 0; m < sentence_parts.Count(); m++)
{
string sentence = sentence_parts[m].Trim();
if (string.IsNullOrWhiteSpace(sentence)) continue;
sentence += ".";
if (m == 0)
{
if (sentence_parts.Count() > 1)
{
string next_sentence = sentence_parts[m+1].Trim();
if (!string.IsNullOrWhiteSpace(next_sentence))
{
next_sentence += ".";
fullBody += $"{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[],\"raw_en_context_after\":[\"{next_sentence}\"],\"preferred_num_beams\":{num_beams}}}";
}
else
{
fullBody += $"{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[],\"raw_en_context_after\":[],\"preferred_num_beams\":{num_beams}}}";
}
}
else
{
fullBody += $"{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[],\"raw_en_context_after\":[],\"preferred_num_beams\":{num_beams}}}";
}
}
else if (m == sentence_parts.Count()-1)
{
fullBody += $",{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[{rowsBefore}],\"raw_en_context_after\":[],\"preferred_num_beams\":{num_beams}}}";
}
else
{
string next_sentence = sentence_parts[m+1].Trim();
if (!string.IsNullOrWhiteSpace(next_sentence))
{
next_sentence += ".";
fullBody += $",{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[{rowsBefore}],\"raw_en_context_after\":[\"{next_sentence}\"],\"preferred_num_beams\":{num_beams}}}";
}
else
{
fullBody += $",{{\"kind\":\"default\",\"sentences\":[{{\"text\":\"{sentence}\",\"id\":{m},\"prefix\":\"\"}}],\"raw_en_context_before\":[{rowsBefore}],\"raw_en_context_after\":[],\"preferred_num_beams\":{num_beams}}}";
}
}
if (string.IsNullOrWhiteSpace(rowsBefore)) rowsBefore += $"\"{sentence}\"";
else rowsBefore += $",\"{sentence}\"";
}
if (string.IsNullOrWhiteSpace(proxy))
for (int p = proxy_list.Count-1; p >= 0; p--)
{
lock (SyncObjects.ListSyncer)
{
project.SendInfoToLog("Меняем прокси", true);
proxy = proxy_list[p].Trim();
proxy_list.RemoveAt(p);
if (string.IsNullOrWhiteSpace(proxy)) continue;
}
string firstResponse = ZennoPoster.HTTP.Request(
InterfacesLibrary.Enums.Http.HttpMethod.GET,
"https://www.deepl.com/translator",
string.Empty,
string.Empty,
$"{proxy_kind}://{proxy}",
"UTF-8",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderAndBody,
5000,
string.Empty,
project.Profile.UserAgent,
true,
5,
new[] {string.Empty},
string.Empty,
true,
true,
project.Profile.CookieContainer
);
if (!string.IsNullOrWhiteSpace(firstResponse))
{
string cookie = Encoding.UTF8.GetString(project.Profile.CookieContainer.Export());
cookie = cookie.Replace(".deepl.com", "www2.deepl.com");
byte[] cookieBytes = System.Text.Encoding.UTF8.GetBytes(cookie);
project.Profile.CookieContainer.Import(cookieBytes);
break;
}
}
if (string.IsNullOrWhiteSpace(proxy))
{
project.SendErrorToLog("Закончились прокси! Несите новые!", true);
return "0";
}
string mainResponse = ZennoPoster.HTTP.Request(
InterfacesLibrary.Enums.Http.HttpMethod.POST,
"https://www2.deepl.com/jsonrpc?method=LMT_handle_jobs",
$"{head}{fullBody}{tail}",
"application/json",
$"{proxy_kind}://{proxy}",
"UTF-8",
ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.HeaderAndBody,
5000,
string.Empty,
project.Profile.UserAgent,
true,
5,
new[] {string.Empty},
string.Empty,
true,
true,
project.Profile.CookieContainer
);
if (!string.IsNullOrWhiteSpace(mainResponse) && !mainResponse.Contains("Too many requests"))
{
var regex = new Regex("(?<=\"text\":\")[\\w\\W]*?(?=\")");
var resultList = regex.Matches(mainResponse).Cast<Match>().ToList();
string resultRow = string.Empty;
for (int m = 0; m < resultList.Count; m++) resultRow += resultList[m].Value;
resultRow = System.Text.RegularExpressions.Regex.Unescape(resultRow);
success = true;
output_list.Add(resultRow);
project.SendInfoToLog($"Успешно перевели строку #{i}", true);
Thread.Sleep(rand.Next(l_delay, h_delay));
break;
}
else if (!string.IsNullOrWhiteSpace(mainResponse) && mainResponse.Contains("Invalid Request") && mainResponse.Contains("Invalid commonJobParams"))
{
project.SendErrorToLog("Алярм! Все сломалось!");
return "0";
}
else
{
proxy = string.Empty;
}
}
}
if (!success)
{
project.SendErrorToLog("Лимит по количеству неудач для проекта! Помянем!", true);
return "0";
}
}
project.SendInfoToLog("Работа выполнена! Ничегосебе! Ура!", true);
return "1";