From 795ecce45ee44fcdc94cd895651d6e46de0a3577 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 5 Jun 2021 08:15:19 -0600 Subject: [PATCH] fix bing trranslate --- GUI/host/textthread.h | 2 +- extensions/bingtranslate.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index a58c292..98a9e98 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -9,7 +9,7 @@ public: inline static OutputCallback Output; inline static bool filterRepetition = false; - inline static int flushDelay = 400; // flush every 400ms by default + inline static int flushDelay = 500; // flush every 500ms by default inline static int maxBufferSize = 1000; inline static int maxHistorySize = 10'000'000; diff --git a/extensions/bingtranslate.cpp b/extensions/bingtranslate.cpp index 4c9eb79..f6a3a4d 100644 --- a/extensions/bingtranslate.cpp +++ b/extensions/bingtranslate.cpp @@ -101,11 +101,16 @@ std::pair Translate(const std::wstring& text) else return { false, FormatString(L"%s (code=%u)", TRANSLATION_ERROR, httpRequest.errorCode) }; } + static Synchronized token; + if (token->empty()) if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"www.bing.com", L"GET", L"translator" }) + if (auto tokenPos = httpRequest.response.find(L"[" + std::to_wstring(time(nullptr) / 10)); tokenPos != std::string::npos) + token->assign(FormatString(L"&key=%s&token=%s", httpRequest.response.substr(tokenPos + 1, 13), httpRequest.response.substr(tokenPos + 16, 32))); + if (token->empty()) return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, L"token missing") }; if (HttpRequest httpRequest{ L"Mozilla/5.0 Textractor", L"www.bing.com", L"POST", - FormatString(L"/ttranslatev3?fromLang=%s&to=%s&text=%s", translateFrom.Copy(), translateTo.Copy(), Escape(text)).c_str() + FormatString(L"/ttranslatev3?fromLang=%s&to=%s&text=%s%s", translateFrom.Copy(), translateTo.Copy(), Escape(text), token.Copy()).c_str() }) if (auto translation = Copy(JSON::Parse(httpRequest.response)[0][L"translations"][0][L"text"].String())) return { true, translation.value() }; else return { false, FormatString(L"%s: %s", TRANSLATION_ERROR, httpRequest.response) };