Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
5f7d6a1f80 | |||
a793406ab7 | |||
ffc92d6a1f | |||
c9e1f2c454 |
22
.vscode/settings.json
vendored
22
.vscode/settings.json
vendored
@ -107,6 +107,26 @@
|
||||
"span": "cpp",
|
||||
"valarray": "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`相关信息。
|
||||
- 命令行第二个参数,读取一个txt文件,每一行是一句要翻译的内容。
|
||||
- 命令行第二个参数,读取一个txt文件(需要UTF-8编码),每一行是一句要翻译的内容。
|
||||
|
||||
# 依赖
|
||||
|
||||
@ -19,4 +19,4 @@ windows下需要特殊处理编码问题(API要求为UTF-8)。
|
||||
|
||||
对于`Debian`系列系统,示例安装`sudo apt install libssl-dev libcurl4-openssl-dev`。
|
||||
|
||||
对用`windows`系统,可以使用`vcpkg install xlnt openssl curl[ssl]`。
|
||||
对于`windows`系统,可以使用`vcpkg install xlnt openssl curl[ssl]`。
|
@ -1,4 +1,5 @@
|
||||
[Config]
|
||||
BaseURL = https://fanyi-api.baidu.com/api/trans/vip/translate?
|
||||
AppID =
|
||||
SecretID =
|
||||
SecretID =
|
||||
Interval = 200
|
@ -4,6 +4,7 @@
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <SimpleIni.h>
|
||||
#include <cctype>
|
||||
|
||||
CHttpsHandle::CHttpsHandle()
|
||||
{
|
||||
@ -144,15 +145,23 @@ bool CConfig::parse_config(ConfigInfo& config, const std::string& config_path)
|
||||
return false;
|
||||
}
|
||||
config.baseUrl = ini_handle.GetValue("Config", "BaseURL");
|
||||
|
||||
if (!ini_handle.KeyExists("Config", "AppID")) {
|
||||
std::cerr << "Not Key Found Config/AppID in fanyi.ini" << std::endl;
|
||||
return false;
|
||||
}
|
||||
config.appID = ini_handle.GetValue("Config", "AppID");
|
||||
|
||||
if (!ini_handle.KeyExists("Config", "SecretID")) {
|
||||
std::cerr << "Not Key Found Config/SecretID in fanyi.ini" << std::endl;
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
1
handle.h
1
handle.h
@ -47,6 +47,7 @@ struct ConfigInfo {
|
||||
std::string baseUrl;
|
||||
std::string appID;
|
||||
std::string secretID;
|
||||
std::string interval{"1000"};
|
||||
};
|
||||
|
||||
class CConfig
|
||||
|
22
main.cpp
22
main.cpp
@ -3,8 +3,10 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
#include <thread>
|
||||
#include <xlnt/xlnt.hpp>
|
||||
#include <unordered_map>
|
||||
#include <cctype>
|
||||
|
||||
// #include <filesystem>
|
||||
using json = nlohmann::json;
|
||||
@ -54,11 +56,16 @@ int main(int argc, char* argv[])
|
||||
std::cout << "BaseURL:" << base_info.baseUrl << "\n";
|
||||
std::cout << "AppID:" << base_info.appID << "\n";
|
||||
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 trans_tool = std::make_shared<CTransTool>();
|
||||
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 {
|
||||
std::string request_url = base_info.baseUrl + trans_tool->combine(words, "auto", "en");
|
||||
std::string response = https->sendGetRequest(request_url);
|
||||
@ -81,13 +88,22 @@ int main(int argc, char* argv[])
|
||||
continue;
|
||||
}
|
||||
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)) {
|
||||
vec.emplace_back(TResult{line, "Failed"});
|
||||
word_map[line] = "Failed";
|
||||
} else {
|
||||
vec.emplace_back(TResult{line, ret});
|
||||
word_map[line] = ret;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
std::cout << "翻译:" << line << ", 结果:" << ret << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
||||
}
|
||||
in.close();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user