google seems to be locking down their ap. imove to bing translate
This commit is contained in:
parent
104f9a7b8c
commit
5e6d67691c
@ -1,4 +1,4 @@
|
||||
cd Builds/x86-Release/Build;
|
||||
Compress-Archive -Force -Path "Textractor.exe","styles","platforms","Qt5Core.dll","Qt5Gui.dll","Qt5Widgets.dll","vnrhook.dll","256_Remove Repetition.dll","512_Copy to Clipboard.dll","1024_Google Translate.dll","2048_Extra Newlines.dll" -DestinationPath Textractor;
|
||||
Compress-Archive -Force -Path "Textractor.exe","styles","platforms","Qt5Core.dll","Qt5Gui.dll","Qt5Widgets.dll","vnrhook.dll","256_Remove Repetition.dll","512_Copy to Clipboard.dll","1024_Bing Translate.dll","2048_Extra Newlines.dll" -DestinationPath Textractor;
|
||||
cd ../../x64-Release/Build;
|
||||
Compress-Archive -Force -Path "Textractor.exe","styles","platforms","Qt5Core.dll","Qt5Gui.dll","Qt5Widgets.dll","vnrhook.dll","256_Remove Repetition.dll","512_Copy to Clipboard.dll","1024_Google Translate.dll","2048_Extra Newlines.dll" -DestinationPath Textractor;
|
||||
Compress-Archive -Force -Path "Textractor.exe","styles","platforms","Qt5Core.dll","Qt5Gui.dll","Qt5Widgets.dll","vnrhook.dll","256_Remove Repetition.dll","512_Copy to Clipboard.dll","1024_Bing Translate.dll","2048_Extra Newlines.dll" -DestinationPath Textractor;
|
||||
|
@ -1,8 +1,12 @@
|
||||
include(QtUtils)
|
||||
msvc_registry_search()
|
||||
find_qt5(Core Widgets)
|
||||
|
||||
cmake_policy(SET CMP0037 OLD)
|
||||
|
||||
add_library(256_Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp)
|
||||
add_library(512_Copy\ to\ Clipboard SHARED copyclipboard.cpp extensionimpl.cpp)
|
||||
add_library(1024_Google\ Translate SHARED googletranslate.cpp extensionimpl.cpp)
|
||||
add_library(1024_Bing\ Translate SHARED bingtranslate.cpp extensionimpl.cpp)
|
||||
add_library(2048_Extra\ Newlines SHARED extranewlines.cpp extensionimpl.cpp)
|
||||
|
||||
target_link_libraries(1024_Google\ Translate winhttp.lib)
|
||||
target_link_libraries(1024_Bing\ Translate winhttp.lib Qt5::Widgets)
|
||||
|
123
extensions/bingtranslate.cpp
Normal file
123
extensions/bingtranslate.cpp
Normal file
@ -0,0 +1,123 @@
|
||||
#include "extension.h"
|
||||
#include <winhttp.h>
|
||||
#include <regex>
|
||||
#include <QInputDialog>
|
||||
#include <QSettings>
|
||||
|
||||
QStringList languages
|
||||
{
|
||||
"English: en",
|
||||
"Japanese: ja",
|
||||
"Hebrew: he",
|
||||
"Arabic: ar",
|
||||
"Hindi: hi",
|
||||
"Romanian: ro",
|
||||
"Bosnian: bs-Latn",
|
||||
"Russian: ru",
|
||||
"Bulgarian: bg",
|
||||
"Hungarian: hu",
|
||||
"Serbian: sr-Cyrl",
|
||||
"Catalan: ca",
|
||||
"Indonesian: id",
|
||||
"Chinese(Simplified): zh-CHS",
|
||||
"Italian: it",
|
||||
"Slovak: sk",
|
||||
"Chinese(Traditional): zh-CHT",
|
||||
"Slovenian: sl",
|
||||
"Croatian: hr",
|
||||
"Klingon: tlh", // Wait what? Apparently Bing supports this???????
|
||||
"Spanish: es",
|
||||
"Czech: cs",
|
||||
"Swedish: sv",
|
||||
"Danish: da",
|
||||
"Korean: ko",
|
||||
"Thai: th",
|
||||
"Dutch: nl",
|
||||
"Latvian: lv",
|
||||
"Turkish: tr",
|
||||
"Lithuanian: lt",
|
||||
"Ukranian: uk",
|
||||
"Estonian: et",
|
||||
"Malay: ms",
|
||||
"Urdu: ur",
|
||||
"Finnish: fi",
|
||||
"Maltese: mt",
|
||||
"Vietnamese: vi",
|
||||
"French: fr",
|
||||
"Norwegian: no",
|
||||
"Welsh: cy",
|
||||
"German: de",
|
||||
"Persian: fa",
|
||||
"Greek: el",
|
||||
"Polish: pl",
|
||||
"Portuguese: pt"
|
||||
};
|
||||
QString translateFrom;
|
||||
QString translateTo;
|
||||
|
||||
QString GetLanguage(QString prompt)
|
||||
{
|
||||
bool ok;
|
||||
QString ret = QInputDialog::getItem(nullptr, prompt, prompt, languages, 0, false, &ok);
|
||||
if (!ok) ret = "English: en";
|
||||
return ret.split(" ")[1];
|
||||
}
|
||||
|
||||
std::wstring GetTranslationUri(std::wstring text)
|
||||
{
|
||||
return ("/ttranslate?from=" + translateFrom + "&to=" + translateTo + "&text=").toStdWString() + text;
|
||||
}
|
||||
|
||||
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
||||
{
|
||||
static bool languagesLoaded = false;
|
||||
if (!languagesLoaded)
|
||||
{
|
||||
languages.sort();
|
||||
QSettings translateSettings("Bing Translation.ini", QSettings::IniFormat);
|
||||
if (translateSettings.contains("Translate_From")) translateFrom = translateSettings.value("Translate_From").toString();
|
||||
else translateSettings.setValue("Translate_From", translateFrom = GetLanguage("What language should Bing translate from?"));
|
||||
if (translateSettings.contains("Translate_To")) translateTo = translateSettings.value("Translate_To").toString();
|
||||
else translateSettings.setValue("Translate_To", translateTo = GetLanguage("What language should Bing translate to?"));
|
||||
translateSettings.sync();
|
||||
languagesLoaded = true;
|
||||
}
|
||||
|
||||
static HINTERNET internet = NULL;
|
||||
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
||||
|
||||
std::wstring translation;
|
||||
|
||||
if (sentenceInfo["hook address"] == -1 || sentenceInfo["current select"] != 1) return false;
|
||||
|
||||
if (internet)
|
||||
{
|
||||
if (HINTERNET connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
||||
{
|
||||
if (HINTERNET request = WinHttpOpenRequest(connection, L"POST", (GetTranslationUri(sentence)).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
||||
{
|
||||
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
||||
{
|
||||
DWORD bytesRead;
|
||||
char buffer[10000] = {};
|
||||
WinHttpReceiveResponse(request, NULL);
|
||||
WinHttpReadData(request, buffer, 10000, &bytesRead);
|
||||
// Response formatted as JSON: starts with '{'
|
||||
if (buffer[0] == '{')
|
||||
{
|
||||
wchar_t wbuffer[10000] = {};
|
||||
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
|
||||
if (std::wcmatch results; std::regex_search(wbuffer, results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
|
||||
for (auto& c : translation) if (c == L'\\') c = 0x200b;
|
||||
}
|
||||
}
|
||||
WinHttpCloseHandle(request);
|
||||
}
|
||||
WinHttpCloseHandle(connection);
|
||||
}
|
||||
}
|
||||
|
||||
if (translation == L"") translation = L"Error while translating.";
|
||||
sentence += L"\r\n" + translation;
|
||||
return true;
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
#include "extension.h"
|
||||
#include <winhttp.h>
|
||||
#include <ctime>
|
||||
#include <regex>
|
||||
|
||||
std::wstring GetTranslationUri(const wchar_t* text, unsigned int TKK)
|
||||
{
|
||||
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access.
|
||||
if (!TKK) return std::wstring(L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=tq=") + text;
|
||||
|
||||
// Artikash 8/19/2018: reverse engineered from translate.google.com
|
||||
char* utf8text = new char[wcslen(text) * 4];
|
||||
WideCharToMultiByte(CP_UTF8, 0, text, -1, utf8text, wcslen(text) * 4, NULL, NULL);
|
||||
|
||||
unsigned int a = (unsigned int)(_time64(NULL) / 3600), b = a; // <- the first part of TKK
|
||||
for (int i = 0; utf8text[i];)
|
||||
{
|
||||
a += (unsigned char)utf8text[i++];
|
||||
a += a << 10;
|
||||
a ^= a >> 6;
|
||||
}
|
||||
a += a << 3;
|
||||
a ^= a >> 11;
|
||||
a += a << 15;
|
||||
a ^= TKK;
|
||||
a %= 1000000;
|
||||
b ^= a;
|
||||
|
||||
std::wstring encodedText;
|
||||
for (int i = 0; utf8text[i];)
|
||||
{
|
||||
wchar_t utf8char[3] = {};
|
||||
swprintf_s<3>(utf8char, L"%02X", (int)(unsigned char)utf8text[i++]);
|
||||
encodedText += L"%" + std::wstring(utf8char);
|
||||
}
|
||||
|
||||
delete[] utf8text;
|
||||
return std::wstring(L"/translate_a/single?client=t&dt=ld&dt=rm&dt=t&tk=") + std::to_wstring(a) + L"." + std::to_wstring(b) + L"&q=" + std::wstring(encodedText);
|
||||
}
|
||||
|
||||
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
||||
{
|
||||
static HINTERNET internet = NULL;
|
||||
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
||||
static unsigned int TKK = 0;
|
||||
|
||||
std::wstring translation;
|
||||
|
||||
if (sentenceInfo["hook address"] == -1) return false;
|
||||
|
||||
if (internet)
|
||||
{
|
||||
if (!TKK)
|
||||
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
||||
{
|
||||
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
|
||||
{
|
||||
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
||||
{
|
||||
DWORD bytesRead;
|
||||
char buffer[100000] = {}; // Google Translate page is ~64kb
|
||||
WinHttpReceiveResponse(request, NULL);
|
||||
WinHttpReadData(request, buffer, 100000, &bytesRead);
|
||||
if (strstr(buffer, "a\\x3d")) TKK = strtoll(strstr(buffer, "a\\x3d") + 5, nullptr, 10) + strtoll(strstr(buffer, "b\\x3d") + 5, nullptr, 10);
|
||||
else TKK = strtoll(strstr(buffer, "TKK") + 12, nullptr, 10);
|
||||
}
|
||||
WinHttpCloseHandle(request);
|
||||
}
|
||||
WinHttpCloseHandle(connection);
|
||||
}
|
||||
|
||||
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
||||
{
|
||||
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence.c_str(), TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
||||
{
|
||||
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
||||
{
|
||||
DWORD bytesRead;
|
||||
char buffer[10000] = {};
|
||||
WinHttpReceiveResponse(request, NULL);
|
||||
WinHttpReadData(request, buffer, 10000, &bytesRead);
|
||||
// Response formatted as JSON: starts with '[[["'
|
||||
if (buffer[0] == '[')
|
||||
{
|
||||
wchar_t wbuffer[10000] = {};
|
||||
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
|
||||
std::wstring response(wbuffer);
|
||||
std::wregex translationFinder(L"\\[\"(.*?)\",[n\"]");
|
||||
std::wsmatch results;
|
||||
while (std::regex_search(response, results, translationFinder))
|
||||
{
|
||||
translation += std::wstring(results[1]) + L" ";
|
||||
response = results.suffix().str();
|
||||
}
|
||||
for (auto& c : translation) if (c == L'\\') c = 0x200b;
|
||||
}
|
||||
}
|
||||
WinHttpCloseHandle(request);
|
||||
}
|
||||
WinHttpCloseHandle(connection);
|
||||
}
|
||||
}
|
||||
|
||||
if (translation == L"") translation = L"Error while translating.";
|
||||
sentence += L"\r\n" + translation;
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue
Block a user