summary refs log tree commit diff stats
path: root/timeline.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'timeline.cpp')
-rw-r--r--timeline.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/timeline.cpp b/timeline.cpp new file mode 100644 index 0000000..bc34329 --- /dev/null +++ b/timeline.cpp
@@ -0,0 +1,66 @@
1#include "timeline.h"
2#include <sstream>
3#include <hkutil/string.h>
4#include <iostream>
5
6timeline::timeline(mastodonpp::API::endpoint_type endpoint) : endpoint_(endpoint)
7{
8}
9
10std::list<nlohmann::json> timeline::poll(mastodonpp::Connection& connection)
11{
12 std::string maxId;
13 std::list<nlohmann::json> result;
14
15 for (int i = 0; i < 5; i++)
16 {
17 mastodonpp::parametermap arguments;
18
19 if (i > 0)
20 {
21 arguments["max_id"] = maxId;
22 }
23
24 if (hasSince_)
25 {
26 arguments["since_id"] = sinceId_;
27 }
28
29 auto answer{connection.get(endpoint_, arguments)};
30 if (!answer)
31 {
32 if (answer.curl_error_code == 0)
33 {
34 std::cout << "HTTP status: " << answer.http_status << std::endl;
35 }
36 else
37 {
38 std::cout << "libcurl error " << std::to_string(answer.curl_error_code)
39 << ": " << answer.error_message << std::endl;
40 }
41 return {};
42 }
43
44 nlohmann::json rjs = nlohmann::json::parse(answer.body);
45
46 if (rjs.empty())
47 {
48 break;
49 }
50
51 for (auto& single : rjs)
52 {
53 result.push_back(single);
54 }
55
56 maxId = result.back()["id"].get<std::string>();
57 }
58
59 if (!result.empty())
60 {
61 sinceId_ = result.front()["id"].get<std::string>();
62 hasSince_ = true;
63 }
64
65 return result;
66}