mirror of
https://github.com/Syping/dtranslatebot.git
synced 2025-01-06 19:19:44 +01:00
add lingvatranslate support
This commit is contained in:
parent
a42932de12
commit
691e46a507
4 changed files with 180 additions and 0 deletions
|
@ -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
|
||||
)
|
||||
|
||||
|
|
|
@ -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<bot::tra
|
|||
|
||||
translator_instance = std::make_shared<bot::translator::libretranslate>(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<bot::translator::lingvatranslate>(translator.hostname, translator.port, translator.url, translator.tls);
|
||||
}
|
||||
else if (translator.type == "stub") {
|
||||
translator_instance = std::make_shared<bot::translator::stub>();
|
||||
}
|
||||
|
|
99
src/translator/lingvatranslate/lingvatranslate.cpp
Normal file
99
src/translator/lingvatranslate/lingvatranslate.cpp
Normal file
|
@ -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 <dpp/json.h>
|
||||
#include <dpp/httpsclient.h>
|
||||
#include <dpp/utility.h>
|
||||
#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<language> 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;
|
||||
}
|
45
src/translator/lingvatranslate/lingvatranslate.h
Normal file
45
src/translator/lingvatranslate/lingvatranslate.h
Normal file
|
@ -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 <cstdint>
|
||||
#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<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_hostname;
|
||||
supported_languages m_languages;
|
||||
uint16_t m_port;
|
||||
std::string m_url;
|
||||
bool m_tls;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TRANSLATOR_LINGVATRANSLATE_H
|
Loading…
Reference in a new issue