Switched from mastodon-cpp to mastodonpp.

This commit is contained in:
tastytea 2020-05-16 17:38:10 +02:00
parent 3e5a7def80
commit 73aa20a193
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 26 additions and 35 deletions

View File

@ -12,7 +12,7 @@ pkg_check_modules(jsoncpp REQUIRED IMPORTED_TARGET jsoncpp)
find_package(mastodonpp 0.5.4 REQUIRED CONFIG) find_package(mastodonpp 0.5.4 REQUIRED CONFIG)
find_package(Filesystem REQUIRED) find_package(Filesystem REQUIRED)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)

View File

@ -1,6 +1,7 @@
// Author: tastytea <tastytea@tastytea.de> // Author: tastytea <tastytea@tastytea.de>
// CC-0 / Public Domain // CC-0 / Public Domain
#include <mastodonpp/instance.hpp>
#include <string> #include <string>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
@ -15,8 +16,7 @@
#include <curlpp/Exception.hpp> #include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp> #include <curlpp/Infos.hpp>
#include <json/json.h> #include <json/json.h>
#include <mastodon-cpp/easy/easy.hpp> #include <mastodonpp/mastodonpp.hpp>
#include <mastodon-cpp/easy/entities/status.hpp>
#include "xdgjson/src/xdgjson.hpp" #include "xdgjson/src/xdgjson.hpp"
namespace curlopts = curlpp::options; namespace curlopts = curlpp::options;
@ -29,7 +29,7 @@ using std::chrono::system_clock;
using std::chrono::hours; using std::chrono::hours;
using std::uint_fast16_t; using std::uint_fast16_t;
const string version = "2019-04-20_2"; const string version = "2020-05-16_1";
struct Holiday struct Holiday
{ {
@ -82,38 +82,30 @@ const string get_date(const system_clock::time_point &timepoint,
const string get_token(const string &instance) const string get_token(const string &instance)
{ {
Mastodon::API masto(instance, ""); mastodonpp::Instance server{instance, ""};
Mastodon::return_call ret; mastodonpp::Instance::ObtainToken token{server};
string client_id, client_secret, url;
string code, access_token; auto answer{token.step_1("feiertagebot",
ret = masto.register_app1("feiertagebot", "write:statuses",
"urn:ietf:wg:oauth:2.0:oob", "https://schlomp.space/tastytea/feiertagebot")};
"write:statuses", if (!answer)
"https://schlomp.space/tastytea/feiertagebot",
client_id,
client_secret,
url);
if (!ret)
{ {
cerr << "Error " << std::to_string(ret.error_code) << endl; cerr << "Error " << answer.error_message << endl;
return ""; return "";
} }
cout << "Visit " << url << " to authorize this application.\n"; string code;
cout << "Visit " << answer << " to authorize this application.\n";
cout << "Insert code: "; cout << "Insert code: ";
std::getline(std::cin, code); std::getline(std::cin, code);
ret = masto.register_app2(client_id, answer = token.step_2(code);
client_secret, if (!answer)
"urn:ietf:wg:oauth:2.0:oob",
code,
access_token);
if (!ret)
{ {
cerr << "Error " << std::to_string(ret.error_code) << endl; cerr << "Error " << answer.error_message << endl;
return ""; return "";
} }
return access_token; return answer.body;
} }
int main() int main()
@ -213,16 +205,15 @@ int main()
if (!output.empty()) if (!output.empty())
{ {
Mastodon::Easy::return_entity<Mastodon::Easy::Status> ret; mastodonpp::Instance server{instance, access_token};
Mastodon::Easy::API masto(instance, access_token); mastodonpp::Connection connection{server};
masto.exceptions(true); string status{output + "#bot"};
Mastodon::Easy::Status status; const auto answer{connection.post(mastodonpp::API::v1::statuses,
status.content(output + "#bot"); {{"status", output}})};
ret = masto.send_post(status); if (!answer)
if (!ret)
{ {
cerr << "Error " << std::to_string(ret.error_code) << endl; cerr << "Error " << answer.error_message << endl;
return ret.error_code; return answer.curl_error_code;
} }
} }
} }