From 8b197134ab3bd5679224477cf908cc74dd591a69 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 6 Oct 2019 15:10:52 +0200 Subject: [PATCH] Added favourites-example. --- examples/example03_get_favourites.cpp | 91 +++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 examples/example03_get_favourites.cpp diff --git a/examples/example03_get_favourites.cpp b/examples/example03_get_favourites.cpp new file mode 100644 index 0000000..067ecc5 --- /dev/null +++ b/examples/example03_get_favourites.cpp @@ -0,0 +1,91 @@ +// This file is part of mastodon-cpp. +// Get 2 pages of 5 favourites each. + +// Don't compile this if the Easy-interface is turned off +#ifndef WITHOUT_EASY + +#include +#include +#include +#include "mastodon-cpp.hpp" +#include "easy/all.hpp" + +using std::cout; +using std::cerr; +using std::endl; +using std::string; +using std::vector; +using namespace Mastodon; + +void print_favs(Easy::API &masto, const string &max_id = "") +{ + // Set up parameters. + Mastodon::parameters params = + { + { "limit", { "5" } }, + { "exclude_types", { "follow", "reblog", "mention" } } + }; + // Set max_id if given. + if (!max_id.empty()) + { + params.push_back({ "max_id", { max_id } }); + } + + // Retrieve the last 5 favourites. + return_call ret = masto.get(API::v1::notifications, params); + // If no error was returned. + if (ret) + { + // Convert answer to vector of strings, loop through vector. + for (const string &str : Easy::json_array_to_vector(ret.answer)) + { + // Construct Mastodon::Easy::Notification from string. + const Easy::Notification notif(str); + cout << notif.created_at().strtime("%F %T: "); + cout << notif.account().display_name() << " favourited a status.\n"; + } + } + else + { + // Print error message. + cerr << ret.error_message << endl; + // Print HTTP status code, if there is one. + if (ret.http_error_code != 0) + { + cerr << "HTTP status code: " + << std::to_string(ret.http_error_code) << endl; + } + } +} + +int main(int argc, char *argv[]) +{ + const vector args(argv, argv + argc); + if (args.size() < 3) + { + std::cerr << "usage: " << args[0] << " \n"; + return 1; + } + + // Construct a Mastodon::Easy object. + Easy::API masto(args[1], args[2]); + print_favs(masto); + + cout << "\nLink header is: " << masto.get_header("link") << "\n\n"; + + // Get the max_id for pagination. + // See + print_favs(masto, masto.get_link().max_id()); + // The same: print_favs(masto, masto.get_link().next()); + + return 0; +} + +#else +#include +int main() +{ + std::cout << "mastodon-cpp was compiled without Easy support.\n"; + return 255; +} +#endif // WITHOUT_EASY