mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 08:54:12 +08:00
try both deepl api versions
This commit is contained in:
parent
9c006bce17
commit
6da63208c7
@ -28,8 +28,8 @@ QStringList languages
|
||||
bool translateSelectedOnly = true, rateLimitAll = true, rateLimitSelected = true, useCache = true;
|
||||
int tokenCount = 10, tokenRestoreDelay = 60000, maxSentenceSize = 500;
|
||||
|
||||
const wchar_t* accept[] = { L"*/*", nullptr };
|
||||
Synchronized<std::wstring> LMTBID;
|
||||
enum KeyType { CAT, REST };
|
||||
int keyType = CAT;
|
||||
|
||||
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"api.deepl.com",
|
||||
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()),
|
||||
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 "}]
|
||||
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) };
|
||||
@ -75,7 +82,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
||||
}
|
||||
)", ++id, r + (n - r % n), translateTo.Copy(), JSON::Escape(text));
|
||||
// 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{
|
||||
L"Mozilla/5.0 Textractor",
|
||||
L"www2.deepl.com",
|
||||
@ -84,14 +91,10 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text)
|
||||
body,
|
||||
headers.c_str(),
|
||||
L"https://www.deepl.com/translator",
|
||||
WINHTTP_FLAG_SECURE,
|
||||
NULL,
|
||||
accept
|
||||
WINHTTP_FLAG_SECURE
|
||||
})
|
||||
{
|
||||
// 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] };
|
||||
else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };
|
||||
}
|
||||
else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) };
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ HttpRequest::HttpRequest(
|
||||
if (WinHttpSendRequest(request, headers, -1UL, body.empty() ? NULL : body.data(), body.size(), body.size(), NULL))
|
||||
{
|
||||
WinHttpReceiveResponse(request, NULL);
|
||||
|
||||
//DWORD size = 0;
|
||||
//WinHttpQueryHeaders(request, WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &size, WINHTTP_NO_HEADER_INDEX);
|
||||
//this->headers.resize(size);
|
||||
|
Loading…
x
Reference in New Issue
Block a user