mirror of
https://github.com/Syping/dtranslatebot.git
synced 2025-01-06 19:19:44 +01:00
add mozhi support
This commit is contained in:
parent
691e46a507
commit
1dc2b2dffa
4 changed files with 196 additions and 2 deletions
|
@ -34,6 +34,7 @@ set(DTRANSLATEBOT_HEADERS
|
|||
src/core/webhook_push.h
|
||||
src/database/file/file.h
|
||||
src/translator/deepl/deepl.h
|
||||
src/translator/mozhi/mozhi.h
|
||||
src/translator/libretranslate/libretranslate.h
|
||||
src/translator/lingvatranslate/lingvatranslate.h
|
||||
src/translator/stub/stub.h
|
||||
|
@ -49,6 +50,7 @@ set(DTRANSLATEBOT_SOURCES
|
|||
src/core/webhook_push.cpp
|
||||
src/database/file/file.cpp
|
||||
src/translator/deepl/deepl.cpp
|
||||
src/translator/mozhi/mozhi.cpp
|
||||
src/translator/libretranslate/libretranslate.cpp
|
||||
src/translator/lingvatranslate/lingvatranslate.cpp
|
||||
src/translator/stub/stub.cpp
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "settings.h"
|
||||
#include "../database/file/file.h"
|
||||
#include "../translator/deepl/deepl.h"
|
||||
#include "../translator/mozhi/mozhi.h"
|
||||
#include "../translator/libretranslate/libretranslate.h"
|
||||
#include "../translator/lingvatranslate/lingvatranslate.h"
|
||||
#include "../translator/stub/stub.h"
|
||||
|
@ -254,6 +255,46 @@ bool process_translator_settings(const dpp::json &json, std::shared_ptr<bot::tra
|
|||
|
||||
translator_instance = std::make_shared<bot::translator::deepl>(translator.hostname, translator.apiKey);
|
||||
}
|
||||
else if (translator.type == "mozhi") {
|
||||
auto json_mozhi_hostname = json.find("hostname");
|
||||
if (json_mozhi_hostname != json.end())
|
||||
translator.hostname = *json_mozhi_hostname;
|
||||
else
|
||||
translator.hostname = {};
|
||||
|
||||
auto json_mozhi_tls = json.find("tls");
|
||||
if (json_mozhi_tls != json.end())
|
||||
translator.tls = *json_mozhi_tls;
|
||||
else
|
||||
translator.tls = false;
|
||||
|
||||
auto json_mozhi_port = json.find("port");
|
||||
if (json_mozhi_port != json.end())
|
||||
translator.port = *json_mozhi_port;
|
||||
else
|
||||
translator.port = 0;
|
||||
|
||||
auto json_mozhi_url = json.find("url");
|
||||
if (json_mozhi_url == json.end()) {
|
||||
std::cerr << "[Error] Value url not found in translator object" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (translator.hostname.empty()) {
|
||||
process_url(*json_mozhi_url, translator);
|
||||
}
|
||||
else {
|
||||
translator.url = *json_mozhi_url;
|
||||
}
|
||||
|
||||
std::string mozhi_engine;
|
||||
auto json_mozhi_engine = json.find("engine");
|
||||
if (json_mozhi_engine != json.end())
|
||||
mozhi_engine = *json_mozhi_engine;
|
||||
else
|
||||
mozhi_engine = "google";
|
||||
|
||||
translator_instance = std::make_shared<bot::translator::mozhi>(translator.hostname, translator.port, translator.url, translator.tls, mozhi_engine);
|
||||
}
|
||||
else if (translator.type == "libretranslate") {
|
||||
auto json_lt_hostname = json.find("hostname");
|
||||
if (json_lt_hostname != json.end())
|
||||
|
@ -288,8 +329,6 @@ bool process_translator_settings(const dpp::json &json, std::shared_ptr<bot::tra
|
|||
auto json_lt_apiKey = json.find("apiKey");
|
||||
if (json_lt_apiKey != json.end())
|
||||
translator.apiKey = *json_lt_apiKey;
|
||||
else
|
||||
translator.apiKey.clear();
|
||||
|
||||
translator_instance = std::make_shared<bot::translator::libretranslate>(translator.hostname, translator.port, translator.url, translator.tls, translator.apiKey);
|
||||
}
|
||||
|
|
107
src/translator/mozhi/mozhi.cpp
Normal file
107
src/translator/mozhi/mozhi.cpp
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*****************************************************************************
|
||||
* 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 <dpp/json.h>
|
||||
#include <dpp/httpsclient.h>
|
||||
#include <dpp/utility.h>
|
||||
#include "mozhi.h"
|
||||
using namespace bot::translator;
|
||||
using namespace std::chrono_literals;
|
||||
using namespace std::string_literals;
|
||||
|
||||
mozhi::mozhi(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string &engine) :
|
||||
m_hostname(hostname), m_port(port), m_url(url), m_tls(tls), m_engine(engine)
|
||||
{
|
||||
}
|
||||
|
||||
mozhi::~mozhi()
|
||||
{
|
||||
}
|
||||
|
||||
const std::vector<language> mozhi::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 {
|
||||
const std::string parameters = dpp::utility::make_url_parameters({
|
||||
{"engine"s, m_engine}
|
||||
});
|
||||
|
||||
dpp::https_client http_request(m_hostname, m_port, m_url + "api/target_languages" + parameters, "GET", {}, {}, !m_tls);
|
||||
if (http_request.get_status() == 200) {
|
||||
const dpp::json response = dpp::json::parse(http_request.get_content());
|
||||
if (response.is_array()) {
|
||||
m_languages.languages.clear();
|
||||
for (auto json_language = response.begin(); json_language != response.end(); json_language++) {
|
||||
if (json_language->is_object()) {
|
||||
language language;
|
||||
|
||||
auto json_lang_code = json_language->find("Id");
|
||||
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 mozhi::translate(const std::string &text, const std::string &source, const std::string &target)
|
||||
{
|
||||
try {
|
||||
const std::string parameters = dpp::utility::make_url_parameters({
|
||||
{"engine"s, m_engine},
|
||||
{"from"s, source},
|
||||
{"to"s, target},
|
||||
{"text"s, text},
|
||||
});
|
||||
|
||||
dpp::https_client http_request(m_hostname, m_port, m_url + "api/translate" + parameters, "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("translated-text");
|
||||
if (tr_text != response.end())
|
||||
return *tr_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception &exception) {
|
||||
std::cerr << "[Exception] " << exception.what() << std::endl;
|
||||
}
|
||||
|
||||
return text;
|
||||
}
|
46
src/translator/mozhi/mozhi.h
Normal file
46
src/translator/mozhi/mozhi.h
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*****************************************************************************
|
||||
* 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_MOZHI_H
|
||||
#define TRANSLATOR_MOZHI_H
|
||||
|
||||
#include <cstdint>
|
||||
#include "../../core/translator.h"
|
||||
|
||||
namespace bot {
|
||||
namespace translator {
|
||||
class mozhi : public translator {
|
||||
|
||||
public:
|
||||
explicit mozhi(const std::string &hostname, uint16_t port, const std::string &url, bool tls, const std::string &engine);
|
||||
~mozhi() override;
|
||||
const std::vector<language> get_languages() override;
|
||||
const std::string translate(const std::string &text, const std::string &source, const std::string &target) override;
|
||||
|
||||
private:
|
||||
std::string m_engine;
|
||||
std::string m_hostname;
|
||||
supported_languages m_languages;
|
||||
uint16_t m_port;
|
||||
std::string m_url;
|
||||
bool m_tls;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TRANSLATOR_MOZHI_H
|
Loading…
Reference in a new issue