small fixes

This commit is contained in:
Akash Mozumdar 2019-09-10 18:53:55 -04:00
parent 5d77350e3c
commit 0c9fc5c08a
2 changed files with 8 additions and 9 deletions

View File

@ -73,14 +73,16 @@ QStringList languages
"Zulu: zu"
};
std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
unsigned TKK = 0;
std::wstring GetTranslationUri(const std::wstring& text)
{
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access
if (!TKK) return FormatString(L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=t&tl=%s&q=%s", translateTo->c_str(), text);
// Artikash 8/19/2018: reverse engineered from translate.google.com
std::wstring escapedText;
unsigned a = _time64(NULL) / 3600, b = a; // <- the first part of TKK
unsigned a = time(NULL) / 3600, b = a; // the first part of TKK
for (unsigned char ch : WideStringToString(text))
{
escapedText += FormatString(L"%%%02X", (int)ch);
@ -93,9 +95,8 @@ std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
a += a << 15;
a ^= TKK;
a %= 1000000;
b ^= a;
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, b, escapedText);
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);
}
bool IsHash(const std::wstring& result)
@ -105,13 +106,12 @@ bool IsHash(const std::wstring& result)
std::pair<bool, std::wstring> Translate(const std::wstring& text)
{
static unsigned TKK = 0;
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);
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.google.com", L"GET", GetTranslationUri(text, TKK).c_str() })
if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"translate.googleapis.com", L"GET", GetTranslationUri(text).c_str() })
{
// Response formatted as JSON: starts with "[[[" and translation is enclosed in quotes followed by a comma
if (httpRequest.response[0] == L'[')

View File

@ -26,9 +26,6 @@ constexpr bool x64 = true;
constexpr bool x64 = false;
#endif
#define MESSAGE(text) MessageBoxW(NULL, text, L"Textractor", MB_OK)
#define CRITIAL_SECTION static std::mutex m; std::scoped_lock l(m)
template <typename T> using Array = T[];
template <auto F> using Functor = std::integral_constant<std::decay_t<decltype(F)>, F>;
@ -111,6 +108,8 @@ inline std::wstring FormatString(const wchar_t* format, const Args&... args)
}
#pragma warning(pop)
#define MESSAGE(text) MessageBoxW(NULL, FormatArg(text), L"Textractor", MB_OK)
#ifdef _DEBUG
#define TEST(...) static auto _ = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr);
#else