From e26d20a8692c83c8f634ba80bb1449975e0cee41 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Sun, 31 Aug 2025 02:29:42 +0900 Subject: [PATCH] =?UTF-8?q?=EC=8A=A4=ED=83=80=EC=9D=BC=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=EC=BB=B4=ED=8C=8C=EC=9D=BC=20=EC=98=A4?= =?UTF-8?q?=EB=A5=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/utils/console.h | 19 ++++---- include/utils/update_checker.h | 2 +- src/main.cc | 82 +++++++++++++++++----------------- src/utils/console.cc | 30 +++++++------ src/utils/file_downloader.cc | 3 +- src/utils/update_checker.cc | 50 +++++++++++---------- 6 files changed, 97 insertions(+), 89 deletions(-) diff --git a/include/utils/console.h b/include/utils/console.h index c0ad6d7..da52528 100644 --- a/include/utils/console.h +++ b/include/utils/console.h @@ -7,32 +7,35 @@ namespace utils { // @brief 명령어가 실행 가능한지 체크합니다 // @param cmd 실행할 명령 // @return int error_code -int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector& args = std::vector()); +int ValidateCommand( + boost::asio::io_context& ctx, std::string cmd, + const std::vector& args = std::vector()); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @return int error_code -int ExecuteCommand(boost::asio::io_context& ctx, - const std::string& cmd, +int ExecuteCommand( + boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args = std::vector()); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param result 실행결과 // @return int error_code -int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result); +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, + std::string& result); // @brief 명령어를 쉘에서 실행하고 결과를 EOF 전까지 읽어 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @param result 실행결과 // @return int error_code -int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args, - std::string& result); +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, + const std::vector& args, std::string& result); // @brief 명령어를 쉘에서 실행하고 결과를 파이프로 연결하여 반환합니다 // @param cmd 실행할 명령 // @param args 아규먼트 // @return boost::process::popen -boost::process::popen OpenPipe(boost::asio::io_context& ctx, - const std::string& cmd, +boost::process::popen OpenPipe( + boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args = std::vector()); } // namespace utils #endif \ No newline at end of file diff --git a/include/utils/update_checker.h b/include/utils/update_checker.h index 36b1019..0f31ed2 100644 --- a/include/utils/update_checker.h +++ b/include/utils/update_checker.h @@ -5,7 +5,7 @@ namespace utils { -int InstallYtdlp(); +int InstallYtdlp(boost::asio::io_context& ctx); int CheckUpdate(boost::asio::io_context& ctx); diff --git a/src/main.cc b/src/main.cc index ba26b75..7ad2377 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,54 +3,52 @@ #include "utils/update_checker.h" int main(int argc, char* argv[]) { - boost::asio::io_context ctx; - boost::system::error_code ec; - char buf[8192]; - std::string output; + boost::asio::io_context ctx; + boost::system::error_code ec; + char buf[8192]; + std::string output; - utils::CheckUpdate(ctx); + utils::CheckUpdate(ctx); #ifdef WIN32 - /*try { - auto ytdlp_pipe = utils::OpenPipe(ctx, - "yt-dlp.exe", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", - "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); + /*try { + auto ytdlp_pipe = utils::OpenPipe(ctx, + "yt-dlp.exe", { "-o", "-", "--quiet", "--ignore-errors", "-f", + "bestaudio", "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); - while (true) { - boost::system::error_code read_ec; - size_t bytes_read = - boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 8192), - read_ec); - if (bytes_read > 0) { - std::cout.write(buf, bytes_read); - } - if (read_ec == boost::asio::error::eof || read_ec) { - break; - } - } - } - catch (const boost::process::system_error& e) { - std::string error = e.what(); - return -1; - }*/ + while (true) { + boost::system::error_code read_ec; + size_t bytes_read = + boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, + 8192), read_ec); if (bytes_read > 0) { std::cout.write(buf, bytes_read); + } + if (read_ec == boost::asio::error::eof || read_ec) { + break; + } + } + } + catch (const boost::process::system_error& e) { + std::string error = e.what(); + return -1; + }*/ #else - auto ytdlp_pipe = utils::OpenPipe( - "yt-dlp", { "-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", - "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR" }); + auto ytdlp_pipe = utils::OpenPipe( + ctx, "yt-dlp", + {"-o", "-", "--quiet", "--ignore-errors", "-f", "bestaudio", + "https://youtu.be/9_bTl2vvYQg?si=IVhvpDhnpPvziwQR"}); - while (true) { - boost::system::error_code read_ec; - size_t bytes_read = - boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 16384), - read_ec); - if (bytes_read > 0) { - std::cout.write(buf, bytes_read); - } - if (read_ec == boost::asio::error::eof || read_ec) { - break; - } - } + while (true) { + boost::system::error_code read_ec; + size_t bytes_read = + boost::asio::read(ytdlp_pipe, boost::asio::buffer(buf, 8192), read_ec); + if (bytes_read > 0) { + std::cout.write(buf, bytes_read); + } + if (read_ec == boost::asio::error::eof || read_ec) { + break; + } + } #endif - return 0; + return 0; } \ No newline at end of file diff --git a/src/utils/console.cc b/src/utils/console.cc index 2b18f03..9c5c22b 100644 --- a/src/utils/console.cc +++ b/src/utils/console.cc @@ -4,7 +4,8 @@ namespace utils { -int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, const std::vector& args) { +int ValidateCommand(boost::asio::io_context& ctx, std::string cmd, + const std::vector& args) { try { ExecuteCommand(ctx, cmd, args); } catch (const boost::process::system_error& e) { @@ -19,30 +20,31 @@ int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, return ExecuteCommand(ctx, cmd, args, ignored); } -int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, std::string& result) { +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, + std::string& result) { std::vector ignored; return ExecuteCommand(ctx, cmd, ignored, result); } -int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, const std::vector& args, - std::string& result) { +int ExecuteCommand(boost::asio::io_context& ctx, const std::string& cmd, + const std::vector& args, std::string& result) { try { - boost::system::error_code ec; - boost::process::popen proc( - ctx, cmd, args, boost::process::process_stdio{ {}, nullptr, nullptr }); + boost::system::error_code ec; + boost::process::popen proc( + ctx, cmd, args, boost::process::process_stdio{{}, nullptr, nullptr}); - boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec); + boost::asio::read(proc, boost::asio::dynamic_buffer(result), ec); - proc.wait(); + proc.wait(); - return proc.exit_code(); - } - catch (const boost::process::system_error& e) { - return -1; + return proc.exit_code(); + } catch (const boost::process::system_error& e) { + return -1; } } -boost::process::popen OpenPipe(boost::asio::io_context& ctx, const std::string& cmd, +boost::process::popen OpenPipe(boost::asio::io_context& ctx, + const std::string& cmd, const std::vector& args) { return boost::process::popen( ctx, cmd, args, boost::process::process_stdio{nullptr, nullptr, nullptr}); diff --git a/src/utils/file_downloader.cc b/src/utils/file_downloader.cc index 976c770..bc6acc2 100644 --- a/src/utils/file_downloader.cc +++ b/src/utils/file_downloader.cc @@ -57,7 +57,8 @@ void DownloadFileFromHTTPS(std::string url, std::string filename) { boost::beast::flat_buffer buffer; // 응답 파서 만들기 - boost::beast::http::response_parser parser; + boost::beast::http::response_parser + parser; // body 제한 해제 (무제한) parser.body_limit((std::numeric_limits::max)()); diff --git a/src/utils/update_checker.cc b/src/utils/update_checker.cc index bde5f0c..a57cd59 100644 --- a/src/utils/update_checker.cc +++ b/src/utils/update_checker.cc @@ -6,7 +6,7 @@ namespace utils { -int InstallYtdlp() { +int InstallYtdlp(boost::asio::io_context& ctx) { BOOST_LOG_TRIVIAL(warning) << "ytdlp is unavailable. downloading ytdlp..."; #ifdef WIN32 DownloadFileFromHTTPS( @@ -16,21 +16,22 @@ int InstallYtdlp() { DownloadFileFromHTTPS( "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp", "yt-dlp"); - ExecuteCommand(boost::process::environment::find_executable("chmod").c_str(), + ExecuteCommand(ctx, + boost::process::environment::find_executable("chmod").string(), {"+x", "yt-dlp"}); #endif return 0; } static int FindNewLinePos(std::string& string, int start_pos) { - int newline_pos = start_pos; - while (newline_pos < string.size()) { - if (string[newline_pos] == '\n') { - return newline_pos; - } - newline_pos++; + int newline_pos = start_pos; + while (newline_pos < string.size()) { + if (string[newline_pos] == '\n') { + return newline_pos; } - return newline_pos; + newline_pos++; + } + return newline_pos; } int CheckUpdate(boost::asio::io_context& ctx) { @@ -39,27 +40,30 @@ int CheckUpdate(boost::asio::io_context& ctx) { int newline_pos = 0; #ifdef WIN32 - if (ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output) != 0) { - InstallYtdlp(); - ExecuteCommand(ctx, "yt-dlp.exe", { "--version", "--newline" }, output); + if (ExecuteCommand(ctx, "yt-dlp.exe", {"--version", "--newline"}, output) != + 0) { + InstallYtdlp(ctx); + ExecuteCommand(ctx, "yt-dlp.exe", {"--version", "--newline"}, output); } - BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); + BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " + << output.substr(0, output.size() - 1); output = ""; - ExecuteCommand(ctx, "yt-dlp.exe", { "-U", "--newline" }, output); + ExecuteCommand(ctx, "yt-dlp.exe", {"-U", "--newline"}, output); #else - if (ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output) != 0) { - InstallYtdlp(); - ExecuteCommand(ctx, "yt-dlp", { "--version", "--newline" }, output); + if (ExecuteCommand(ctx, "yt-dlp", {"--version", "--newline"}, output) != 0) { + InstallYtdlp(ctx); + ExecuteCommand(ctx, "yt-dlp", {"--version", "--newline"}, output); } - BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " << output.substr(0, output.size()-1); + BOOST_LOG_TRIVIAL(info) << "yt-dlp version: " + << output.substr(0, output.size() - 1); output = ""; - ExecuteCommand(ctx, "yt-dlp", { "-U", "--newline" }, output); + ExecuteCommand(ctx, "yt-dlp", {"-U", "--newline"}, output); #endif while (newline_pos < output.size()) { - old_newline_pos = newline_pos; - newline_pos = FindNewLinePos(output, newline_pos); - BOOST_LOG_TRIVIAL(info) << output.substr(old_newline_pos, newline_pos - 1); - newline_pos++; + old_newline_pos = newline_pos; + newline_pos = FindNewLinePos(output, newline_pos); + BOOST_LOG_TRIVIAL(info) << output.substr(old_newline_pos, newline_pos - 1); + newline_pos++; } return 0;