try both deepl api versions

This commit is contained in:
Akash Mozumdar 2020-08-12 01:42:24 -06:00
parent 9c006bce17
commit 6da63208c7
2 changed files with 14 additions and 10 deletions

View File

@ -28,8 +28,8 @@ QStringList languages
bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = true; bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = true;
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 500; int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 500;
const wchar_t* accept[] = { L"*/*", nullptr }; enum KeyType { CAT, REST };
Synchronized<std::wstring> LMTBID; int keyType = CAT;
std::pair<bool, std::wstring> Translate(const std::wstring& text) std::pair<bool, std::wstring> Translate(const std::wstring& text)
{ {
@ -38,10 +38,17 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
L"Mozilla/5.0 Textractor", L"Mozilla/5.0 Textractor",
L"api.deepl.com", L"api.deepl.com",
L"POST", L"POST",
L"/v2/translate", keyType == CAT ? L"/v1/translate" : L"/v2/translate",
FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), apiKey.Copy(), translateTo.Copy()), FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), apiKey.Copy(), translateTo.Copy()),
L"Content-Type: application/x-www-form-urlencoded" L"Content-Type: application/x-www-form-urlencoded"
}) }; httpRequest && (!httpRequest.response.empty() || (httpRequest = HttpRequest{
L"Mozilla/5.0 Textractor",
L"api.deepl.com",
L"POST",
(keyType = !keyType) == CAT ? L"/v1/translate" : L"/v2/translate",
FormatString("text=%S&auth_key=%S&target_lang=%S", Escape(text), apiKey.Copy(), translateTo.Copy()),
L"Content-Type: application/x-www-form-urlencoded"
})))
// Response formatted as JSON: translation starts with text":" and ends with "}] // Response formatted as JSON: translation starts with text":" and ends with "}]
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+?)\"\\}\\]"))) return { true, results[1] }; if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"text\":\"(.+?)\"\\}\\]"))) return { true, results[1] };
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) }; else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
@ -75,7 +82,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
} }
)", ++id, r + (n - r % n), translateTo.Copy(), JSON::Escape(text)); )", ++id, r + (n - r % n), translateTo.Copy(), JSON::Escape(text));
// missing accept-encoding header since it fucks up HttpRequest // missing accept-encoding header since it fucks up HttpRequest
std::wstring headers = L"Host: www2.deepl.com\r\nAccept-Language: en-US,en;q=0.5\r\nContent-type: text/plain; charset=utf-8\r\nOrigin: https://www.deepl.com\r\nTE: Trailers" + LMTBID.Acquire().contents; std::wstring headers = L"Host: www2.deepl.com\r\nAccept-Language: en-US,en;q=0.5\r\nContent-type: text/plain; charset=utf-8\r\nOrigin: https://www.deepl.com\r\nTE: Trailers";
if (HttpRequest httpRequest{ if (HttpRequest httpRequest{
L"Mozilla/5.0 Textractor", L"Mozilla/5.0 Textractor",
L"www2.deepl.com", L"www2.deepl.com",
@ -84,14 +91,10 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
body, body,
headers.c_str(), headers.c_str(),
L"https://www.deepl.com/translator", L"https://www.deepl.com/translator",
WINHTTP_FLAG_SECURE, WINHTTP_FLAG_SECURE
NULL,
accept
}) })
{
// Response formatted as JSON: translation starts with preprocessed_sentence":" and ends with "," // Response formatted as JSON: translation starts with preprocessed_sentence":" and ends with ","
if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"postprocessed_sentence\":\"(.+?)\",\""))) return { true, results[1] }; if (std::wsmatch results; std::regex_search(httpRequest.response, results, std::wregex(L"postprocessed_sentence\":\"(.+?)\",\""))) return { true, results[1] };
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) }; else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
}
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) }; else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
} }

View File

@ -21,6 +21,7 @@ HttpRequest::HttpRequest(
if (WinHttpSendRequest(request, headers, -1UL, body.empty() ? NULL : body.data(), body.size(), body.size(), NULL)) if (WinHttpSendRequest(request, headers, -1UL, body.empty() ? NULL : body.data(), body.size(), body.size(), NULL))
{ {
WinHttpReceiveResponse(request, NULL); WinHttpReceiveResponse(request, NULL);
//DWORD size = 0; //DWORD size = 0;
//WinHttpQueryHeaders(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &size, WINHTTP_NO_HEADER_INDEX); //WinHttpQueryHeaders(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &size, WINHTTP_NO_HEADER_INDEX);
//this->headers.resize(size); //this->headers.resize(size);