Compare commits
No commits in common. "main" and "v1.0.0" have entirely different histories.
22
.vscode/settings.json
vendored
22
.vscode/settings.json
vendored
@ -107,26 +107,6 @@
|
|||||||
"span": "cpp",
|
"span": "cpp",
|
||||||
"valarray": "cpp",
|
"valarray": "cpp",
|
||||||
"variant": "cpp",
|
"variant": "cpp",
|
||||||
"xstring": "cpp",
|
"xstring": "cpp"
|
||||||
"chrono": "cpp",
|
|
||||||
"filesystem": "cpp",
|
|
||||||
"ios": "cpp",
|
|
||||||
"locale": "cpp",
|
|
||||||
"xfacet": "cpp",
|
|
||||||
"xhash": "cpp",
|
|
||||||
"xiosbase": "cpp",
|
|
||||||
"xlocale": "cpp",
|
|
||||||
"xlocbuf": "cpp",
|
|
||||||
"xlocinfo": "cpp",
|
|
||||||
"xlocmes": "cpp",
|
|
||||||
"xlocmon": "cpp",
|
|
||||||
"xlocnum": "cpp",
|
|
||||||
"xloctime": "cpp",
|
|
||||||
"xmemory": "cpp",
|
|
||||||
"xmemory0": "cpp",
|
|
||||||
"xstddef": "cpp",
|
|
||||||
"xtr1common": "cpp",
|
|
||||||
"xtree": "cpp",
|
|
||||||
"xutility": "cpp"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,7 @@ windows下需要特殊处理编码问题(API要求为UTF-8)。
|
|||||||
# 输入格式
|
# 输入格式
|
||||||
|
|
||||||
- 在程序所在位置下,读取`fanyi.ini`配置文件(请参考模板),设置百度翻译`API`相关信息。
|
- 在程序所在位置下,读取`fanyi.ini`配置文件(请参考模板),设置百度翻译`API`相关信息。
|
||||||
- 命令行第二个参数,读取一个txt文件(需要UTF-8编码),每一行是一句要翻译的内容。
|
- 命令行第二个参数,读取一个txt文件,每一行是一句要翻译的内容。
|
||||||
|
|
||||||
# 依赖
|
# 依赖
|
||||||
|
|
||||||
@ -19,4 +19,4 @@ windows下需要特殊处理编码问题(API要求为UTF-8)。
|
|||||||
|
|
||||||
对于`Debian`系列系统,示例安装`sudo apt install libssl-dev libcurl4-openssl-dev`。
|
对于`Debian`系列系统,示例安装`sudo apt install libssl-dev libcurl4-openssl-dev`。
|
||||||
|
|
||||||
对于`windows`系统,可以使用`vcpkg install xlnt openssl curl[ssl]`。
|
对用`windows`系统,可以使用`vcpkg install xlnt openssl curl[ssl]`。
|
@ -2,4 +2,3 @@
|
|||||||
BaseURL = https://fanyi-api.baidu.com/api/trans/vip/translate?
|
BaseURL = https://fanyi-api.baidu.com/api/trans/vip/translate?
|
||||||
AppID =
|
AppID =
|
||||||
SecretID =
|
SecretID =
|
||||||
Interval = 200
|
|
@ -4,7 +4,6 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <SimpleIni.h>
|
#include <SimpleIni.h>
|
||||||
#include <cctype>
|
|
||||||
|
|
||||||
CHttpsHandle::CHttpsHandle()
|
CHttpsHandle::CHttpsHandle()
|
||||||
{
|
{
|
||||||
@ -145,23 +144,15 @@ bool CConfig::parse_config(ConfigInfo& config, const std::string& config_path)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
config.baseUrl = ini_handle.GetValue("Config", "BaseURL");
|
config.baseUrl = ini_handle.GetValue("Config", "BaseURL");
|
||||||
|
|
||||||
if (!ini_handle.KeyExists("Config", "AppID")) {
|
if (!ini_handle.KeyExists("Config", "AppID")) {
|
||||||
std::cerr << "Not Key Found Config/AppID in fanyi.ini" << std::endl;
|
std::cerr << "Not Key Found Config/AppID in fanyi.ini" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
config.appID = ini_handle.GetValue("Config", "AppID");
|
config.appID = ini_handle.GetValue("Config", "AppID");
|
||||||
|
|
||||||
if (!ini_handle.KeyExists("Config", "SecretID")) {
|
if (!ini_handle.KeyExists("Config", "SecretID")) {
|
||||||
std::cerr << "Not Key Found Config/SecretID in fanyi.ini" << std::endl;
|
std::cerr << "Not Key Found Config/SecretID in fanyi.ini" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
config.secretID = ini_handle.GetValue("Config", "SecretID");
|
config.secretID = ini_handle.GetValue("Config", "SecretID");
|
||||||
|
|
||||||
if (!ini_handle.KeyExists("Config", "Interval")) {
|
|
||||||
std::cerr << "Not Key Found Config/Interval in fanyi.ini" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
config.interval = ini_handle.GetValue("Config", "Interval");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
1
handle.h
1
handle.h
@ -47,7 +47,6 @@ struct ConfigInfo {
|
|||||||
std::string baseUrl;
|
std::string baseUrl;
|
||||||
std::string appID;
|
std::string appID;
|
||||||
std::string secretID;
|
std::string secretID;
|
||||||
std::string interval{"1000"};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class CConfig
|
class CConfig
|
||||||
|
22
main.cpp
22
main.cpp
@ -3,10 +3,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <thread>
|
|
||||||
#include <xlnt/xlnt.hpp>
|
#include <xlnt/xlnt.hpp>
|
||||||
#include <unordered_map>
|
#include <thread>
|
||||||
#include <cctype>
|
|
||||||
|
|
||||||
// #include <filesystem>
|
// #include <filesystem>
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
@ -56,16 +54,11 @@ int main(int argc, char* argv[])
|
|||||||
std::cout << "BaseURL:" << base_info.baseUrl << "\n";
|
std::cout << "BaseURL:" << base_info.baseUrl << "\n";
|
||||||
std::cout << "AppID:" << base_info.appID << "\n";
|
std::cout << "AppID:" << base_info.appID << "\n";
|
||||||
std::cout << "SecretID:" << base_info.secretID << std::endl;
|
std::cout << "SecretID:" << base_info.secretID << std::endl;
|
||||||
std::cout << "Interval:" << base_info.interval << std::endl;
|
|
||||||
|
|
||||||
int interval = std::stoi(base_info.interval);
|
|
||||||
interval = interval < 100 ? 100 : interval;
|
|
||||||
|
|
||||||
auto https = std::make_shared<CHttpsHandle>();
|
auto https = std::make_shared<CHttpsHandle>();
|
||||||
auto trans_tool = std::make_shared<CTransTool>();
|
auto trans_tool = std::make_shared<CTransTool>();
|
||||||
trans_tool->set_id(base_info.appID, base_info.secretID);
|
trans_tool->set_id(base_info.appID, base_info.secretID);
|
||||||
|
|
||||||
std::unordered_map<std::string, std::string> word_map;
|
|
||||||
auto trans = [&](const std::string& words, std::string& out) -> bool {
|
auto trans = [&](const std::string& words, std::string& out) -> bool {
|
||||||
std::string request_url = base_info.baseUrl + trans_tool->combine(words, "auto", "en");
|
std::string request_url = base_info.baseUrl + trans_tool->combine(words, "auto", "en");
|
||||||
std::string response = https->sendGetRequest(request_url);
|
std::string response = https->sendGetRequest(request_url);
|
||||||
@ -88,22 +81,13 @@ int main(int argc, char* argv[])
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::string ret;
|
std::string ret;
|
||||||
|
std::cout << "翻译:" << line << std::endl;
|
||||||
if (word_map.count(line)) {
|
|
||||||
ret = word_map[line];
|
|
||||||
vec.emplace_back(TResult{line, ret});
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!trans(line, ret)) {
|
if (!trans(line, ret)) {
|
||||||
vec.emplace_back(TResult{line, "Failed"});
|
vec.emplace_back(TResult{line, "Failed"});
|
||||||
word_map[line] = "Failed";
|
|
||||||
} else {
|
} else {
|
||||||
vec.emplace_back(TResult{line, ret});
|
vec.emplace_back(TResult{line, ret});
|
||||||
word_map[line] = ret;
|
|
||||||
}
|
}
|
||||||
std::cout << "翻译:" << line << ", 结果:" << ret << std::endl;
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user