From 691e46a507c606534b660abdd72afd013233a3ec Mon Sep 17 00:00:00 2001 From: Syping Date: Sun, 29 Dec 2024 14:26:05 +0100 Subject: [PATCH] add lingvatranslate support --- CMakeLists.txt | 2 + src/core/settings.cpp | 34 +++++++ .../lingvatranslate/lingvatranslate.cpp | 99 +++++++++++++++++++ .../lingvatranslate/lingvatranslate.h | 45 +++++++++ 4 files changed, 180 insertions(+) create mode 100644 src/translator/lingvatranslate/lingvatranslate.cpp create mode 100644 src/translator/lingvatranslate/lingvatranslate.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c169ce..8b04c9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ set(DTRANSLATEBOT_HEADERS src/database/file/file.h src/translator/deepl/deepl.h src/translator/libretranslate/libretranslate.h + src/translator/lingvatranslate/lingvatranslate.h src/translator/stub/stub.h ) set(DTRANSLATEBOT_SOURCES @@ -49,6 +50,7 @@ set(DTRANSLATEBOT_SOURCES src/database/file/file.cpp src/translator/deepl/deepl.cpp src/translator/libretranslate/libretranslate.cpp + src/translator/lingvatranslate/lingvatranslate.cpp src/translator/stub/stub.cpp ) diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 8ba95ed..b2675a5 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -23,6 +23,7 @@ #include "../database/file/file.h" #include "../translator/deepl/deepl.h" #include "../translator/libretranslate/libretranslate.h" +#include "../translator/lingvatranslate/lingvatranslate.h" #include "../translator/stub/stub.h" using namespace bot::settings; @@ -292,6 +293,39 @@ bool process_translator_settings(const dpp::json &json, std::shared_ptr(translator.hostname, translator.port, translator.url, translator.tls, translator.apiKey); } + else if (translator.type == "lingvatranslate") { + auto json_lt_hostname = json.find("hostname"); + if (json_lt_hostname != json.end()) + translator.hostname = *json_lt_hostname; + else + translator.hostname = {}; + + auto json_lt_tls = json.find("tls"); + if (json_lt_tls != json.end()) + translator.tls = *json_lt_tls; + else + translator.tls = false; + + auto json_lt_port = json.find("port"); + if (json_lt_port != json.end()) + translator.port = *json_lt_port; + else + translator.port = 0; + + auto json_lt_url = json.find("url"); + if (json_lt_url == json.end()) { + std::cerr << "[Error] Value url not found in translator object" << std::endl; + return false; + } + if (translator.hostname.empty()) { + process_url(*json_lt_url, translator); + } + else { + translator.url = *json_lt_url; + } + + translator_instance = std::make_shared(translator.hostname, translator.port, translator.url, translator.tls); + } else if (translator.type == "stub") { translator_instance = std::make_shared(); } diff --git a/src/translator/lingvatranslate/lingvatranslate.cpp b/src/translator/lingvatranslate/lingvatranslate.cpp new file mode 100644 index 0000000..15c08d1 --- /dev/null +++ b/src/translator/lingvatranslate/lingvatranslate.cpp @@ -0,0 +1,99 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#include +#include +#include +#include "lingvatranslate.h" +using namespace bot::translator; +using namespace std::chrono_literals; +using namespace std::string_literals; + +lingvatranslate::lingvatranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls) : + m_hostname(hostname), m_port(port), m_url(url), m_tls(tls) +{ +} + +lingvatranslate::~lingvatranslate() +{ +} + +const std::vector lingvatranslate::get_languages() +{ + if (!m_languages.languages.empty()) { + auto current_time = std::chrono::system_clock::now(); + auto threshold_time = m_languages.query_time + 24h; + if (current_time <= threshold_time) + return m_languages.languages; + } + + try { + dpp::https_client http_request(m_hostname, m_port, m_url + "api/v1/languages/target", "GET", {}, {}, !m_tls); + if (http_request.get_status() == 200) { + const dpp::json response = dpp::json::parse(http_request.get_content()); + if (response.is_object()) { + auto languages = response.find("languages"); + if (languages != response.end()) { + m_languages.languages.clear(); + for (auto json_language = languages->begin(); json_language != languages->end(); json_language++) { + if (json_language->is_object()) { + language language; + + auto json_lang_code = json_language->find("code"); + if (json_lang_code != json_language->end()) + language.code = *json_lang_code; + + auto json_lang_name = json_language->find("name"); + if (json_lang_name != json_language->end()) + language.name = *json_lang_name; + + if (!language.code.empty() && !language.name.empty()) + m_languages.languages.push_back(std::move(language)); + } + } + m_languages.query_time = std::chrono::system_clock::now(); + } + } + } + } + catch (const std::exception &exception) { + std::cerr << "[Exception] " << exception.what() << std::endl; + } + + return m_languages.languages; +} + +const std::string lingvatranslate::translate(const std::string &text, const std::string &source, const std::string &target) +{ + try { + dpp::https_client http_request(m_hostname, m_port, m_url + "api/v1/" + source + "/" + target + "/" + dpp::utility::url_encode(text), "GET", {}, {}, !m_tls); + if (http_request.get_status() == 200) { + const dpp::json response = dpp::json::parse(http_request.get_content()); + if (response.is_object()) { + auto tr_text = response.find("translation"); + if (tr_text != response.end()) + return *tr_text; + } + } + } + catch (const std::exception &exception) { + std::cerr << "[Exception] " << exception.what() << std::endl; + } + + return text; +} diff --git a/src/translator/lingvatranslate/lingvatranslate.h b/src/translator/lingvatranslate/lingvatranslate.h new file mode 100644 index 0000000..37cf604 --- /dev/null +++ b/src/translator/lingvatranslate/lingvatranslate.h @@ -0,0 +1,45 @@ +/***************************************************************************** +* dtranslatebot Discord Translate Bot +* Copyright (C) 2024 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#ifndef TRANSLATOR_LINGVATRANSLATE_H +#define TRANSLATOR_LINGVATRANSLATE_H + +#include +#include "../../core/translator.h" + +namespace bot { + namespace translator { + class lingvatranslate : public translator { + + public: + explicit lingvatranslate(const std::string &hostname, uint16_t port, const std::string &url, bool tls); + ~lingvatranslate() override; + const std::vector get_languages() override; + const std::string translate(const std::string &text, const std::string &source, const std::string &target) override; + + private: + std::string m_hostname; + supported_languages m_languages; + uint16_t m_port; + std::string m_url; + bool m_tls; + }; + } +} + +#endif // TRANSLATOR_LINGVATRANSLATE_H