Textractor_test/extensions/googletranslate.cpp

176 lines
5.2 KiB
C++
Raw Normal View History

2020-03-26 20:12:20 +08:00
#include "qtcommon.h"
#include "extension.h"
2019-02-11 10:46:39 +08:00
#include "network.h"
2018-11-04 11:26:53 +08:00
#include <ctime>
2019-02-28 00:33:17 +08:00
extern const wchar_t* TRANSLATION_ERROR;
2020-03-26 20:12:20 +08:00
extern const char* API_KEY;
2019-02-28 00:33:17 +08:00
2020-03-26 20:12:20 +08:00
extern QFormLayout* display;
extern QSettings settings;
extern Synchronized<std::wstring> translateTo;
const char* TRANSLATION_PROVIDER = "Google Translate";
2018-11-04 11:26:53 +08:00
QStringList languages
{
"Afrikaans: af",
"Arabic: ar",
2020-03-26 20:12:20 +08:00
"Albanian: sq",
2018-11-04 11:26:53 +08:00
"Belarusian: be",
"Bengali: bn",
"Bosnian: bs",
"Bulgarian: bg",
"Catalan: ca",
"Chinese (simplified): zh-CH",
"Chinese (traditional): zh-TW",
2018-11-04 11:26:53 +08:00
"Croatian: hr",
"Czech: cs",
"Danish: da",
"Dutch: nl",
"English: en",
2018-11-04 11:26:53 +08:00
"Esperanto: eo",
"Estonian: et",
"Filipino: tl",
"Finnish: fi",
"French: fr",
"Galician: gl",
"German: de",
"Greek: el",
"Hebrew: iw",
"Hindi: hi",
"Hungarian: hu",
"Icelandic: is",
"Indonesian: id",
"Irish: ga",
"Italian: it",
"Japanese: ja",
2020-03-26 20:12:20 +08:00
"Klingon: tlh",
2018-11-04 11:26:53 +08:00
"Korean: ko",
"Latin: la",
"Latvian: lv",
"Lithuanian: lt",
"Macedonian: mk",
"Malay: ms",
"Maltese: mt",
"Norwegian: no",
"Persian: fa",
"Polish: pl",
"Portuguese: pt",
"Romanian: ro",
"Russian: ru",
"Serbian: sr",
"Slovak: sk",
"Slovenian: sl",
"Somali: so",
"Spanish: es",
"Swahili: sw",
"Swedish: sv",
"Thai: th",
"Turkish: tr",
2020-03-26 20:12:20 +08:00
"Ukranian: uk",
2018-11-04 11:26:53 +08:00
"Urdu: ur",
"Vietnamese: vi",
"Welsh: cy",
"Yiddish: yi",
"Zulu: zu"
};
bool translateSelectedOnly = false, rateLimitAll = true, rateLimitSelected = false, useCache = true;
int tokenCount = 30, tokenRestoreDelay = 60000;
2020-03-26 20:12:20 +08:00
Synchronized<std::wstring> key;
2019-09-11 06:53:55 +08:00
unsigned TKK = 0;
2020-03-26 20:12:20 +08:00
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
auto keyInput = new QLineEdit(settings.value(API_KEY).toString());
key->assign(S(keyInput->text()));
QObject::connect(keyInput, &QLineEdit::textChanged, [](QString key) { settings.setValue(API_KEY, S(::key->assign(S(key)))); });
display->addRow(API_KEY, keyInput);
auto googleCloudInfo = new QLabel(
"<a href=\"https://codelabs.developers.google.com/codelabs/cloud-translation-intro\">https://codelabs.developers.google.com/codelabs/cloud-translation-intro</a>"
);
googleCloudInfo->setOpenExternalLinks(true);
display->addRow(googleCloudInfo);
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
return TRUE;
}
2019-09-11 06:53:55 +08:00
std::wstring GetTranslationUri(const std::wstring& text)
2018-11-04 11:26:53 +08:00
{
2018-11-08 18:51:20 +08:00
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access
2019-06-13 16:01:29 +08:00
if (!TKK) return FormatString(L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=t&tl=%s&q=%s", translateTo->c_str(), text);
2018-11-04 11:26:53 +08:00
// Artikash 8/19/2018: reverse engineered from translate.google.com
2019-02-11 10:46:39 +08:00
std::wstring escapedText;
2019-09-11 06:53:55 +08:00
unsigned a = time(NULL) / 3600, b = a; // the first part of TKK
2019-02-11 10:46:39 +08:00
for (unsigned char ch : WideStringToString(text))
2018-11-04 11:26:53 +08:00
{
2019-06-13 16:01:29 +08:00
escapedText += FormatString(L"%%%02X", (int)ch);
2019-02-11 10:46:39 +08:00
a += ch;
2018-11-04 11:26:53 +08:00
a += a << 10;
a ^= a >> 6;
}
a += a << 3;
a ^= a >> 11;
a += a << 15;
a ^= TKK;
a %= 1000000;
2019-09-11 06:53:55 +08:00
return FormatString(L"/translate_a/single?client=webapp&dt=ld&dt=rm&dt=t&sl=auto&tl=%s&tk=%u.%u&q=%s", translateTo->c_str(), a, a ^ b, escapedText);
2018-11-04 11:26:53 +08:00
}
2019-06-22 11:16:23 +08:00
bool IsHash(const std::wstring& result)
{
return result.size() == 32 && std::all_of(result.begin(), result.end(), [](char ch) { return (ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'z'); });
2019-06-22 11:16:23 +08:00
}
std::pair<bool, std::wstring> Translate(const std::wstring& text, SentenceInfo)
2018-11-04 11:26:53 +08:00
{
2020-03-26 20:12:20 +08:00
if (!key->empty())
{
if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor",
L"translation.googleapis.com",
L"GET",
FormatString(L"/language/translate/v2?format=text&q=%s&target=%s&key=%s", Escape(text), translateTo->c_str(), key->c_str()).c_str()
})
{
// Response formatted as JSON: starts with "translatedText": " and translation is enclosed in quotes followed by a comma
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\"translatedText\": \"(.+?)\","))) return { true, results[1] };
return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
}
2019-06-13 16:01:29 +08:00
if (!TKK)
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.google.com", L"GET", L"/" })
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"(\\d{7,})'")))
_InterlockedCompareExchange(&TKK, stoll(results[1]), 0);
2018-11-04 11:26:53 +08:00
2019-09-11 06:53:55 +08:00
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.googleapis.com", L"GET", GetTranslationUri(text).c_str() })
2018-11-04 11:26:53 +08:00
{
2019-06-13 16:01:29 +08:00
// Response formatted as JSON: starts with "[[[" and translation is enclosed in quotes followed by a comma
if (httpRequest.response[0] == L'[')
{
std::wstring translation;
for (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"\\[\"(.*?)\",[n\"]")); httpRequest.response = results.suffix())
2019-06-22 11:16:23 +08:00
if (!IsHash(results[1])) translation += std::wstring(results[1]) + L" ";
2019-06-13 16:01:29 +08:00
if (!translation.empty()) return { true, translation };
}
return { false, FormatString(L"%s (TKK=%u): %s", TRANSLATION_ERROR, _InterlockedExchange(&TKK, 0), httpRequest.response) };
2018-11-04 11:26:53 +08:00
}
2019-06-13 16:01:29 +08:00
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
2018-12-19 05:32:28 +08:00
}