2025-01-06 13:31:00 +08:00
|
|
|
#include "handle.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <memory>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <xlnt/xlnt.hpp>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
// #include <filesystem>
|
|
|
|
using json = nlohmann::json;
|
|
|
|
// namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
struct TResult {
|
|
|
|
std::string source;
|
|
|
|
std::string ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool parse_result(const std::string& str, std::string& out)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
json j = json::parse(str);
|
|
|
|
auto trans_result = j["trans_result"];
|
|
|
|
for (const auto& result : trans_result) {
|
|
|
|
out = result["dst"];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
std::cerr << "Error parsing JSON: " << e.what() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2025-01-07 16:42:43 +08:00
|
|
|
|
|
|
|
#if _WIN32
|
|
|
|
system("chcp 65001");
|
|
|
|
#endif
|
|
|
|
|
2025-01-06 13:31:00 +08:00
|
|
|
if (argc < 2) {
|
|
|
|
std::cout << "Second parm is file name(find in current director)." << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_name(argv[1]);
|
|
|
|
std::cout << "Will Parse:" << file_name << std::endl;
|
|
|
|
ConfigInfo base_info;
|
|
|
|
auto config = std::make_shared<CConfig>();
|
|
|
|
if (!config->parse_config(base_info, "fanyi.ini")) {
|
|
|
|
std::cerr << "Parse fanyi.ini failed, Please Check!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::cout << "BaseURL:" << base_info.baseUrl << "\n";
|
|
|
|
std::cout << "AppID:" << base_info.appID << "\n";
|
|
|
|
std::cout << "SecretID:" << base_info.secretID << std::endl;
|
|
|
|
|
|
|
|
auto https = std::make_shared<CHttpsHandle>();
|
|
|
|
auto trans_tool = std::make_shared<CTransTool>();
|
|
|
|
trans_tool->set_id(base_info.appID, base_info.secretID);
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (response.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return parse_result(response, out);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 读取文件
|
|
|
|
std::ifstream in(file_name);
|
|
|
|
if (!in.is_open()) {
|
|
|
|
std::cerr << "File Open Failed:" << file_name << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::vector<TResult> vec;
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(in, line)) {
|
|
|
|
if (line.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string ret;
|
|
|
|
std::cout << "翻译:" << line << std::endl;
|
|
|
|
if (!trans(line, ret)) {
|
|
|
|
vec.emplace_back(TResult{line, "Failed"});
|
|
|
|
} else {
|
|
|
|
vec.emplace_back(TResult{line, ret});
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
if (vec.empty()) {
|
|
|
|
std::cout << "vec is empty." << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
xlnt::workbook wb;
|
|
|
|
xlnt::worksheet ws = wb.active_sheet();
|
|
|
|
int row = 1;
|
|
|
|
for (const auto& item : vec) {
|
|
|
|
ws.cell(1, row).value(item.source);
|
|
|
|
ws.cell(2, row).value(item.ret);
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
wb.save("out.xlsx");
|
|
|
|
return 0;
|
|
|
|
}
|