google translate optimized

This commit is contained in:
Akash Mozumdar 2018-12-18 14:21:27 -05:00
parent 3c67f5a15d
commit 5523644ef9

View File

@ -154,23 +154,27 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
std::wstring translation; std::wstring translation;
if (internet) if (internet)
{ {
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) if (!TKK)
{ {
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE)) if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{ {
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL)) if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
{ {
DWORD bytesRead; if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
char buffer[200000] = {}; // Google Translate page is ~180kb (good god the bloat >_>) {
WinHttpReceiveResponse(request, NULL); DWORD bytesRead;
WinHttpReadData(request, buffer, 200000, &bytesRead); char buffer[200000] = {}; // Google Translate page is ~180kb (good god the bloat >_>)
if (std::cmatch results; std::regex_search(buffer, results, std::regex("(\\d{7,})'"))) TKK = stoll(results[1]); WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 200000, &bytesRead);
if (std::cmatch results; std::regex_search(buffer, results, std::regex("(\\d{7,})'"))) TKK = stoll(results[1]);
}
WinHttpCloseHandle(request);
} }
WinHttpCloseHandle(request); WinHttpCloseHandle(connection);
} }
WinHttpCloseHandle(connection);
} }
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{ {
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE)) if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
@ -191,6 +195,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
translation += std::wstring(results[1]) + L" "; translation += std::wstring(results[1]) + L" ";
for (auto& c : translation) if (c == L'\\') c = 0x200b; for (auto& c : translation) if (c == L'\\') c = 0x200b;
} }
else TKK = 0;
} }
WinHttpCloseHandle(request); WinHttpCloseHandle(request);
} }