From d4fa51bcd951ea16e4b5e35b9a2b26d5696369af Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 17 Apr 2019 01:00:33 +0200 Subject: [PATCH] Dump JSON to stdout. --- CMakeLists.txt | 6 ++++- src/main.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d186017..416388f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ project(gitea2rss include(GNUInstallDirs) find_package(PkgConfig REQUIRED) +pkg_check_modules(CURLPP REQUIRED curlpp) pkg_check_modules(JSONCPP REQUIRED jsoncpp) set(CMAKE_CXX_STANDARD 14) @@ -18,7 +19,10 @@ set(CMAKE_CXX_FLAGS_DEBUG include_directories(${PROJECT_BINARY_DIR}) +include_directories(${CURLPP_INCLUDE_DIRS}) include_directories(${JSONCPP_INCLUDE_DIRS}) + +link_directories(${CURLPP_LIBRARY_DIRS}) link_directories(${JSONCPP_LIBRARY_DIRS}) # Write version in header @@ -29,7 +33,7 @@ configure_file( file(GLOB sources src/*.cpp) add_executable(${PROJECT_NAME} "${sources}") -target_link_libraries(${PROJECT_NAME} ${JSONCPP_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${CURLPP_LIBRARIES} ${JSONCPP_LIBRARIES}) install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) set(WITH_MAN "YES" CACHE STRING "WITH_MAN defaults to \"YES\"") diff --git a/src/main.cpp b/src/main.cpp index c7b4077..428b200 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,10 +15,59 @@ */ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "version.hpp" using std::cout; using std::cerr; using std::endl; +using std::string; +using std::ostringstream; +using std::uint16_t; + +namespace curlopts = curlpp::options; + +const string get_http(const string &url) +{ + string answer; + + try + { + ostringstream oss; + curlpp::Easy request; + request.setOpt(url); + request.setOpt(string("gitea2rss/") + + global::version); + request.setOpt({ "Connection: close" }); + request.setOpt(true); + request.setOpt(&oss); + request.perform(); + uint16_t ret = curlpp::infos::ResponseCode::get(request); + if (ret == 200 || ret == 302 || ret == 307 + || ret == 301 || ret == 308) + { + answer = oss.str(); + } + else + { + cerr << "HTTP error: " << std::to_string(ret) << endl; + } + } + catch (const std::exception &e) + { + cerr << "Error: " << e.what() << endl; + } + + return answer; +} int main(int argc, char *argv[]) { @@ -27,5 +76,15 @@ int main(int argc, char *argv[]) cerr << "usage: " << argv[0] << " URL of Gitea project\n"; return 1; } + + curlpp::initialize(); + + string url = argv[1]; + size_t pos_repo = url.find('/', 8) + 1; + const string baseurl = url.substr(0, pos_repo - 1); + const string repo = url.substr(pos_repo); + + cout << get_http(baseurl + "/api/v1/repos/" + repo + "/releases"); + return 0; }