From 12b25928d0ec2b18dda075b8c3b37b8a7e6f1172 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 15 Nov 2022 17:44:56 -0500 Subject: Bot now posts to Mastodon instead of Twitter --- timeline.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 timeline.cpp (limited to 'timeline.cpp') diff --git a/timeline.cpp b/timeline.cpp new file mode 100644 index 0000000..bc34329 --- /dev/null +++ b/timeline.cpp @@ -0,0 +1,66 @@ +#include "timeline.h" +#include +#include +#include + +timeline::timeline(mastodonpp::API::endpoint_type endpoint) : endpoint_(endpoint) +{ +} + +std::list timeline::poll(mastodonpp::Connection& connection) +{ + std::string maxId; + std::list result; + + for (int i = 0; i < 5; i++) + { + mastodonpp::parametermap arguments; + + if (i > 0) + { + arguments["max_id"] = maxId; + } + + if (hasSince_) + { + arguments["since_id"] = sinceId_; + } + + auto answer{connection.get(endpoint_, arguments)}; + if (!answer) + { + if (answer.curl_error_code == 0) + { + std::cout << "HTTP status: " << answer.http_status << std::endl; + } + else + { + std::cout << "libcurl error " << std::to_string(answer.curl_error_code) + << ": " << answer.error_message << std::endl; + } + return {}; + } + + nlohmann::json rjs = nlohmann::json::parse(answer.body); + + if (rjs.empty()) + { + break; + } + + for (auto& single : rjs) + { + result.push_back(single); + } + + maxId = result.back()["id"].get(); + } + + if (!result.empty()) + { + sinceId_ = result.front()["id"].get(); + hasSince_ = true; + } + + return result; +} -- cgit 1.4.1