diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2022-11-16 12:58:44 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2022-11-16 12:58:44 -0500 |
commit | 04bd93a63692977c947542f8ff88cca2b9662c97 (patch) | |
tree | 44ca4932dc1c65eb79cb9f60a7a8311c5ebc267d /timeline.cpp | |
parent | d16ccf85c75f34c142736b6e1b7dc491898a1932 (diff) | |
download | father-04bd93a63692977c947542f8ff88cca2b9662c97.tar.gz father-04bd93a63692977c947542f8ff88cca2b9662c97.tar.bz2 father-04bd93a63692977c947542f8ff88cca2b9662c97.zip |
Bot now uses Mastodon instead of Twitter
Diffstat (limited to 'timeline.cpp')
-rw-r--r-- | timeline.cpp | 66 |
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 | |||
6 | timeline::timeline(mastodonpp::API::endpoint_type endpoint) : endpoint_(endpoint) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | std::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 | } | ||