#if __has_include("mastodonpp.hpp")
# include "mastodonpp.hpp"
#else
# include <mastodonpp/mastodonpp.hpp>
#endif
#include <chrono>
#include <iostream>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
using std::cerr;
using std::cout;
using std::endl;
using std::string_view;
using std::thread;
using std::to_string;
using std::vector;
using std::this_thread::sleep_for;
int main(int argc, char *argv[])
{
const vector<string_view> args(argv, argv + argc);
if (args.size() <= 1)
{
cerr << "Usage: " << args[0] << " <instance hostname>\n";
return 1;
}
try
{
masto::Instance instance{args[1], {}};
masto::Connection connection{instance};
auto answer{connection.get(masto::API::v1::streaming_health)};
if (answer && answer.body == "OK")
{
thread stream_thread{[&]
{
answer = connection.get(masto::API::v1::streaming_public);
}};
for (auto counter{0}; counter < 5; ++counter)
{
sleep_for(2s);
for (const auto &event : connection.get_new_events())
{
cout << event.type << ": " << event.data.substr(0, 70)
<< " …" << endl;
}
}
connection.cancel_stream();
stream_thread.join();
}
else
{
if (answer.curl_error_code == 0)
{
cerr << "HTTP status: " << answer.http_status << endl;
}
else
{
cerr << "libcurl error " << to_string(answer.curl_error_code)
<< ": " << answer.error_message << endl;
}
}
}
catch (const masto::CURLException &e)
{
cerr << e.what() << endl;
}
return 0;
}
C++ wrapper for the Mastodon API.
Definition: api.hpp:25