From ee8f8428180b237c54e7cf11e904268c1dc7dd9d Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 7 Jul 2020 08:20:43 +0200 Subject: [PATCH] Upgrade from mastodon-cpp to mastodonpp. Adds dependency on C++17. --- CMakeLists.txt | 9 +++++-- README.md | 20 ++++------------ src/soupbot.cpp | 62 ++++++++++++++++++++++++------------------------- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cdc27bd..230ca75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,12 @@ project (soupbot VERSION 0.3.2 LANGUAGES CXX ) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall") + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +find_package(mastodonpp REQUIRED CONFIG) # Write version in header configure_file ( @@ -15,7 +20,7 @@ configure_file ( include_directories(${PROJECT_BINARY_DIR}) add_executable(soupbot src/soupbot.cpp) -target_link_libraries(soupbot mastodon-cpp boost_system boost_filesystem ssl crypto) +target_link_libraries(soupbot mastodonpp boost_system boost_filesystem ssl crypto) install(TARGETS soupbot DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ingredients.json diff --git a/README.md b/README.md index c5a1409..0a67da7 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,10 @@ ## Dependencies * Tested OS: Linux - * C++ compiler (tested: gcc 8) - * [cmake](https://cmake.org/) (tested: 3.9) - * [boost](http://www.boost.org/) (tested: 1.65) - * [mastodon-cpp](https://schlomp.space/tastytea/mastodon-cpp) (at least: 0.105.0) + * C++ compiler (tested: gcc 9) + * [cmake](https://cmake.org/) (tested: 3.16) + * [boost](http://www.boost.org/) (tested: 1.72) + * [mastodonpp](https://schlomp.space/tastytea/mastodonpp) (at least: 0.5.5) ## Get sourcecode @@ -39,20 +39,10 @@ Copy `ingredients.json` to `~/.config/soupbot/ingredients.json`. You will need a pre-generated authentication code. -## Error codes - -Same as -[mastodon-cpp](https://schlomp.space/tastytea/mastodon-cpp/src/branch/master/README.md#error-codes), -plus: - -| Code | Explanation | -| --------: |:------------------------------| -| 1 | File not readable | - # Copyright ``` text -Copyright © 2018, 2019 tastytea . +Copyright © 2018, 2019, 2020 tastytea . License GPLv3: GNU GPL version 3 . This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. diff --git a/src/soupbot.cpp b/src/soupbot.cpp index 7350802..ba83959 100644 --- a/src/soupbot.cpp +++ b/src/soupbot.cpp @@ -1,5 +1,5 @@ /* This file is part of soupbot. - * Copyright © 2018, 2019 tastytea + * Copyright © 2018, 2019, 2020 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,23 +14,23 @@ * along with this program. If not, see . */ -#include -#include -#include +#include "version.hpp" +#include +#include +#include +#include +#include #include #include -#include -#include -#include +#include #include -#include -#include -#include -#include -#include "version.hpp" +#include +#include +#include +#include namespace pt = boost::property_tree; -using namespace Mastodon; +namespace masto = mastodonpp; using std::cout; using std::string; @@ -42,7 +42,8 @@ void read_config(pt::ptree &config, string &instance, string &access_token) bool config_changed = false; // Read config file, get access token - try { + try + { pt::read_json(filepath + "config.json", config); instance = config.get("instance", ""); access_token = config.get("access_token", ""); @@ -79,9 +80,9 @@ void populate_vector(const pt::ptree &ingredients, const string &node, std::vector &vector) { const pt::ptree childs = ingredients.get_child(node); - std::transform(childs.begin(), childs.end(), std::back_inserter(vector), - [](const pt::ptree::value_type &value) - { return value.second.data(); }); + std::transform( + childs.begin(), childs.end(), std::back_inserter(vector), + [](const pt::ptree::value_type &value) { return value.second.data(); }); } string get_ingredient(std::vector &vector) @@ -91,7 +92,8 @@ string get_ingredient(std::vector &vector) unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); std::default_random_engine generator(seed); - std::uniform_int_distribution distribution(0, vector.size() - 1); + std::uniform_int_distribution distribution(0, vector.size() + - 1); const std::uint16_t i = distribution(generator); const string ingredient = vector.at(i); @@ -120,7 +122,8 @@ int main() catch (std::exception &e) { // most likely file not found - std::cerr << "ERROR: " << filepath << "ingredients.json not found or not readable.\n"; + std::cerr << "ERROR: " << filepath + << "ingredients.json not found or not readable.\n"; return 1; } @@ -171,25 +174,22 @@ int main() toot += ", \nand plenty oil. Salt to taste. \n\nHappy cooking! 🍲 \n\n#bot"; - return_call ret; - API masto(instance, access_token); - masto.set_useragent("soupbot/" + (string)global::version); + masto::Instance server{instance, access_token}; + server.set_useragent("soupbot/" + (string)global::version); + masto::Connection connection{server}; - parameters params = - { - { "status", { toot } }, - { "visibility", { "public" } } - }; - ret = masto.post(API::v1::statuses, params); + masto::parametermap params = {{"status", {toot}}, + {"visibility", {"public"}}}; + auto ret{connection.post(masto::API::v1::statuses, params)}; if (ret) { - cout << ret.answer << '\n'; + cout << ret.body << '\n'; } else { - std::cerr << "Error code: " << ret.error_code << '\n'; - return ret.error_code; + std::cerr << "Error code: " << ret.curl_error_code << '\n'; + return ret.curl_error_code; } return 0;