Dump JSON to stdout.

This commit is contained in:
tastytea 2019-04-17 01:00:33 +02:00
parent 9a4d40a79d
commit d4fa51bcd9
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 64 additions and 1 deletions

View File

@ -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\"")

View File

@ -15,10 +15,59 @@
*/
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <cstdint>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#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<curlopts::Url>(url);
request.setOpt<curlopts::UserAgent>(string("gitea2rss/")
+ global::version);
request.setOpt<curlopts::HttpHeader>({ "Connection: close" });
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlopts::WriteStream>(&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;
}