From 88e2846e835cb4c873b6bd1c4fa49c780079519e Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 1 Apr 2018 04:16:40 +0200 Subject: [PATCH] added example 13, fixed other examples --- examples/example10_simplify.cpp | 3 +- examples/example12_easy_laststatus.cpp | 1 + examples/example13_easy_stream.cpp | 97 ++++++++++++++++++++++++++ src/mastodon-cpp.hpp | 1 + 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 examples/example13_easy_stream.cpp diff --git a/examples/example10_simplify.cpp b/examples/example10_simplify.cpp index baf4a53..0212cf6 100644 --- a/examples/example10_simplify.cpp +++ b/examples/example10_simplify.cpp @@ -43,11 +43,10 @@ int main(int argc, char *argv[]) return 1; } - std::string answer; std::uint16_t ret = 0; EasyToot masto(argv[1], argv[2]); - masto.toot("Test"); + ret = masto.toot("Test"); if (ret != 0) { std::cerr << "Error: " << ret << '\n'; diff --git a/examples/example12_easy_laststatus.cpp b/examples/example12_easy_laststatus.cpp index 8c481ad..f17d2b7 100644 --- a/examples/example12_easy_laststatus.cpp +++ b/examples/example12_easy_laststatus.cpp @@ -1,4 +1,5 @@ /* This file is part of mastodon-cpp. + * Prints some information about your last status. */ // Don't compile this if the Easy-interface is turned off diff --git a/examples/example13_easy_stream.cpp b/examples/example13_easy_stream.cpp new file mode 100644 index 0000000..0831f5a --- /dev/null +++ b/examples/example13_easy_stream.cpp @@ -0,0 +1,97 @@ +/* This file is part of mastodon-cpp. + * Prints some information from the public timeline. + */ + +// Don't compile this if the Easy-interface is turned off +#ifndef WITHOUT_EASY + +#include +#include +#include +#include +#include +#include + +// If we are compiling mastodon-cpp, use another include path +#ifdef MASTODON_CPP + #include "mastodon-cpp.hpp" + #include "easy/all.hpp" +#else + #include + // Include all headers in mastodon-cpp/easy/ + #include +#endif + +using Mastodon::API; +using Mastodon::Easy; +using std::cout; + +int main(int argc, char *argv[]) +{ + if (argc < 3) + { + std::cerr << "usage: " << argv[0] << " \n"; + return 1; + } + + cout << "I'll show you the public timeline. Press CTRL-C to abort\n"; + + // These have to be static in order to use them in- and outside the thread + static std::string stream; + // You can abort the stream with this pointer (ptr->abort_stream()) + static std::unique_ptr ptr; + + // Start a new thread for the stream + std::thread pub_tl([=] + { + Easy masto(argv[1], argv[2]); + masto.get_stream(Mastodon::API::v1::streaming_public, stream, ptr); + }); + + while (true) + { + // Parse event stream and clear it afterwards + std::vector events = Easy::parse_stream(stream); + stream.clear(); + + // The contents of the stream are now in a vector of pairs + // { Easy::event_type, std::string } + for (const Easy::stream_event &event : events) + { + Easy::Status status; + Easy::Notification notification; + + // Print out some information about the events + switch (event.first) + { + case Easy::event_type::Update: + status.from_string(event.second); + cout << "Status from: " << status.account().acct() + << " (" << status.url() << ")\n"; + break; + case Easy::event_type::Notification: + notification.from_string(event.second); + cout << "Notification involving: " + << notification.account().acct() + << " (" << notification.id() << ")\n"; + break; + case Easy::event_type::Delete: + cout << "Deleted: " << event.second << '\n'; + break; + default: + cout << "Something undefined happened. 😱\n"; + } + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + pub_tl.join(); +} + +#else +int main() +{ + printf("mastodon-cpp was compiled without Easy support.\n"); + return 255; +} +#endif // WITHOUT_EASY diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 4b1b535..c328864 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -38,6 +38,7 @@ * @example example10_simplify.cpp * @example example11_post_media.cpp * @example example12_easy_laststatus.cpp + * @example example13_easy_laststatus.cpp */ namespace Mastodon {