#if __has_include("mastodonpp.hpp")
# include "mastodonpp.hpp"
#else
# include <mastodonpp/mastodonpp.hpp>
#endif
#if __has_include(<nlohmann/json.hpp>)
# include <nlohmann/json.hpp>
# include <cstdlib>
# include <iostream>
# include <string>
# include <string_view>
# include <vector>
using json = nlohmann::json;
using std::cerr;
using std::cout;
using std::exit;
using std::string_view;
using std::to_string;
using std::vector;
void handle_error(const masto::answer_type &answer);
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::timelines_public,
{{"limit", "4"}, {"local", "true"}})};
if (answer)
{
auto statuses{json::parse(answer.body)};
for (const auto &status : statuses)
{
const auto acct{status["account"]["acct"].get<string_view>()};
const auto content{status["content"].get<string_view>()};
const auto id{status["id"].get<string_view>()};
cout << acct << " wrote status " << id << ": \n";
cout << " " << content.substr(0, 76) << " …\n";
const auto tags{status["tags"]};
if (!tags.empty())
{
cout << " Tags: ";
for (const auto &tag : tags)
{
cout << '#' << tag["name"].get<string_view>() << " ";
}
cout << '\n';
}
const auto n_attachments{status["media_attachments"].size()};
if (n_attachments > 0)
{
cout << " " << n_attachments << " attachment";
if (n_attachments > 1)
{
cout << "s";
}
cout << ".\n";
}
}
}
else
{
handle_error(answer);
}
}
catch (const masto::CURLException &e)
{
cerr << e.what() << '\n';
}
catch (const nlohmann::detail::exception &e)
{
cerr << "JSON exception: " << e.what() << '\n';
}
}
void handle_error(const masto::answer_type &answer)
{
if (answer.curl_error_code == 0)
{
cerr << "HTTP status: " << answer.http_status << '\n';
}
else
{
cerr << "libcurl error " << to_string(answer.curl_error_code) << ": "
<< answer.error_message << '\n';
}
exit(1);
}
#else
# include <iostream>
int main()
{
std::cout << "Example could not be compiled "
"because nlohmann-json was not found.\n";
}
#endif // __has_include(<nlohmann/json.hpp>)